简体   繁体   English

如何在php中编写javascript代码

[英]how to write javascript code inside php

i have a got a form, on clicking the submit button:我有一个表格,点击提交按钮:

  1. I want to do some task in the same file (db task) AND我想在同一个文件中做一些任务(数据库任务)和
  2. I want the form data to be sent to test.php with the redirection我希望通过重定向将表单数据发送到 test.php

here is my code这是我的代码

    <?php
    if(isset($_POST['btn'])){
        //do some task
    ?>
    <script type="text/javascript">
        var e = document.getElementById('testForm'); e.action='test.php'; e.submit();</script>
    <?php
    }
    ?>
<form name="testForm" id="testForm"  method="POST"  >
    <input type="submit" name="btn" value="submit" autofocus  onclick="return true;"/>
 </form>

but not able to submit the form, if i call the javascript code on onClick, it works.what is the problem in this code, Is there any work around for this但无法提交表单,如果我在 onClick 上调用 javascript 代码,它可以工作。此代码有什么问题,是否有任何解决方法

Just echo the javascript out inside the if function只需在 if 函数中回显 javascript

 <form name="testForm" id="testForm"  method="POST"  >
     <input type="submit" name="btn" value="submit" autofocus  onclick="return true;"/>
 </form>
 <?php
    if(isset($_POST['btn'])){
        echo "
            <script type=\"text/javascript\">
            var e = document.getElementById('testForm'); e.action='test.php'; e.submit();
            </script>
        ";
     }
  ?>

Lately I've come across yet another way of putting JS code inside PHP code.最近我发现了另一种将 JS 代码放入 PHP 代码的方法。 It involves Heredoc PHP syntax.它涉及 Heredoc PHP 语法。 I hope it'll be helpful for someone.我希望它会对某人有所帮助。

<?php
$script = <<< JS

$(function() {
   // js code goes here
});

JS;
?>

After closing the heredoc construction the $script variable contains your JS code that can be used like this:关闭 heredoc 结构后, $script 变量包含您可以像这样使用的 JS 代码:

<script><?= $script ?></script>

The profit of using this way is that modern IDEs recognize JS code inside Heredoc and highlight it correctly unlike using strings.使用这种方式的好处是现代 IDE 可以识别 Heredoc 中的 JS 代码并正确突出显示它,而不是使用字符串。 And you're still able to use PHP variables inside of JS code.而且您仍然可以在 JS 代码中使用 PHP 变量。

You can put up all your JS like this, so it doesn't execute before your HTML is ready您可以像这样放置所有 JS,因此它不会在您的 HTML 准备好之前执行

$(document).ready(function() {
   // some code here
 });

Remember this is jQuery so include it in the head section.请记住这是 jQuery,因此将其包含在 head 部分中。 Also see Why you should use jQuery and not onload另请参阅为什么您应该使用 jQuery 而不是 onload

At the time the script is executed, the button does not exist because the DOM is not fully loaded.在执行脚本时,按钮不存在,因为 DOM 未完全加载。 The easiest solution would be to put the script block after the form.最简单的解决方案是将脚本块放在表单之后。

Another solution would be to capture the window.onload event or use the jQuery library (overkill if you only have this one JavaScript).另一种解决方案是捕获window.onload事件或使用jQuery 库(如果您只有这一个 JavaScript,那就大材小用了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM