PHP提交后调用JavaScript函数(Calling JavaScript function after PHP Submit)

我试图弄清楚为什么在提交表单后没有调用此函数。 “checkForm()”JS函数在提交表单时工作正常,但我似乎无法使checkDates()函数正常工作。 我试图将它移动到PHP代码上方,并且没有任何警告消息出现。 任何帮助非常感谢,谢谢!

这是我的文件的伪代码版本:

<?php if(isset($_POST['next'])){ // Some PHP code echo "<script> checkDates(); </script>"; } ?> <form name="form1" id="form1" method="post" action="<?php print $thispage;?>" onsubmit="return checkForm()"> // Some HTML code </form> <script> var isDateTimeValid = true; function checkDates() { alert("Hello"); isDateTimeValid = false; } function checkForm () { if (isDateTimeValid == true) { return true; } else { return false; } } </script>

I'm trying to figure out why this function isn't being called after the form is submitted. The "checkForm()" JS function works fine when the form is submitted, but I can't seem to get the checkDates() function working. I tried moving it above the PHP code and no alert message showed up. Any help is greatly appreciated, thank you!

Here's the pseudo code version of my file:

<?php if(isset($_POST['next'])){ // Some PHP code echo "<script> checkDates(); </script>"; } ?> <form name="form1" id="form1" method="post" action="<?php print $thispage;?>" onsubmit="return checkForm()"> // Some HTML code </form> <script> var isDateTimeValid = true; function checkDates() { alert("Hello"); isDateTimeValid = false; } function checkForm () { if (isDateTimeValid == true) { return true; } else { return false; } } </script>

最满意答案

试试这个会起作用。

这里你的php代码必须在你的文件中最后才能起作用。 否则不。

<form name="form1" id="form1" method="post" action="#" onsubmit="return checkForm()"> // Some HTML code <input type="hidden" name="next" value="sd"> <input type="submit"> </form> <script> var isDateTimeValid = true; function checkDates() { alert("Hello"); isDateTimeValid = false; } console.log(isDateTimeValid); function checkForm () { if (isDateTimeValid == true) { return true; } else { return false; } } </script> <?php if(isset($_POST['next'])){ echo "<script> checkDates(); </script>"; } ?>

try this it will work.

here your php code must be last in your file then it will work. otherwise not.

<form name="form1" id="form1" method="post" action="#" onsubmit="return checkForm()"> // Some HTML code <input type="hidden" name="next" value="sd"> <input type="submit"> </form> <script> var isDateTimeValid = true; function checkDates() { alert("Hello"); isDateTimeValid = false; } console.log(isDateTimeValid); function checkForm () { if (isDateTimeValid == true) { return true; } else { return false; } } </script> <?php if(isset($_POST['next'])){ echo "<script> checkDates(); </script>"; } ?>

更多推荐