-1

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();
                        }
                    });
                })
            }
        });
Kevin Yang
  • 59
  • 9

1 Answers1

1

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.

DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • oh i guess that explains why, but how do i check out if the value has already been sent to php? – Kevin Yang Dec 05 '17 at 17:00
  • It's sent on a per-request basis. Your `console.log(d)` verifies that the data was sent, was "mathed" correctly, and was retrieved by your javascript function correctly. – DACrosby Dec 05 '17 at 17:03
  • I assume you're making an ajax form submission function (not just a math function). You can assume, using your current setup, that `$_POST['index']` in the PHP file is indeed the index that you sent via ajax. So you can do something like `if ($_POST['index'] == 7) { echo "Form index#7 was submited- do related things now"; }`. [Something like this SO post](https://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php-without-page-refresh) – DACrosby Dec 05 '17 at 17:07
  • alright, but after i changed console.log(d); to $this.parent().submit(); what i thought on php page is the result is index, however, it's still 1. – Kevin Yang Dec 05 '17 at 17:16
  • Nothing you're doing in the JS file will ever affect the outcome if you view the PHP file directly [ technically you could [use cookies like this SO post](https://stackoverflow.com/a/5045117/1265817)]. Here you're using ajax and it works - just stop verifying it by viewing the PHP directly :) – DACrosby Dec 05 '17 at 20:41