douping1993 2012-04-27 11:03
浏览 39
已采纳

HTML表单停止重定向并允许PHP从HTML文件中调用JavaScript

I'd like to have a form on a HTML page not refresh when it's sent, which I've done, but I'd also like to allow the echo command in the PHP file to be able to call JavaScript from within the HTML file.

So far, all the echo commands aren't being carried out, which isn't what I expected.

Here's some code from the HTML and PHP files:

HTML:

<script type="text/javascript">

function functionInFile() {
    alert("recieved");
}

    $(function() {  
      $(".postform").submit(function() {  
      var content = $(this).serialize();

      $.post('signup.php?', content);

      return false;

      });  
    });     

</script>

and the PHP:

echo '<script type=\'text/javascript\'>functionInFile()</script>';

So basically, I'd like the PHP file to be able to invoke a function in the HTML file, while not being redirected when I click submit.

Any help appreciated.

  • 写回答

3条回答 默认 最新

  • duanjian4150 2012-04-27 11:06
    关注

    You can use the success callback of the $.post() to execute a function which your PHP passes back. Try this:

    PHP

    // do some stuff with the posted data
    echo 'functionInFile'; // name of js function to execute in calling page
    

    jQuery

    function functionInFile() {
        alert("recieved");
    }
    
    $(function() {  
        $(".postform").submit(function() {  
            $.post(
                'signup.php?', 
                $(this).serialize(),
                function(func) {
                    window[func]();
                },
                'text'
            );
            return false;
        });  
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题