Home  >  Article  >  Web Front-end  >  Ajax submit form and receive json method

Ajax submit form and receive json method

小云云
小云云Original
2017-12-19 14:09:031872browse

After clicking the button, the data is submitted to the server in the form of a form and the return data from the server is received. The page does not refresh during the process. This article mainly introduces the example code of Ajax submitting a form and receiving json. It is very good and has reference value. Friends in need can refer to it. I hope it can help everyone.

html code


<html xmlns="http://www.w3.org/1999/xhtml">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
 <script src="./testajaxjs.js"></script>
 <head>
 </head>
 <body>
  <form id="form1">
   <p>xingming:<input type="text" name="xingming"/></p>
   <p>nianling:<input type="text" name="nianling"/></p>
  </form>
  <button type="button" id="mybt" onclick="mysubmmit()">
   ajax提交
  </button>
 </body>
</html>

js code


function mysubmmit(){
 $.ajax({
  type: "POST",
  url: "testajaxend.php",
  data: $(&#39;#form1&#39;).serialize(),
  async: false,
  success: function(databack){
   //console.log("chenggong");
   console.log(databack);
  },
  error: function(request){
   console.log("shibaile");
  }
 });
}

Backend php code


<?php
  $name = $_POST[&#39;xingming&#39;];
  $age = $_POST[&#39;nianling&#39;];
  $myarray = array("name"=>$name, "age"=>$age);
  $myjson = json_encode($myarray);
  echo $myjson;
?>

Related recommendations:

Two methods for jquery to implement ajax submission form

Ajax technology-ajax submission Form jquery ajax tutorial js ajax

php HTML no refresh submit form form form submit ajax submit form js submit form

The above is the detailed content of Ajax submit form and receive json method. 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