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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么