The example in this article describes the thinkPHP5 ajax form submission operation. Share it with everyone for your reference, the details are as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <script> function ajaxPost(){ var formData = $("#myform").serialize(); //serialize() 方法通过序列化表单值,创建 URL 编码文本字符串,这个是jquery提供的方法 $.ajax({ type:"post", url:"{:url('Index/index/test')}", data:formData,//这里data传递过去的是序列化以后的字符串 success:function(data){ $("#content").append(data);//获取成功以后输出返回值 } }); } </script> </head> <body> <form id="myform"><!--这里给表单起个id用于获取表单并序列化--> <input type="text" name="mess" /> <input type="text" name="id" /> <button onclick="ajaxPost()">---------</button> </form> <p id="content"> </p> </body> </html>
In fact, it is no different from normal ajax. It mainly uses jquery’s serialize()
method to serialize the form
In the middle I encountered some troubles because I am not familiar with the thinkphp5 framework. I have not had time to use 3.2.3 before. There are still some differences between the two versions.
One is that $_POST cannot be used, and the original ajax url can be used. The U method of the framework is gone. I looked specifically at the assistant function under think and found that it is url
Below is a random method written to return json data
public function test($mess,$id){ if($mess == '123'){ return json("ajax成功!".$mess."---".$id); }else{ return json("你输出的是其他值:".$mess."---".$id); } }
In addition, the regular parameter submission and The processing method is as follows:
Form submission and passing parameters:
<input type="hidden" name="project_name" value="$project_name"/>
Obtain
$project_name=input("post.project_name");
in the controller and jump to the passing parameters in php:
$this->success('新增项目成功',url("Version/index",array('project_name'=>$project_name))); die;
Related learning recommendations: thinkphp
The above is the detailed content of thinkPHP5 ajax form submission operation example analysis. For more information, please follow other related articles on the PHP Chinese website!