0

我有两页,一页是 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>";
}
}
4

1 回答 1

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 );
    }
}
于 2012-11-21T09:54:18.247 回答