Thinkphp,jquery,ajax Asynchronous Publishing

Source: Internet
Author: User

1. Define a variable in the
<script type= ' Text/javascript ' >
var handleurl= ' <{:u ("Index/index/handle", "" "," ")}> ';
Create a Handleurl variable for use with Index.js (this is the jquery file that was introduced), because the template syntax is not available in the referenced JS file <{}> to get the address of the submitted data, So here is the definition of a variable to get the address of the method that JS needs to submit to PHP to process the data, note that you must write the full "application name/Controller name/Method name" path in the U method, the following two parameters are null to indicate that the pseudo-static suffix is empty, so that the resulting address is correct

2, in the introduction of the JS file using $.post () for Ajax asynchronous processing

Click the button to submit
$ (' #send-btn '). Click (function () {
Determine the user name, whether the content is empty
var username=$ (' input[name=username] ');
var content=$ (' textarea[name=content] ');
if (username.val () = = ") {
Alert (' User name cannot be null ');
Username.focus ();
Return The function of return is to jump out of the
if (content.val () = = ") {
Alert (' content cannot be null ');
Content.focus ();
Ajax asynchronously publishes, using $.post (); The first parameter is the way the PHP side receives the content of the asynchronous publication, the second parameter is to send past data, use the JSON format, and the third parameter is a callback function to process the data returned by PHP The fourth parameter specifies the format to receive the PHP return data, usually in JSON format
$.post (handleurl,{"username": Username.val (), "content": Content.val ()},function (data) {//data is the array name returned by PHP
if (data.status) {//Here begins processing the data returned by PHP; the status in the data array returned by PHP determines whether the PHP-side operation is successful
var str= ' <dl class= ' paper A1 ' > ';
str+= ' <dt><span class= "username" > ' +data.username+ ' </span> ';
str+= ' <span class= "num" >no. ' ' </span> ';
str+= ' </dt><dd class= "content" > ' +data.content+ ' </dd> ';
str+= ' <dd class= "bottom" > ";
str+= ' <span class= "Time" > ' +data.time+ ' </span> ';
str+= ' <a href= "" class= "Close" ></a></dd></dl> ";
$ ("#main"). Append (str);
$ ("#close"). Click ();
}else {
Alert ("Failed to publish");
}, ' json ')

3. PHP handles data sent by jquery via Ajax

Asynchronous release processing
Public function handle () {//This method is Handleurl= ' <{:u ("Index/index/handle", "", "")}> ';
if (!is_ajax) {//determines whether asynchronous processing via AJAX prevents direct access to the browser input path
Halt (' page does not exist '); Halt () Output error page, configuration file is configured with error page path, or you can use _404 ()
Start processing the data that jquery sends over
//jquery is submitted asynchronously using $.post, so the data sent by jquery is received with $_post.
$data =array ("username" =>$_post[' username '], "content" =>htmlspecialchars ($_post[' content '), "time" = Time ()); Note that the content is filtered using htmlspecialchars () to prevent JS from running
if ($id =m (' Wish ')->data ($data)->add ()) {//Insert data successfully then fetch data back to page
$data [' id ']= $id;
$data [' Content ']=replace_phiz ($data [' content ']); Call a custom function to replace the expression in the published content with the available string form
$data [' Time ']=date (' y-m-d h:i ', $data [' time ']); Replace release date with formatted date
$data [' Status ']=1; Returns a status code that indicates the success of inserting the database; the jquery side will use this to determine if the operation is successful.
$this->ajaxreturn ($data, ' json ');//Ajaxreturn () that calls TP returns data; The first argument is an array; the second parameter specifies the form of the returned data, usually in JSON format. will automatically convert that array to JSON format
$this->ajaxreturn (Array (' status ' =>0), ' json '); Failed to insert the database returns status 0

Note: The action in form form does not have to write the address of the submission background, leave blank on the line, because the data are sent through jquery, and even without form form, as long as there is input data, jquery can get data through the name value of input.

Thinkphp,jquery,ajax Asynchronous Publishing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.