Suriya Karthikeyan
Suriya Karthikeyan

Reputation: 83

How to get the jquery variable from one php file into another?

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?

Upvotes: 1

Views: 90

Answers (2)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

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'];
?>

Upvotes: 1

Girish Ninama
Girish Ninama

Reputation: 593

Please in page2.php page

try to print

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

 echo $data1;

it will help you .... :)

Upvotes: 0

Related Questions