Javascript 如何在php中编写javascript代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10596218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:03:23  来源:igfitidea点击:

how to write javascript code inside php

phpjavascriptforms

提问by n92

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
  1. 我想在同一个文件中做一些任务(数据库任务)和
  2. 我希望通过重定向将表单数据发送到 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 代码,它可以工作。此代码有什么问题,是否有任何解决方法

回答by keto23

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>
        ";
     }
  ?>

回答by nischayn22

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. Also see Why you should use jQuery and not onload

请记住这是 jQuery,因此将其包含在 head 部分中。另请参阅为什么您应该使用 jQuery 而不是 onload

回答by chiborg

At the time the script is executed, the button does not exist because the DOM is not fully loaded. The easiest solution would be to put the script block after the form.

在执行脚本时,按钮不存在,因为 DOM 未完全加载。最简单的解决方案是将脚本块放在表单之后。

Another solution would be to capture the window.onloadevent or use the jQuery library(overkill if you only have this one JavaScript).

另一种解决方案是捕获window.onload事件或使用jQuery 库(如果您只有这个 JavaScript,那就有点矫枉过正了)。

回答by Peter Dvukhrechensky

Lately I've come across yet another way of putting JS code inside PHP code. It involves Heredoc PHP syntax. I hope it'll be helpful for someone.

最近我发现了另一种将 JS 代码放入 PHP 代码的方法。它涉及 Heredoc PHP 语法。我希望它会对某人有所帮助。

<?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. And you're still able to use PHP variables inside of JS code.

使用这种方式的好处是现代 IDE 可以识别 Heredoc 中的 JS 代码并正确突出显示它,而不是使用字符串。而且您仍然可以在 JS 代码中使用 PHP 变量。