How to send data from JavaScript to PHP and vice versa?

How can I switch to data transfer from JavaScript code to PHP and vice versa. Right now, I am doing this the round way:
PHP for JavaScript: I would just use the built-in echo to send data:

<script type="text/javascript"> var data = <? echo $target_variable ?> </script> 

OR

 <script type="text/javascript"> var data = <?= $target_variable ?> </script> 

JavaScript for PHP: I would create a form element in JavaScript that would pass me data to a php file:

 <script type="text/javascript"> var data = targetData; document.write(" <form method=\"post\"> <input type=\"hidden\" value=\""+target_value+\""></input> </form> "); </script> </script> 

Are there any better ways to do this? Best practice.

+4
source share
6 answers

If you want to submit this data through a form, you do not need to create the form using Javascript. Just create an invisible form using HTML, fill in the hidden Javascript field and automatically submit when you are ready.

  <form method="post" action="process.php"> <input type="hidden" name="data" id="data" /> </form> document.getElementById("data").value = "foo"; 

If you want to send this in ajax style, I would suggest implementing jQuery, which makes it extremely simple. Note the previous case converted to a jQuery solution:

 $.post("process.php", {data:"foo"}, function(results){ // the output of the response is now handled via a variable call 'results' alert(results); }); 
+5
source

The following code shows an ajax request that presents a form on your page in the background using jquery ajax. Hope this makes sense.

 <html> <head><title>Sample</title></head> <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> <script language="javascript"> function submitMe() { variableString = 'name='+$("#name").val()+'&location='+$("#location").val(); jQuery.ajax({ type: "POST", url: "http://google.com/some.php", data: variableString, success: function(msg){ alert( "Data Saved: " + msg ); } }); } </script> <body> <form id="xyzForm"> <input type="text" id="name" name="name"/> <input type="text" id="location" name="location"/> <input type="button" value="submit" onClick="submitMe();"/> </form> </body> </html> 
+3
source

First, you need to understand that php and javascript work on different computers.

Php runs on the server, while javascript runs on the client computer. This makes things a little undifferentiated. I mean, php and javascript cannot directly modify variables because they do not run at the same time. The first php runs on the server, prepares HTML and js and passes it to the client, and then on the computer javascript runs on the computer, if javascript needs something from the server, it calls the server again (either post, get, or ajax does not matter, this is still an http request).

So, from php to javascript you can pass a variable when preparing javascript, in which pont only runs php and no javascript, and from javascript to php you can pass a variable by making a request from the client computer to your php server with some variables.

+3
source

You can use GET or POST to transfer data from one to another.

+2
source

The best thing you can do with a couple of PHP and JavaScript is to start using the json_encode () and json_decode () functions in PHP.

This will allow you to receive data in JSON, which is a JavaScript Object Designator that allows you to transfer objects between two languages. You can do eval as soon as you get it in JS to get the object.

Actually moving the data should depend on what you are doing exactly, but many times I use Ajax, and it would be nice to start the search.

+1
source

Well, if you want asynchronous interaction, you can use XMLHTTPRequest to execute the async POST request to the server, to send data.

0
source

All Articles