Home >Backend Development >PHP Problem >How to send data to php with ajax
I have been reading online for a long time and found that it is actually very simple to use ajax to submit data to the backend, but many of the explanations are not clear. For beginners, many of them are really about reading. I'm a little confused and want to use it directly, but I also want to understand what's going on. In fact, it is very simple to use ajax to submit data to the backend.
$.ajax({ type: "POST", url: "register.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } });
First of all, we interpret the above string of code. Of course, jQuery is required to use ajax.
type: "POST"
, is the type of submission url: "register.PHP"
, is the direction of submission, I submitted it to register. php for processingdata:"name=Jhon&&location=Boston"
, this is the data we submitted, Jhon and Boston are the data we submittedsuccess:function(msg) {}
, msg is the data returned after successful submission
How to write in the background to obtain these data:
<?php //首先是获取到了数据 $username=$_POST['name']; $password=$_POST['location']; echo $password; ?>
What we clearly see is $_POST["name"] ;Is to get the Jhon
$_POST['location'] is to get the Boston
The data returned by our background, that is, the data from the echo, that is Boston.
Recommended tutorial: PHP video tutorial
The above is the detailed content of How to send data to php with ajax. For more information, please follow other related articles on the PHP Chinese website!