我已经验证了我的表单。我想弹出验证消息,“信息有一些错误,请插入正确的信息”。但它出现了2弹出框,“信息有一些错误,请插入正确的信息。”并且“谢谢你!你的订单已被下订单!”。我可以知道这是什么问题吗?
$fnameErr=$lnameErr=$add1Err=$add2Err=$postErr=$mobileErr=$creditErr=$pinErr ="";
$credit=$pin="";
if($_REQUEST['command']=='update')
{
$date = date('Y-m-d');
$time = date("H:i:s");
$charge = $_REQUEST['ocharge'];
$fname = $_REQUEST['ofname'];
$lname = $_REQUEST['olname'];
$mobile = $_REQUEST['omobile'];
$add1 = $_REQUEST['oadd1'];
$add2 = $_REQUEST['oadd2'];
$postcode = $_REQUEST['opostcode'];
$state = $_REQUEST['ostate'];
$country = $_REQUEST['ocountry'];
$credit = $_REQUEST['ocredit'];
$pin = $_REQUEST['opin'];
$city = $_REQUEST['ocity'];
if($fname==""||$lname==""||$mobile==""||$add1 ==""||$add2 ==""||$postcode==""||$state ==""||$country==""||$credit==""||$pin==""||$city=="")
{
?>
<script type="text/javascript">
alert("Please fill in all the required informations.");
</script>
<?php
}
else
{
$sql=" INSERT INTO `order` (Order_Date,Order_Time,Delivery_Charge,
Delivery_Fname,Delivery_Lname,Delivery_HP,Delivery_Street1,
Delivery_Street2,Delivery_Postcode,Delivery_State,
Delivery_Country,Credit_No,Pin_No,Delivery_City,Order_Status)
VALUES
('$date','$time','$charge','$fname','$lname','$mobile',
'$add1','$add2','$postcode','$state','$country',
'$credit','$pin','$city','Pending')";
$result=mysql_query($sql);
if($result === FALSE)
{
die("Query Failed!".mysql_error().$result);
}
$orderid=mysql_insert_id();
if (empty($errors) === true)
{
//fname xx
if (!preg_match("/^[A-Z][a-zA-Z -]+$/i", $_REQUEST['ofname']))
{
$fnameErr= 'Your first name cannot contain with any symbol and number';
}
//lname xx
if (!preg_match("/^[A-Z][a-zA-Z -]+$/i",$_REQUEST['olname']) )
{
$lnameErr= 'Your last name cannot contain with any symbol and number';
}
//add1 xx
if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd1']))
{
$add1Err = 'Address 1 must be only letters, numbers or one of the following';
}
//add2 xx
if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd2']))
{
$add2Err= 'Address 2 must be only letters, numbers or one of the following';
}
//postcode xx
if (!preg_match("/^\d{5}$/i", $_REQUEST['opostcode']))
{
$postErr = 'Postcode must be 5 digits';
}
//mobile xx
if (!preg_match("/^\d{3}-\d{7}$/i", $_REQUEST['omobile']))
{
$mobileErr= 'Phone must comply with this mask: 010-1111111 or 0111-1111111';
}
//credit card xx
if (!preg_match("/^\d{4}-\d{4}-\d{4}-\d{4}$/i", $_REQUEST['ocredit']))
{
$creditErr= 'Credit Card must comply with this mask: 0000-1111-2222-3333';
}
//pin xx
if (!preg_match("/^\d{6}$/i", $_REQUEST['opin']))
{
$pinErr = 'Pin must be 6 digits';
}
?>
<script type="text/javascript">
alert("Information has some error,please insert the proper information.");
</script>
<?php
}
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++)
{
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$price=get_price($pid);
$insert_query="INSERT INTO order_detail (Order_ID,Product_ID,Order_Quantity,Sub_Total) VALUES ('$orderid','$pid','$q','$price')";
if(mysql_query($insert_query))
{
echo"<script>alert('Thank You! your order has been placed!')</script>";
}
}
}
}
?>
答案 0 :(得分:0)
您的代码似乎没有正确的结构。您正在检查各个字段是否有错误,然后显示错误消息,无论是否存在任何错误。之后,无论错误检查发生了什么,您都将继续保存信息并显示其他警报。
不要过多地考虑代码的细节,构建它的一种方法是:
$errors = array();
//fname xx
if (!preg_match("/^[A-Z][a-zA-Z -]+$/i", $_REQUEST['ofname']))
{
$fnameErr= 'Your first name cannot contain with any symbol and number';
array_push($errors, $fnameErr);
}
//lname xx
if (!preg_match("/^[A-Z][a-zA-Z -]+$/i",$_REQUEST['olname']) )
{
$lnameErr= 'Your last name cannot contain with any symbol and number';
array_push($errors, $lnameErr);
}
//add1 xx
if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd1']))
{
$add1Err = 'Address 1 must be only letters, numbers or one of the following';
array_push($errors, $add1Err);
}
//add2 xx
if (!preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/i",$_REQUEST['oadd2']))
{
$add2Err= 'Address 2 must be only letters, numbers or one of the following';
array_push($errors, $add2Err);
}
//postcode xx
if (!preg_match("/^\d{5}$/i", $_REQUEST['opostcode']))
{
$postErr = 'Postcode must be 5 digits';
array_push($errors, $postErr);
}
//mobile xx
if (!preg_match("/^\d{3}-\d{7}$/i", $_REQUEST['omobile']))
{
$mobileErr= 'Phone must comply with this mask: 010-1111111 or 0111-1111111';
array_push($errors, $mobileErr);
}
//credit card xx
if (!preg_match("/^\d{4}-\d{4}-\d{4}-\d{4}$/i", $_REQUEST['ocredit']))
{
$creditErr= 'Credit Card must comply with this mask: 0000-1111-2222-3333';
array_push($errors, $creditErr);
}
//pin xx
if (!preg_match("/^\d{6}$/i", $_REQUEST['opin']))
{
$pinErr = 'Pin must be 6 digits';
array_push($errors, $pinErr);
}
if(count($errors) > 0) {
?>
<script type="text/javascript">
alert("Information has some error,please insert the proper information.");
</script>
<?php
}
else {
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++)
{
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$price=get_price($pid);
$insert_query="INSERT INTO order_detail (Order_ID,Product_ID,Order_Quantity,Sub_Total) VALUES ('$orderid','$pid','$q','$price')";
if(mysql_query($insert_query))
{
echo"<script>alert('Thank You! your order has been placed!')</script>";
}
}
}
?>
最后,请注意,mysql_
系列函数已弃用,您应尽快停止使用它们。使用PDO代替至少mysqli_
个函数。