dtdh11647
2015-12-01 06:55
浏览 65
已采纳

PHP提交后调用JavaScript函数

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>

图片转代码服务由CSDN问答提供 功能建议

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

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

 &lt;?php 
 \  nif(isset($ _ POST ['next'])){
 //一些PHP代码
 
 echo“&lt; script&gt; checkDates();&lt; / script&gt;”; 
} 
 
?  &gt; 
 
&lt; form name =“form1”id =“form1”method =“post”action =“&lt;?php print $ thispage;?&gt;”  onsubmit =“return checkForm()”&gt; 
 
 //一些HTML代码
 
&lt; / form&gt; 
 
&lt; script&gt; 
var isDateTimeValid = true; 
 
function checkDates(){
  alert(“Hello”); 
 isDateTimeValid = false; 
} 
 
函数checkForm(){
 
 if if(isDateTimeValid == true){
 return true; 
} 
 else {
 返回false; 
} 
} 
 
&lt; / script&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dqsvnsad79721 2015-12-01 07:22
    已采纳

    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>";
         } 
         ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongrong5189 2015-12-01 07:06

    I think you should first do this in your javascript function

    <form method="post" action="" id="form">
        <!-- some html code -->
    </form>
    
    <script>
       // your all code
    
       $("#form").submit(function(e) {
          e.preventDefault();
          if (isDateTimeValid == true) {
            $.post(
               // post logic with proper url
            );
          }
          else {
            // return some error message
          }
      });
    </script>
    
    评论
    解决 无用
    打赏 举报
  • doudou348131346 2015-12-01 07:07

    try putting the javascript at the top. since you are calling the function which might have not been defined yet in the DOM

    评论
    解决 无用
    打赏 举报
  • duanhao1004 2015-12-01 07:11

    the problem was your variable and function scoping all the identifiers must be declared. If you run your code with "browser console output" opened, you will see an "Uncaught ReferenceError: checkDates is not defined" after your form submit. If you put the php code after the javascript function deceleration it will work.

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

    see the browser console output.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题