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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM