0

The title of this question is probably misleading, I didn't know a good way to consicely ask what I need help with.

So basically, I use the following ajax to send a javacript variable on index.php to a separate php file (page2.php)

var Variable1 = '1';
$.ajax({
        type: "POST",
        url: 'page2.php',
        data: "NewVariable=" + Variable1,
        success: function() {
              ...Save or echo the value of $newervariable here ....
        }
});

So basically above I am sending the variable Variabl1 to page2.php . Page2.php looks something like this:

<?php
     if(isset($_POST['NewVariable'])) {
          $NewVariable = $_POST['NewVariable'];
          $NewerVariable = $NewVariable + 1;
}

?>

I know the example is kind of obsolete because you could just do add 1 using javascript without having the 2nd php page, but I just simplified it down really it boils down to me needing the 2nd php page, and also knowing how to save the values of it after success (if it's even possible)

Jason Hunn
  • 41
  • 1
  • 1
  • 9

2 Answers2

0

On JS

success: function(data) {
          Variable1=data;
        }

On PHP:

<?php
     if(isset($_POST['NewVariable'])) {
          $NewVariable = $_POST['NewVariable'];
          $NewerVariable = $NewVariable + 1;
          echo $NewerVariable;
}
Man Programmer
  • 5,300
  • 2
  • 21
  • 21
0

In php page echo the output :

<?php
 if(isset($_POST['NewVariable'])) {
    $NewVariable = $_POST['NewVariable'];
    $NewerVariable = $NewVariable + 1;
    echo $NewerVariable; 
}
?>

And in your js :

var Variable1 = '1';
$.ajax({
    type: "POST",
    url: 'page2.php',
    data: "NewVariable=" + Variable1,
    success: function( data ) {
       // data --> data you echo from server side
         alert( data );
       // do whatever you want here
    }
});
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40