简体   繁体   English

返回成功和错误的Ajax成功发布函数

[英]Ajax post success function returning both success and error

Okay so I am trying to get ajax to post to my php file, lookup a mysql field and if it exists echo 'clientsuccess' otherwise echo 'Client already exists' but on success function it returns both values despite the fact that they're in an php if statement. 好吧,所以我试图让ajax发布到我的php文件中,查找一个mysql字段,如果存在,则回显'clientsuccess',否则回显'Client已经存在',但是在成功函数中,尽管它们位于一个php if语句。 I am quite possibly missing something incredibly simply, but any help is greatly appreciated. 我很可能会简单地丢失一些东西,但是任何帮助都将不胜感激。

PHP: PHP:

    <?php 
session_start();
$clientArray = $_POST['clientArray'];

$clientArray = explode(',', $clientArray);
$count = 0;


foreach($clientArray as $clientField) 
{

trim($clientField);
if(empty($clientField)) {
$clientField = '-'; 
}

}




$con = mysql_connect("localhost",$_SESSION['MysqlUser'],$_SESSION['MysqlPass']);
if (!$con)
{
die('Could not connect with '.$_SESSION['MysqlUser'].mysql_error());
}

mysql_select_db("smeinsti_SPW_Inventory", $con);

$checkclient = mysql_query("SELECT ClientName FROM Clients WHERE ClientName = '".$clientArray[0]."'", $con);

if(mysql_num_rows($checkclient)==0)
{

$sql="INSERT INTO Clients (`ClientName`, `PhoneNumber`, `Email`, `Address`, `OrderDate`)
VALUES
('$clientArray[0]', '$clientArray[1]', '$clientArray[2]', '$clientArray[3]', CURDATE())";


$clientArray[0] = $_SESSION['ClientName'];
echo "clientsuccess";



} else {
echo 'Client already exists';   
}


?>

JS: JS:

    function NextPage()
{
var ClientData = [];

$('form#order-form.create input[type=text]').each(function() {

ClientData += $(this).val() + ',';

})

alert(ClientData);

var parameters = {
clientArray: ClientData
};

$.ajax({
type: "POST",
async:false,
url: "write_client.php",
data: parameters,
success: function(result){       
var res=result;
if(res = 'clientsuccess') {
window.location = 'admin.php?id=7';
} else {
alert('Client already exists');
}
}
}); 







}

Your condition Equal symbol is not correct! 您的情况“等号”不正确! Put '==' 放'=='

    if(res == 'clientsuccess') { //Double Equal to
    window.location = 'admin.php?id=7';
    } else {
    alert('Client already exists');
    }

mysql_num_rows returns the number of selected rows and not the fields of a certain row. mysql_num_rows返回选择的行数,而不是某一行的字段。 Use mysql_fetch_row to fetch the row you have selected with your query: 使用mysql_fetch_row来获取您在查询中选择的行:

You could also use mysql_result to fetch a row and get a certain field: 您也可以使用mysql_result来获取一行并获取某个字段:

$client exist = mysql_result($checkclient, 0, 0); $ client存在= mysql_result($ checkclient,0,0); This fetches the first row (zero based) and returns the first field (zero based). 这将获取第一行(从零开始)并返回第一字段(从零开始)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM