weixin_33725515 2017-12-05 16:47 采纳率: 0%
浏览 13

php没有从ajax获取数据

What I'm doing is I want to detect which reply button I click and send its index to php.

jQuery:

$(".reply").each(function (index5) {

$(".reply_button").each(function (index_b) {

            if(index_b==index5){
                $(this).on("click",function (e) {
                    e.preventDefault();
                    var $this = $(this)
                    $.ajax({
                        type:"POST",
                        url:"/yyqGS/public/form/reply.php",
                        data:{index:index_b},
                        dataType: "json",
                        success:function (d) {
                            console.log(d);
                        }
                    });
                })
            }
        });
});

PHP:

 <?php 
    $n = $_POST['index'];
    echo $n+1;

I want to pass the variable index_b to php the file, so that $_POST['index'] can get the value.

But the result on php page is 1, which means it doesn't get the value of index_b from ajax.

On the other hand, what is showed on console is, HOWEVER, correct number! Like, if I click the first reply button, it shows 1, if I click second, it shows 2, and so on...

result on php: always 1.

result on console: correct number.

I couldn't figure out how it happened!


UPDATE: so i changed console.log() to submit(), i want ajax would send the index to php first, and then another a form will send like 'text' i type to php. But, what's on php page is still 1.

$this.parent() is a form.

So what i really want to do is i want to send the variable index_b to php, and also, at the same time, send those input data to php. So i can use them both.

$(".reply_button").each(function (index_b) {

            if(index_b==index5){
                $(this).on("click",function (e) {
                    e.preventDefault();
                    var $this = $(this)
                    $.ajax({
                        type:"POST",
                        url:"/yyqGS/public/form/reply.php",
                        data:{index:index_b},
                        success:function (d) {
                            $this.parent().submit();
                        }
                    });
                })
            }
        });
  • 写回答

1条回答 默认 最新

  • weixin_33739541 2017-12-05 16:56
    关注

    Your PHP page doesn't store the $_POST value in any way - it receives the data through the ajax request (or other requests that send $_POST['index'] data).

    When you go to form.php directly, it doesn't know what value $_POST['index'] has (because it's not set) so it uses zero (0) in the math formula. Thus, always outputs one (1).

    If you wanted to be able to quickly test the form.php page directly, you could change $_POST['index'] to $_REQUEST['index'], which allows for POST and GET parameters. Meaning, you could test at form.php?index=17 and get output 18 (same output from your existing ajax). Granted, that has it's own potential issues, so I'd recommend just continue as is with the knowledge that your code does work.

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)