doushang1890 2010-10-30 21:14
浏览 90
已采纳

Ajax请求到同一页面

This question may seem completely stupid, but say i have a PHP page with some form processing at the top in php and a html form underneath with the action of submitting to same page and method of post. How do i get the result via ajax, ie. send form to self without refreshing the page, if that makes sense? Thanks

  • 写回答

2条回答 默认 最新

  • double820122 2010-10-30 21:30
    关注

    It sounds like you're asking about Ajax basics, right? I suggest using jQuery to handle the Ajax part.

    Put jQuery in your page, and then do something like

    $(document).ready(function(){
      $('#submit_button').click(function(){
        var something='value to send to PHP';
        $.post('name_of_page.php',{"a_var":something},function(data){ /* do something with the data you received back*/ },'json');
        });
      });
    

    Then in your PHP page, set up to handle a post or normal HTML output.

    <?php
    if($_POST['a_var']){
      $result=do_something($_POST['a_var']);
      echo json_encode($result);
      exit;
      }
    //if there was no POST value, it continues to here
    <html>
    This is the rest of your page.
    You'd have the form and the above javascript and so on here.
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。