1

I want to send a JavaScript variable Data into another php file for a php function. I realized we are unable to use PHP Sessions for that. I tried to do it using AJAX (using jQuery), but I am unable to initialize the AJAX request in to a php variable. However AJAX give me a correct Response:(means ajax is working). but when i print the request [print_r($_POST)], it gave me zero result.

Here is the main html , javascript part.

<div id ='postData'>"+ PostDataResponse + "</div>  // PostDataResponse is also ajax response, it is working correctly.
<a href='server.php' onclick= saveData()> click_Here_Link </a>

When I click the click_Here_Link link I wont send the data to requestFile.php file as session variable. I developed the ajax post method in client side like this.(It give me a correct response.)

  function  saveData(){

     // $.post('/ server.php', { postingData: name});

     var name = document.getElementById("postData").innerHTML.trim();

   $.ajax ({
   type: 'POST',
   url: 'server.php',
   dataType: 'text',
   data: {postingData: name},

  success: function (Response) {
     alert(Response);
  },
   error: function () {
   alert('failure...!  ');
   } 
 });
 }

Here is the server.php code.

<?Php
  $boat = $_POST['postingData'];
  echo $boat;
  print_r($_POST); 
?>

when open the server.php file it give me a error.

Undefined index: postingData

Community
  • 1
  • 1
Dusman
  • 122
  • 4
  • 11
  • What does just `print_r($POST);` give you? Where exactly is the error? – Blue Aug 07 '16 at 21:09
  • are you opening server.php in your browser? you need to look under your browsers dev tools and view the network tools tab then fire your ajax and view the response to the post if(isset($_POST['postingData'])){ /* do something post not set*/ } will check if $_POST['postingData'] is set – futureweb Aug 07 '16 at 21:13
  • It gave me "Undefined index: postingData" and "Array ( )" as error #FrankerZ – Dusman Aug 07 '16 at 21:13
  • @DushmanNalin, check if my answer works for you – coder Aug 07 '16 at 21:22
  • @futureweb, ajax code is working correctly. it gave me a correct response. I wont initialize ajax request into php variable,(in server.php ) like session variable. – Dusman Aug 08 '16 at 06:50

2 Answers2

1

replace your ajax with this :

 $.ajax ({
   method:'POST',
   url: 'server.php',
   data:"postingData="+$('#postData').getAttribute('name'),
  success: function (Response) {
     alert(Response);
  },
   error: function () {
   alert('failure...!  ');
   } 
 });
abdollah zakeri
  • 364
  • 3
  • 11
1

Edit the following in ajax .change name to name2

data: {postingData: name2},
coder
  • 906
  • 1
  • 12
  • 19