PHP如果没有错误,如何提交表单。没有javascript

时间:2014-11-12 11:42:03

标签: php html email

我有单独的电子邮件脚本;但是,如果没有错误,我们将如何运行该代码。我有一个表单错误$errors = array($nameErr, $emailErr, $phoneErr, $zipErr, $serviceErr);的数组,但它们有不同的字符串,如果数组中没有字符串或Null'',我们希望发送电子邮件。



<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $phoneErr = $emailErr = $zipErr = $serviceErr = "";
$name = $phone = $email = $zip = $service = $comment = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   	
   if (empty($_POST["name"])) {
     $nameErr = "name required.";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "letters and spaces only."; 
     }
   }
   
   if (empty($_POST["email"])) {
     $emailErr = "email required.";
   } else {
     $email = test_input($_POST["email"]);
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "invalid email format."; 
     }
   }
   
   if (empty($_POST["phone"])) {
       $phoneErr = "phone required.";
   } else {
   	//Check phone for numbers () or - only
       $phone = test_input($_POST["phone"]);
	   if (!preg_match("/^[\+0-9\-\(\)\s]*$/", $phone)) {
	   	$phoneErr = "format.";		   
	   }
   }
   
     if (empty($_POST["zip"])) {
       $zipErr = "zip required.";
   } else {
       $zip = test_input($_POST["zip"]);
   }
   if (!preg_match("/^[\+0-9\-\(\)\s]*$/", $zip)){
       $zipErr = "format.";
   }
   
   
   if ($_POST["service"] == NULL ) {
       $serviceErr = "service required.";
   }else {
   	$service = test_input($_POST["service"]);
   }
	$comment = test_input($_POST["comment"]);
	
	//**********************************************************************
	$errors = array($nameErr, $emailErr, $phoneErr, $zipErr, $serviceErr);	
	if (isset($_POST['Submit'])) {
            //if no errors run send email CODE.
     }
    //***********************************************************************
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
<form <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
	<table>
		<tr>
			<td> Name:
			<br />
			<input name="name" type="text" size="20" value="<?php echo $name;?>">
			<span class="error">* <?php echo "<br />"; echo $nameErr;?></span>
			</td>
		</tr>
		<tr>
			<td> Phone:
			<br />
			<input name="phone" type="text"  size="20" value="<?php echo $phone;?>">
			<span class="error">* <?php echo "<br />"; echo $phoneErr;?></span>
			</td>
		</tr>
		<tr>
			<td> E-mail:
			<br />
			<input name="email" type="text" size="20" value="<?php echo $email;?>">
			<span class="error">* <?php echo "<br />"; echo $emailErr;?></span>
			</td>
		</tr>
		<tr>
			<td> Zip:
			<br />
			<input name="zip" type="text" size="20" value="<?php echo $zip;?>">
			<span class="error">* <?php echo "<br />"; echo $zipErr;?></span>			
			</td>
		</tr>
		<tr>
			<td> Service:
			<br />
			<select name="service">
				<option selected="selected" value="<?php echo $service;?>"><?php echo $service;?></option>
				<option value="A">A</option>
				<option value="B">B</option>
				<option value="C">C</option>
				<option value="D">D</option>
			</select>
			<span class="error">* <?php echo "<br />"; echo $serviceErr;?></span>
			</td>
		</tr>
		<tr>
			<td> Message:
			<br />
			<textarea name="comment" rows="2" cols="20"><?php echo $comment;?></textarea></td>
		</tr>
		<tr>
			<td>
			<input type="submit" name="Submit" value="Send" />
			</td>
		</tr>
	</table>
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $phone;
echo "<br>";
echo $zip;
echo "<br>";
echo $service;
echo "<br>";
echo "$comment";
?>

</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

$errors = array($nameErr, $emailErr, $phoneErr, $zipErr, $serviceErr);  
if (isset($_POST['Submit'])) {
   if(!array_filter($errors)){
           // code here 
   }
   else {
   echo "Error";
   }
}

答案 1 :(得分:0)

将错误保存在数组中,然后检查数组末尾是否为空。如果是这样,没有错误 - 提交电子邮件。否则,显示错误:

//dont declare separate variables,use an array
//$nameErr = $phoneErr = $emailErr = $zipErr = $serviceErr = "";
$errors = [];

if (empty($_POST["name"])) {
     $errors['nameErr'] = "name required.";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $errors['nameErr'] = "letters and spaces only."; 
     }
   }

//other validation here, then

if(empty($errors){
    //no errors, submit
    your_submit_function();
}else{
    //display errors
    foreach($errors as $val){
        echo $val . '<br/>';
    }
}