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

5
  • What does just print_r($POST); give you? Where exactly is the error? Commented Aug 7, 2016 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 Commented Aug 7, 2016 at 21:13
  • It gave me "Undefined index: postingData" and "Array ( )" as error #FrankerZ Commented Aug 7, 2016 at 21:13
  • @DushmanNalin, check if my answer works for you Commented Aug 7, 2016 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. Commented Aug 8, 2016 at 6:50

2 Answers 2

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...!  ');
   } 
 });
Sign up to request clarification or add additional context in comments.

5 Comments

It gave me same error..
have you tried changing the method to get? some times it works! @DushmanNalin
Yes, I also tried using get.. #abdollah zakeri.
if print_r($_POST) returns an empty array then nothing is being set..
you could try using .done instead of success $.ajax({ */ your params */ }).done(function(data){ alert(data); }).fail(function(){ alert('it failed')});
1

Edit the following in ajax .change name to name2

data: {postingData: name2},

1 Comment

it was a typing mistake, I have changed real variable name in to this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.