带有if语句的PHP弹出框
我必须验证我的表格。我想弹出一条验证消息,它是“信息有一些错误,请插入正确的信息。”但是它出现了2个弹出框,它是“信息有一些错误,请插入正确的信息。”和“谢谢!您的订单已经下了!”。我可以知道有什么问题吗带有if语句的PHP弹出框,php,Php,我必须验证我的表格。我想弹出一条验证消息,它是“信息有一些错误,请插入正确的信息。”但是它出现了2个弹出框,它是“信息有一些错误,请插入正确的信息。”和“谢谢!您的订单已经下了!”。我可以知道有什么问题吗 $fnameErr=$lnameErr=$add1Err=$add2Err=$postErr=$mobileErr=$creditErr=$pinErr =""; $credit=$pin=""; if($_REQUEST['command']=='update')
$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>";
}
}
}
}
?>
$fnameErr=$lnameErr=$add1Err=$add2Err=$postErr=$mobileErr=$creditErr=$pinErr=”“;
$credit=$pin=“”;
如果($_请求['command']=='update')
{
$date=日期('Y-m-d');
$time=日期(“H:i:s”);
$charge=$_请求['ocharge'];
$fname=$\请求['ofname'];
$lname=$\u请求['olname'];
$mobile=$_请求['omobile'];
$add1=$_请求['oadd1'];
$add2=$_请求['oadd2'];
$postcode=$_请求['opostcode'];
$state=$_请求['ostate'];
$country=$\请求['occountry'];
$credit=$_请求['ocredit'];
$pin=$_请求['opin'];
$city=$_请求['ocity];
如果($fname==“”| |$lname==“”| |$mobile==“”| |$add1=“”| |$add2==“”| |$postcode==“”| |$$state==“”| |$$country==“”| |$$credit
{
?>
警告(“请填写所有要求的信息”);
您的代码结构似乎不正确。您正在检查各个字段是否存在错误,然后显示错误消息,而不管是否存在任何错误。之后,您将继续保存信息,并显示其他警报,而不管错误检查发生了什么
在不深入了解代码细节的情况下,构建代码的一种方法是:
$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>";
}
}
}
?>
$errors=array();
//fname xx
如果(!preg_match(“/^[A-Z][A-zA-Z-]+$/i”,$_请求['of name']))
{
$fnameErr='您的名字不能包含任何符号和数字';
数组推送($errors,$fnameErr);
}
//lname xx
如果(!preg_match(“/^[A-Z][A-zA-Z-]+$/i”,$_请求['olname']))
{
$lnameErr='您的姓氏不能包含任何符号和数字';
数组推送($errors,$lnameErr);
}
//地址:xx
如果(!preg_match(“/^[a-zA-Z0-9,:\“\”]+$/i”,$\u请求['oadd1']))
{
$add1Err='地址1必须仅为字母、数字或以下其中一个';
数组推送($errors,$add1errr);
}
//地址2 xx
如果(!preg_match(“/^[a-zA-Z0-9,:\“\”]+$/i”,$\u请求['oadd2']))
{
$add2Err='地址2必须仅为字母、数字或以下其中一个';
数组推送($errors,$add2Err);
}
//邮政编码xx
如果(!preg_match(“/^\d{5}$/i”,$_请求['opostcode']))
{
$postErr='邮政编码必须为5位';
数组推送($errors,$postErr);
}
//移动xx
如果(!preg_match(“/^\d{3}-\d{7}$/i”,$_请求['omobile']))
{
$mobileErr='手机必须符合此掩码:010-1111111或0111-1111111';
阵列推送($errors,$mobileErr);
}
//信用卡xx
如果(!preg_match(“/^\d{4}-\d{4}-\d{4}-\d{4}$/i”,$_请求['ocredit']))
{
$creditErr='信用卡必须符合此掩码:0000-1111-2222-3333';
数组推送($errors,$creditErr);
}
//引脚xx
如果(!preg_match(“/^\d{6}$/i”,$_请求['opin']))
{
$pinErr='Pin必须为6位';
数组推送($errors,$pinErr);
}
如果(计数($errors)>0){
?>
警告(“信息有一些错误,请插入正确的信息。”);
这正是Javascript的用武之地。这不能通过纯Javascript实现吗?或者如果验证需要一些PHP功能,可以使用AJAX进行测试吗?当我在这里时,mysql\u query
不受欢迎。看看PDO或mysqli.*
。我很确定在数据库中保存信用卡号和PIN码不是PCI compliant()。