php ajax处理表单后调用js,php - 使用AJAX / JQuery提交表单并显示结果而无需重新加载 - 堆栈内存溢出...

我试图使用jquery和ajax提交表单并显示结果而无需重新加载。 像典型的ajax评论设置一样。

我的HTML设置如下:

Heading:

JS:

/* attach a submit handler to the form */

$("#create_new_heading").submit(function(event) {

/* stop form from submitting normally */

event.preventDefault();

/* get some values from elements on the page: */

var $form = $( this ),

term = $form.find( 'input[name="heading"]' ).val(),

url = $form.attr( 'action' );

/* Send the data using post and put the results in a div */

$.post( url, { s: term },

function( data ) {

var content = $( data ).find( '#test_me' );

$( "#result" ).empty().append( content );

}

);

});

表单处理器如下所示:

public function write($p) {

if ( $_POST['type'] )

$type = mysql_real_escape_string($_POST['type']);

if ( $_POST['heading'])

$heading = mysql_real_escape_string($_POST['heading']);

if ( $type && $heading ) {

$uniqueid = uniqid();

$sql = "INSERT INTO headings VALUES('$type','$heading','$uniqueid')";

return mysql_query($sql);

} else {

return false;

}

}

我试图遵循jquery文档来实现此功能,但似乎无法正常工作。 表单提交,条目被放入数据库,但是我仍然必须刷新页面才能看到新条目。 任何我做错事的想法吗?