从php中回显的html中拆分出jquery代码

问题描述 投票:0回答:1

所以我只是涉足这个问题而且很可能做错了。但它是一个概念证明,所以我可以获得让别人做正确的权力。 我使用PHP脚本登录和从表单发布变量。

问题:我正在将一个包含css,html和jquery脚本的html代码全部放在一个文件中。哪个适用于css和html部分,但jquery的东西不起作用。

有没有办法将jquery部分放在我的php脚本中以使其正常运行,或者是否需要拆分并在html中引用/加载。 (我认为这是正确的方法,但我不知道如何或从哪里开始。)

代码:(绑定到ldap并进行身份验证后,触发此块)

// verify binding
    if ($ldap_bind) {
    echo "
<head>
<link - stylesheet \link>
</head>
<form class=\"form-horizontal\" action\"form.php\" method=\"POST\" enctype=\"multipart/form-data\">
<fieldset>
<body>
html code....lots of div boxes
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>
<script>
$(document).ready(function(){
Various Jquery functions that hide/unhide div boxes
});
</script>
</body>
</fieldset>
</form>
";

            } else {
                echo "<p class='text-error'>Unable to log you in:</p>";
php jquery html echo
1个回答
1
投票

首先,您的HTML结构非常混乱。看看我如何“重新组织”它,并随时阅读像this one这样的教程。

其次,你可以使用PHP标签(<?php?>)来避免多行echo和所有报价逃避混乱。这些标记之外的内容不会被PHP处理并按原样发送到浏览器。所以你可以在那里写“通常”的HTML。

第三,将你的脚本放在像<form>这样的HTML之外是一个很好的做法。通常将库调用放在<head>中...但是它也常见于文档的末尾,就在</html>上方,而不是<head>。当然,并不奇怪的是标记中的每个地方。

// verify binding
if ($ldap_bind) {
  ?>
  <head>
  <title>My page title</title>
  <link rel="stylesheet" href="...">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>
  <body>
    <form class="form-horizontal" action="form.php" method="POST" enctype="multipart/form-data">
      <fieldset>
        html code....lots of div boxes
      </fieldset>
    </form>

    <script>
    $(document).ready(function(){
      //Various jQuery functions that hide/unhide div boxes
    });
    </script>
  </body>
  <?php
} else {
  ?>
  <p class='text-error'>Unable to log you in:</p>
© www.soinside.com 2019 - 2024. All rights reserved.