我有一些PHP提交表单,如果字段没有正确填写或者转到带有“谢谢”消息的其他页面,则应该抛出错误。
这是HTML:
<form id="contact-form" method="post" action="sendformemail.php">
<fieldset>
<legend>Contact Me</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="user_name" placeholder="Enter your name here" class="clear-right">
<label for="email">Email:</label>
<input type="email" id="email" name="user_email" placeholder="Enter your email address here!" class="clear-right">
<label for="job">Reason for contact:</label>
<select id="job" name="user_job" class="clear-right">
<optgroup label="Work">
<option value="Application Development">Application Development/Design</option>
<option value="Website Design">Website Development/Design</option>
<option value="Computer Diagnostics">Computer Diagnostics</option>
</optgroup>
<optgroup label="Business">
<option value="looking for a job">Looking for a Job</option>
</optgroup>
<optgroup label="Alternative">
<option value="Other Reason">Other Reason</option>
<option value="Ask a question" selected>Ask a Question</option>
</optgroup>
</select>
<label for="textarea">Message:</label>
<textarea id="textarea" name="user_text" rows="10" cols= "50" placeholder="Write your message here..."></textarea>
<label style="display:none;" for="address">address</label>
<input style="display:none;" type="text" id="address" name="address" placeholder="Humans do not enter anything in here." class="clear-right">
</fieldset>
<button type="submit" class="secondary">Send Message</button>
</form>
这是PHP:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["user_name"]);
$email = trim($_POST["user_email"]);
$jobtype = trim($_POST["user_job"]);
$message = trim($_POST["user_text"]);
}
else {
echo "this does not work very well";
}
if ($name =="" OR $email == "" OR $jobtype == "" OR $message == "") {
echo "you must specify a value for name, email address and message";
exit;
}
else {
echo "this doesnt work";
}
foreach ($_POST as $value) {
if (stripos($value, 'Content-Type:')!== FALSE) {
echo "There was a problem submitting your form";
exit;
}
else {
echo "this doesnt work 2";
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
else {
echo "this doesnt work 3";
}
require_once('inc/class.phpmailer.php');
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)) {
echo "You must specify a valid email address";
exit;
}
else {
echo "this doesnt work 4";
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email Address: " . $email . "<br>";
$email_body = $email_body . "Reason for comntact: " . $jobtype . "<br>";
$email_body = $email_body . "Message: " . $message;
对于任何观看此内容并想知道问题是什么的人来说,这四行代码就是问题所在。
Server: smtp.postmarkapp.com
Ports: 25, 2525, or 587
Username/Password: ********************
Authentication: Plain text, CRAM-MD5, or TLS
其他一切都没问题
$mail->SetFrom($email,$name);
$address = "me@nickdavies.info";
$mail->AddAddress($address, "Nick Davies");
$mail->Subject = $jobtype;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "The was a problem sending your message: " . $mail->ErrorInfo;
exit;
}
else {
echo "this doesnt work 4";
}
header("location: contact-thanks.php");
exit;
?>
我把echo放在条件中以查看错误的位置,但是没有显示错误,只是一个没有任何内容的普通页面。
答案 0 :(得分:1)
你的if / else标识不正确,else匹配之前的if子句,所以:
if{
else {
echo "this does not work very well";
}
}
不正确,请再次检查您想要做什么。
正确的是:
if(some_condition){
do some;
}
else{
//code here
}
要检查字段,使用Javascript会更容易,请尝试执行以下操作:
<form name ="form" action="script.php" onsubmit="myFunction()">
Enter name: <input type="text" name="name_field">
<input type="submit" value="Submit">
</form>
<script>
function myFunction() {
if(document.form.name_field.value== ""){
alert("empty field");
//...do something
return false;
}
}
</script>