dragon0118 2016-02-04 20:42
浏览 31
已采纳

PHP没有拿起Ajax POST

I'm trying to send some data from a HTML page to a PHP page.

I've made sure that the dataString is actually sending text by using console log.

It shows up as pid=12345

My AJAX is:

$.ajax({
  type: "POST",
  url: 'Task2.php',
    data: {pid:res[1]},
  success: function(data) {
    //alert(data);
      $("#container").html(data); 
      console.log( data );
 }
});

My PHP is:

$temp = $_POST['pid'];

The PHP isn't picking it up even if I do it manually by going to myserver/Task2.php?pid=12345.

  • 写回答

3条回答 默认 最新

  • dtt5830659 2016-02-04 20:48
    关注

    I think you may be looking for something like this...

    var dataString = res[1];
    console.log( 'pid=' + dataString );
    
    $.ajax({
      type: "POST",
      url: 'Task2.php',
      data: { pid: dataString },
      success: function(data) {
        alert(data);
      }
    });
    

    by specifying data: { pid:dataString } in your ajax call, your telling it to POST the dataString value as a form element named "pid". You chould be able to pick it up PHP side with $_POST['pid'];

    EDIT

    Here is a full, working example. All self contained in a single PHP file. Should help you get it sorted out. The reason for the isset($_GET['testing']) at the top is because I'm using the same file in the ajax call too. Just fyi...

    <?php
        if (isset($_GET['testing'])) {
            if (isset($_POST['pid'])) {
                echo $_POST['pid'];
            } else {
                echo 'not set';
            }
        }
        else
        {
    ?>
    <html>
    <script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
    <a href="javascript:void(0);" onclick="test()">test</a>
    <script type="text/javascript">
        function test() {
            var dataString = "pid=testing123";
            $.ajax({
                type: "POST",
                url: "test.php?testing",
                data: dataString,
                success: function(result) {
                    alert(result);
                }
            });
        }
    </script>
    </html>
    <?php
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数