Home  >  Article  >  Backend Development  >  How to send data to php with ajax

How to send data to php with ajax

王林
王林Original
2019-10-09 11:58:453052browse

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 processing
data:"name=Jhon&&location=Boston", this is the data we submitted, Jhon and Boston are the data we submitted
success:function(msg) {}, msg is the data returned after successful submission

How to write in the background to obtain these data:

<?php
//首先是获取到了数据
$username=$_POST[&#39;name&#39;];
$password=$_POST[&#39;location&#39;];
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:apache2 cannot parse phpNext article:apache2 cannot parse php