帮助!我似乎无法让我的表单通过JavaScript验证,然后如果成功通过PHP验证。我做错了什么?在通过javascript函数检查后,它只是重新加载一个空白的checkout.php?我不能为我的生活弄清楚为什么会这样。它需要检查javascript然后如果成功检查php,然后如果成功也提交并转到conformation.php页面。
Javascript验证码:
function validateForm() {
valid = true;
var a=document.forms["checkout"]["fname"].value;
if (a==null || a=="")
{
alert("First name must be filled out");
valid = false;
}
var b=document.forms["checkout"]["lname"].value;
if (b==null || b=="")
{
alert("Last name must be filled out");
valid = false;
}
var c=document.forms["checkout"]["address"].value;
if (c==null || c=="")
{
alert("Address must be filled out");
valid = false;
}
var d=document.forms["checkout"]["email"].value;
if (d==null || d=="")
{
alert("Email address must be filled out");
valid = false;
}
var e=document.forms["checkout"]["email"].value;
if(!validateEmail(e)){
alert("Must be a valid email address");
valid = false;
}
var f=document.forms["checkout"]["phone"].value;
if (f==null || f=="")
{
alert("Phone number must be filled out");
valid = false;
}
var g=document.forms["checkout"]["phone"].value;
if(!validatePhone(g)){
alert("Must be a valid phone number");
valid = false;
}
var h=document.forms["checkout"]["card"].value;
if (h==null || h=="")
{
alert("Credit card info must be filled out");
valid = false;
}
var i=document.forms["checkout"]["card"].value;
if(!validateCard(i)){
alert("Must be a valid credit card number");
valid = false;
}
return valid;
}
function validateEmail(email) {
var pattern = new RegExp(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/);
return pattern.test(email);
}
function validatePhone(phone) {
var ppattern = new RegExp(/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/);
return ppattern.test(phone);
}
function validateCard(card) {
var cpattern = new RegExp(/(?:\d[ ]?){12,17}\d/);
return cpattern.test(card);
}
php验证码:
if(isset($_POST['SubmitForm'])){
if(empty($_POST['fname'])){
echo "First name cannot be empty!<br/><br/>";
return false;
}
if(empty($_POST['lname'])){
echo "Last name cannot be empty!<br/><br/>";
return false;
}
if(empty($_POST['address'])){
echo "Address field cannot be empty!<br/><br/>";
return false;
}
if(empty($_POST['email'])){
echo "Email field cannot be empty!<br/><br/>";
return false;
}
if($_POST['email']){
$email = $_POST['email'];
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$match = preg_match($regex,$email);
if(!$match)
echo "Not a valid email address!<br/><br/>";
return false;
}
if(empty($_POST['phone'])){
echo "Phone number cannot be empty!<br/><br/>";
return false;
}
if($_POST['phone']){
$phone = $_POST['phone'];
$reg = '/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/';
$pmatch = preg_match($reg, $phone);
if (!$pmatch)
echo "Not a valid phone number!<br/><br/>";
return false;
}
if(empty($_POST['card'])){
echo "Card field cannot be empty!<br/><br/>";
return false;
}
if($_POST['card']){
$card = $_POST['card'];
$match = preg_match("#[0-9]#",$card);
if(strlen($card) < '13')
echo "Card field must be at least 13 digits <br/><br/>";
return false;
if(!$match)
echo "Card number must be numeric<br/><br/>";
return false;
}
if($_POST['month']){
$month = $_POST['month'];
if($month < 05)
echo "Expired credit card";
return false;
}
return true;
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$email = $_SESSION['email'];
$phone = $_SESSION['phone'];
$representation = json_encode($_SESSION['cart'],JSON_PRETTY_PRINT);
$pfile = fopen("orders.txt","a+");
rewind($pfile);
fwrite($pfile, "\r\n$fname:$lname:$email:$phone:$representation");
fclose($pfile);
}
最后是表单本身:
<form name = 'checkout' method='post' onsubmit="return validateForm()" action="<?php echo $_SERVER['PHP_SELF']; ?>" accept-charset='UTF-8'>
<table style="border:thin solid #ccc;" rules="rows" cellpadding="6px" width="50%">
<tr><td>
</td></tr>
<tr><td>First Name:<input type="text" name="fname" class="required" value="<?php if (isset($fname)){echo $fname;}?>" /></td></tr>
<tr><td>
</td></tr>
<tr><td>Last Name:<input type="text" name="lname" value="<?php if (isset($lname)){echo $lname;}?>" /></td></tr>
<tr><td>
</td></tr>
<tr><td>Address:</br><textarea name="address" id="address" value="<?php if (isset($address)){echo $address;}?>"></textarea></td></tr>
<tr><td>
</td></tr>
<tr><td>Email:<input type="text" name="email" value="<?php if (isset($email)){echo $email;}?>"/></td></tr>
<tr><td>
</td></tr>
<tr><td>Phone:<input type="text" name="phone" value="<?php if (isset($phone)){echo $phone;}?>"/></td></tr>
<tr><td>
</td></tr>
<tr><td>Delivery method:<input type="radio" name="delivery"
<?php if (isset($delivery) && $delivery=="regular post") echo "checked";?>
value="regular post" checked>Regular Post
<input type="radio" name="delivery"
<?php if (isset($delivery) && $delivery=="courier") echo "checked";?>
value="courier">Courier
<input type="radio" name="delivery"
<?php if (isset($delivery) && $delivery=="express courier") echo "checked";?>
value="express courier">Express Courier</td></tr>
<tr><td>
</td></tr>
<tr><td>Credit Card No:<input type='text' name='card' maxlength='18' size='19' value="<?php if (isset($card)){echo $card;}?>"/></td></tr>
<tr><td>
</td></tr>
<tr><td>Expiration Month:<select name="month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option></select></td></tr>
<tr><td>
</td></tr>
<tr><td>Expiration Year:<select name="year">
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option></select></td></tr>
<tr><td>
</td></tr>
<tr><td><input type="checkbox" value="newsletter" name="letter[]">Check here to sign up for the newsletter</td></tr>
<tr><td>
</td></tr>
<tr><td colspan="2" align="left"><input type="submit" name="SubmitForm" value="Send"/></td></tr>
</table>
答案 0 :(得分:1)
这部分验证错误!
如果您将$_POST['month']
传递给验证,则会导致验证始终返回FALSE。
在运行文件创建代码之前,它也不会像return true
一样运行文件生成。
if($_POST['month']){
$month = $_POST['month'];
if($month < 05)
echo "Expired credit card";
return false;
}
return true;
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$email = $_SESSION['email'];
$phone = $_SESSION['phone'];
$representation = json_encode($_SESSION['cart'],JSON_PRETTY_PRINT);
$pfile = fopen("orders.txt","a+");
rewind($pfile);
fwrite($pfile, "\r\n$fname:$lname:$email:$phone:$representation");
fclose($pfile);
猜测它应该看起来像这样:
if($_POST['month']){
$month = $_POST['month'];
if($month < 05) {
echo "Expired credit card";
return false;
}
}
$fname = $_SESSION['fname'];
$lname = $_SESSION['lname'];
$email = $_SESSION['email'];
$phone = $_SESSION['phone'];
$representation = json_encode($_SESSION['cart'],JSON_PRETTY_PRINT);
$pfile = fopen("orders.txt","a+");
rewind($pfile);
fwrite($pfile, "\r\n$fname:$lname:$email:$phone:$representation");
fclose($pfile);
return true;