0

After trying really hard for the last 10 hours or more, I have not been able to resolve the issue. I hope someone can help me here.

In the javascript code on page1.php, I am making two AJAX calls to page2.php,one after the end of the other.

My first AJAX call on page1.php looks like this:

          $.ajax({
          type: "POST",
          url: "page2.php",
          data: { payorder: payOrdNum }, 
          dataType: 'text',                     
          success: function(msg){
            callback(msg);
          }
        })

On success of this AJAX call, I get an array that I pass to a function, callback(msg), as shown below. I retrieve values from the array and make my second AJAX call from inside this function:

        function callback(inputmessage) {
          ...
          ... code to retrieve array values ...
              var pamount = 55.00;
          ...
        
          //Now my second AJAX call
          $.ajax({
            type: "POST",
            url: "page2.php",
            data: {orderamt:pamount}   //pamount is one of the array values from the first call
            dataType: 'text',                
            success: function(data){
            }
          });
        }

Here is the problem I am having, When I try to assign the value posted for 'orderamt' in my second AJAX call, to a php variable in page1.php, as below:

       <?php
           $x_amount = $_POST['orderamt'];
       ?>

I get the error "Notice: Undefined index" error in my PHP script on page1.php. How can I retrieve the value for 'orderamt' from second AJAX call in the PHP script on page1.php?

Best regards

user765081
  • 207
  • 2
  • 7
  • 18
  • You cannot assign PHP variables from within JavaScript. Once the JavaScript (client) is running, the PHP (server) is done processing already, it doesn't do anything else after that. When using Ajax, you are actually making a completely new HTTP call to a PHP script, which cannot interact with the PHP that has already loaded on the page you are using Ajax on, because it is _a completely separate request_. – GrumpyCrouton Sep 18 '20 at 19:30
  • Instead, why no show us what this variable is actually for? How does it need to be used? We may be able to help you figure out how to solve your actual problem, instead of the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) you have presented us. – GrumpyCrouton Sep 18 '20 at 19:34
  • Thank you for your response. My actual problem is, I want to assign one of the array values from completion of the first AJAX call to a PHP variable on the same page. Since the array value is inside Javascript, I am not able to assign it to a PHP variable on the same page. How can I do that if that is possible? So I thought of making a second AJAX call to POST this array value and then retrieve it in the PHP section. I hope I am making sense. – user765081 Sep 18 '20 at 19:45
  • PHP cannot interact with anything JavaScript does. You'll have to create the variable you need before or during execution of your PHP code. Or, you could run both of your AJAX calls and utilize PHP Sessions (in the PHP script called by ajax) to create the variable you need, then reload the page to access that session variable. This is not usually how ajax is used (usually it's used to prevent a page reload), but would work. – GrumpyCrouton Sep 18 '20 at 19:47

1 Answers1

1

I'm not sure I understand your problem, but here's a try. You are making two separate requests with different parameters each time, to the same page "page2.php". On your first request, you only use "payorder" as parameter in your POST request, and on the second request only "orderamt", so the simplest solution is to just check if the parameter is set by using the PHP-function isset() in your PHP-code. Something like this:

$x_payorder = isset($_POST['payorder']) ? $_POST['payorder'] : '';
$x_amount = isset($_POST['orderamt']) ? $_POST['orderamt'] : '';

Alternative:

if(isset($_POST['payorder'])){
   $x_payorder = $_POST['payorder'];
} else {
   $x_payorder = '';
} 

if(isset($_POST['orderamt'])){
   $x_amount = $_POST['orderamt'];
} else {
   $x_amount = '';  
} 

This should solve the "Notice: Undefined index" error you get.

But what I don't understand is why you are making a post request to "page2.php" in your callback function while you want the value in "page1.php"?

You have 2 options, either you:

  1. change the callback function and post to "page1.php" instead of "page2.php"
  2. or put the $x_amount = isset($_POST['orderamt']) ? $_POST['orderamt'] : ''; in "page2.php" instead of "page1.php"

Or am I missing something?

Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25
  • From the two options you suggested, option #1 is the right one for my needs. I need the 'orderamt' value in page1.php, because the php code segement where it will be used is on page1.php. So I added >> $x_amount = isset($_POST['orderamt']) ? $_POST['orderamt'] : ''; to page1.php. I then added the line echo $x_amount; I dont get any value. Any suggestion to try or check? Thanks very much! – user765081 Sep 19 '20 at 01:26
  • Also, do you think it is possible to post a value using AJAX, back to the same page where the AJAX call resides? In essence, that is what I am trying to achieve. But like I said in my previous comment, I am not showing any value when I echo the value retrieved from $_POST['orderamt'] – user765081 Sep 19 '20 at 01:36
  • Yes, you can perform a post request to the same page as you are making the ajax call from, but the php server side code must be handled separate from the client side code, and the echoed result on the server side must be picked up and handled within your AJAX success function in your client side code. You must give some more information about whats in your page1.php file in order to help you further. – Michael Krikorev Sep 20 '20 at 09:51
  • Sorry it took so long to get back with you. Your suggestion (option #1) got me into thinking differently. Earlier, my mind was locked into one single way and I wasn't considering possibilities. I moved a section of the php code from page2 to page1 and got my code to work. I therefore accept your answer coz giving you the credit. – user765081 Sep 27 '20 at 19:55