Ajax can pass data to the controller post and return.
JS Terminal code
$ (' #send-btn '). Click (function() {$username=$ (' input[name= ' username "] '); $content=$ (' textarea[id= ' content "] ') if(!$username. Val ()) {Alert (' Account cannot be empty! ‘); $username. focus (); }Else{ if(!$content. Val ()) {Alert (' Content cannot be empty! ‘); $content. focus (); }} $.post (Url,{username: $username. val (), Content: $content. val ()},function(data) {if(data.status) {alert (' Publishing Success '); $(' #close '). Click (); }Else{alert (' Publish failed '); } },' JSON '); })
Returns the address of the first parameter that executes the function method $.post, the second argument is the parameter to pass, the third is the method to return execution, and the fourth is the return type
Controller-side Code
$content=$_post[' Content ']; $data=Array( ' Username ' =>i (' username '), ' content ' =>i (' content '), ' time ' = Time(), ); if(M (' Wish ')->data ($data),Add ()) { $data[' Content ']=phiz_replace ($data[' Content ']); $data[' Time ']=Date(' Y-m-d h:i:s ',$data[' Time ']); $data[' Status ']=1; $this->ajaxreturn ($data, ' JSON '); }Else{ $this->ajaxreturn (Array(' status ' =>0), ' json ');
Ajaxreturn the first parameter to pass the information, can be an array, the second is a type, you can set different types by configuring thinkphp
/** * Expression Replacement*/ functionPhiz_replace ($content){ Preg_match_all('/\[.*?\]/is ',$content,$arr); if($arr[0]){ $phiz=f (' Phiz ', ', './public/')); foreach($arr[0] as $value) foreach($phiz as $k=$v){ if($value==$v){ $content=Str_replace($v, ' $k.‘. GIF "/>",$content); } } } return $content; }
Preg_match_alL Regular match, in the second parameter to find the first parameter similar to the content, stored in the third parameter, as an array of storage, the fourth parameter if it is aPREG_PATTERN_ORDER,则$arr[0]保所
以匹配到的结果,$arr[1]保持匹配到的第一个字组,$arr[2]保持第二个,以此类推,如果第四个参数是PREG_SET_ORDER,则$arr[0]保持第一个,$arr[1]保持第二个,以此类推。
The F method is the buffer method of the thinkphp, the first parameter is the file name, the second parameter is empty is to read the data, not empty is the content to be deposited, './public ' is the public file under the root directory, that is, the contents of the project name,
str_replace: Change the contents of the third argument to the same as the first argument to a second
Ajax asynchronous return under thinkphp