0

If I have data stored in JavaScript variables on my website and I want to take this data and send it to the server using PHP, how can I do this? A separate client will then get the data from the server to use. Does the server have an IP address that I need to send the data to? What code will be necessary to do this using PHP?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Nilim
  • 65
  • 1
  • 6
  • 1
    this is way too broad for SO. as a broad guide, (1) you would need to use ajax to send or build/send a form using javascript, (2) you would need to create the server side code to receive and do something with that data. – Sean Nov 22 '15 at 02:17
  • Thank you for your help but for some reason, ajax isn't working for me. – Nilim Nov 22 '15 at 08:06

2 Answers2

1

This is achieved using the XMLHttpRequest object in Javascript. However, since you're new this this I would recommend using jQuery.

This is basically an Ajax request and you will find much more documentation on the internet if you search for that.

http://api.jquery.com/jquery.ajax/ and http://api.jquery.com/jquery.post/

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
1

You won't be able to do it using PHP, because PHP is executed on the server only. However, you could use jQuery's ajax function if you want to send data from the client side:

$.ajax({
  type: "POST",
  url: "/process-data.php",
  data: {
    // your data (stored in JS variables on the client side)
  },
  success: function() {
    // what do to on success: redirect, maybe?
    window.location.href = "/process-data.php";
  }
});

Then, on /process-data.php, you could use the data:

<?php
  $myData = $_POST['myData'];
  // do something with $myData, or display it
?>
<p>You entered: <?php echo $myData; ?></p>
nathanhleung
  • 506
  • 6
  • 14