ajax form submission operation in thinkPHP5

不言
Release: 2023-03-30 14:06:01
Original
3284 people have browsed it

This article mainly introduces the thinkPHP5 ajax form submission operation, and analyzes the ajax submission form operation skills of thinkPHP5 in the form of examples, and also explains the parameter transfer and processing skills in form submission. Friends who need it can refer to it

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(&#39;Index/index/test&#39;)}",
          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>
Copy after login

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

The following is a random method for returning json data

public function test($mess,$id){
    if($mess == &#39;123&#39;){
      return json("ajax成功!".$mess."---".$id);
    }else{
      return json("你输出的是其他值:".$mess."---".$id);
    }
}
Copy after login

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"/>
Copy after login

Obtain

$project_name=input("post.project_name");
Copy after login

in the controller and jump to the passing parameters in php:

$this->success(&#39;新增项目成功&#39;,url("Version/index",array(&#39;project_name&#39;=>$project_name)));
die;
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of form token errors and solutions under ThinkPHP

Using ajax to achieve zero in Thinkphp Method of refreshing paging

ThinkPHP and Ajax implement secondary linkage drop-down menu

The above is the detailed content of ajax form submission operation in thinkPHP5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template