我有两个页面,一个页面是HTML,用于通过旧方式AJAX向第二页请求数据。
在这里,我向数据库添加一条新记录,并按如下方式返回:
var HttPRequest = false;
function doCallAjax(Mode, Page, ID) {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'AjaxItemsGroupsRecord.php';
var pmeters = "titems_groups_GroupName=" + encodeURI(document.getElementById("items_groups_GroupName").value) +
"&titems_groups_sys_type_ID=" + encodeURI(document.getElementById("items_groups_sys_type_ID").value) +
'&myPage=' + Page +
"&tID=" + ID +
"&tMode=" + Mode;
HttPRequest.open('POST', url, true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function () {
if (HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Loading";
}
if (HttPRequest.readyState == 4) // Return Request
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
如果我插入错误,我需要在其他div上返回按摩错误,我只需要显示错误信息,怎么办呢
我试过但它还没有用
这是我的PHP代码:
if($strMode == "ADD")
{
$sql_setting="SELECT * FROM stores_items";
$sr=mysql_query($sql_setting);
$S=@mysql_fetch_array($sr);
$stores_items_Name=$S["stores_items_Name"];
if($_POST["tstores_items_Name"]==$stores_items_Name){
echo $msg="<div class='gadget err_or'><div class='error_tit'>
<a rel='hide_err' class='hideerr' href='#'><img width='19' height='33' alt='picture' src='../images/spacer.gif'></a>
<a rel='close_err' class='closeerr' href='#'><img width='19' height='33' alt='picture' src='../images/spacer.gif'></a>
<h3>Message</h3>
</div>
<div class='error_msg'><p>its founded</p></div>
</div>";
}
elseif($_POST["tstores_items_Name"]=="" or $_POST["tstores_items_SalePrice_By_Store"]=="" or $_POST["tstores_items_BuyPrice_By_Store"]==""){
echo $msg="<div class='gadget err_or'><div class='error_tit'>
<a rel='hide_err' class='hideerr' href='#'><img width='19' height='33' alt='picture' src='../images/spacer.gif'></a>
<a rel='close_err' class='closeerr' href='#'><img width='19' height='33' alt='picture' src='../images/spacer.gif'></a>
<h3>Message</h3>
</div>
<div class='error_msg'><p>(*) Fill </p></div></div>";
}
else
{
$stores_items_AddDate=date('Y/m/d');
$strSQL = "INSERT INTO stores_items ";
$strSQL .="(stores_items_ID,stores_items_Name,stores_items_GroupId,stores_items_Barcode,stores_items_SalePrice_By_Store,stores_items_BuyPrice_By_Store,stores_items_Unit,stores_items_Currency,stores_items_Type,stores_items_AddType,stores_items_AddUser,stores_items_AddDate,stores_items_Delete,stores_items_StoreId,stores_items_sys_type_ID) ";
$strSQL .="VALUES ";
$strSQL .="('' , '".clean($_POST["tstores_items_Name"])."' , '".clean($_POST["tstores_items_GroupId"])."' , '".clean($_POST["tstores_items_Barcode"])."' , '".clean($_POST["tstores_items_SalePrice_By_Store"])."' ,'".clean($_POST["tstores_items_BuyPrice_By_Store"])."','".clean($_POST["tstores_items_Unit"])."','".clean($_POST["tstores_items_Currency"])."','".clean($_POST["tstores_items_Type"])."','1','$ausers_ID','$stores_items_AddDate' , '0','$ausers_StoreId','".clean($_POST["tstores_items_sys_type_ID"])."') ";
$objQuery = mysql_query($strSQL);
echo $msg="<div class='gadget err_bl'><div class='error_tit'>
<a rel='hide_err' class='hideerr' href='#'><img width='19' height='33' alt='picture' src='../images/spacer.gif'></a>
<a rel='close_err' class='closeerr' href='#'><img width='19' height='33' alt='picture' src='../images/spacer.gif'></a>
<h3>Message</h3>
</div>
<div class='error_msg'><p>Done</p></div></div>";
}
}
答案 0 :(得分:0)
返回复杂结果的最佳方法是在PHP端输出JSON编码数组:
$output = array('result' => false, 'message' => '');
// do your insert, then set $output['result'] = true if successful
// also set the message as required in $output['message'] = 'Success!'
echo json_encode($output);
使用它,你可以返回一个布尔值来说明它是否成功,还有一条消息。
在Javascript端,您可以解码JSON并检查结果:
if(HttPRequest.readyState == 4) // Return Request
{
var obj = JSON.parse(HttPRequest.responseText);
if(obj.result)
{
// success, do something or use obj.message
console.log( "success: " + obj.message );
}
else
{
// fail -do something
console.log( "fail: " + obj.message );
}
}