douqiang4245 2015-09-11 11:20
浏览 62
已采纳

ajax成功函数从php接收信息

I'm not able to receive data from php script with ajax. I have an form sending the information to the php script. This is just a test. If it works I want to receive information from a database.

    $('.search-member-submit-btn').click(function () {
    var firstname = $("#search-member-firstname").val();
    var lastname = $("#search-member-lasttname").val();

    $.ajax({//AJAX request
        type: "POST",
        url: "/website/include/process/send_membersearchrequest_process.php",
        async: true,
        data: {firstname: firstname, lastname: lastname},
        success: function (data) {
            alert(data);
            $(".search-member-result-address").html(data);
        },
    });

});

HTML:

<div class="col-md-3">
     <div class="search-member-result-container">
          <address class="search-member-result-address">

          </address>
      </div>
</div>
<div class="search-member-form">
     <input class="search-member-input" id="search-member-lastname" placeholder="Name">
     <input class="search-member-input" id="search-member-firstname" placeholder="Vorname">
     <div class="search-member-submit-btn">suchen</div>
</div>

PHP

<?php 
    $lastname = $_POST['lastname'];
    echo $lastname;
?>
  • 写回答

2条回答 默认 最新

  • drr7731 2015-09-11 11:26
    关注

    Add response type like :

     $.ajax({//AJAX request
            type: "post",
            url: "/website/include/process/send_membersearchrequest_process.php",
            async: true,
            data: {firstname: firstname, lastname: lastname},
            dataType: 'json', // You have to add this line
            success: function (data) {
                alert(data.response);
                $(".search-member-result-address").html(data.response);
            },
        });
    

    and your PHP file :

    <?php 
    $lastname = $_POST['lastname'];
    echo json_encode(array('response' => $lastname));
    exit();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP