您可以使用此代码,
<form action="../handler/AjaxHelper.php" method="POST">
</form>
$(document).ready(function() {
$('form').submit(function() {
$.ajax({
type: this.method,
url: this.action,
data: $(this).serialize(),
success: function(data)
{
var result = $.parseJSON(data);
if (result["messageCode"] == 'success')
{
alert(result["message"]);
}
else
{
alert(result["message"])
}
},
error: function()
{
alert("Please Try Again");
}
});
return false;
});
});
在AjaxHelper.php 中
$objLoginHelper = new LoginHelper();
$objLoginHelper = unserialize($_SESSION["LoginInformation"]);
$postDate = date("Y-m-d H:i:s", strtotime($_POST['txtTopicDate']));
$dataTopics = array($_POST['txtTopicSubject'], $postDate, $_POST['ddlCategories'], $objLoginHelper->getUserLoginId());
$result = array();
try {
$rp = new Repository();
$rp->SaveForumTopics($dataTopics);
$result["messageCode"] = "success";
$result["message"] = "Category Save Successfully";
} catch (Exception $ex) {
$result["messageCode"] = "error";
$result["message"] = $ex->getMessage();
}
echo json_encode($result);