1

I need to pass a jquery variable from one PHP file to another.

for this, I am posting the variable into the server using ajax in page1.php and getting the variable from the server using PHP in page2.php

In page1.php,

$.ajax({
      url: "page2.php",
      type: "POST",
      data:{"myDat":activeCount}
}).done(function(data) {
      console.log(data);
});

In page2.php,

<?php 
     $data1 = isset($_REQUEST['myDat'])?$_REQUEST['myDat']:"";
     echo $data1;
?>

I am getting the ajax code (console.log(data)) get printed on the console.

But I am not getting the data in PHP (echo $data1)

Can anyone please help?

2
  • use $_POST, not $_REQUEST. and var_dump($_POST); exit on your PHP script so you can check data is being received ok Commented May 25, 2018 at 11:16
  • 1
    Since console.log(data) shows the page2 response, the echo $data1 clearly must have gotten through. Try to debug a bit further / and reexplain what issue you see. Commented May 25, 2018 at 11:17

2 Answers 2

1

I think you want session with that.

Update page2.php. Try:

<?php
    session_start();
    $_SESSION['myDat'] = isset($_SESSION['myDat']) ? $_SESSION['myDat'] : "";
    $_SESSION['myDat'] = isset($_POST['myDat'])?$_POST['myDat']:$_SESSION['myDat'];
    echo $_SESSION['myDat'];
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Please in page2.php page

try to print

 $data1 = isset($_POST['myDat'])? $_POST['myDat']: "";

 echo $data1;

it will help you .... :)

4 Comments

I have also tried, <?php $data1 = $_POST['myDat']; echo $data1; ?> Still, its not working
so you have tried to print out $_POST array still not getting any result than it might be problem with your ajax call please open up console and > go to network and try to make ajax call and check it will post myDat varible or not
Hi. I can get the variable "myDat" printed in the console in page1.php. But I cannot receive it in page2.php using the php code
When I load page2.php, it doesn't display the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.