我决定为我的Wordpress博客硬编码联系表格。我创建了一个模板,如下所示。尽管花了一些时间来看,我还是看不出它的问题。任何帮助将不胜感激。此外,我没有忘记消毒输入。当我在这里看到问题时,我会添加该功能。提前谢谢......
顺便提一下,我收到此消息:解析错误:语法错误,第54行的F:\ xampp \ htdocs \ new_theme \ wp-content \ themes \ twentytwelve_child \ contact.php中的意外'其他'(T_ELSE)
<?php /* Template Name: contact */?>
<?php get_header(); ?>
<?php
if(isset($_POST["submitted"])){
if($_POST["contactName"] === ""){
$nameError = "Please enter your name.";
$hasError = true;
}
else $name = $_POST['contactName'];
if($_POST["email"] === ""){
$emailError = "Please enter your email address.";
$hasError = true;
}
else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $_POST['email'])) {
$emailError = "You entered an invalid email address.";
$hasError = true;
}
else $email = $_POST['email'];
if($_POST['comments'] === ""){
$commentError = "Please enter a message.";
$hasError = true;
}
else $comments = $_POST["comments"];
}
if(!isset($hasError)) {
$emailTo = "mathornley@gmail.com";
$subject = "From Android Scoop - From: $name";
$body = "Name: $name \n Email: $email \n Comments: $comments";
$headers = "From: $name <$emailTo> \n Reply-To: $email";
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
&GT;
<?php if(isset($emailSent) && $emailSent == true){ ?>
<div class="thanks">
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } ?>
<?php else{?>
<?php if(isset($hasError){?> <p class="error">Sorry, an error occured.<p>
<div class="form_left">
<form action="<?php the_permalink();?>" method="post">
<label><span class="requiring">*</span>Name:</label>
<input type="text" name="contactName"/>
<?php if($nameError)?> <span class="error"><?=$nameError;?></span>
<label><span class="requiring">*</span>Email</label>
<input type="text" name="email"/>
<?php if($emailError)?><span class="error"><?=$emailError;?></span>
<label>Phone</label><input type="text" name="phone"/>
<input type="submit" value="Send your message"/>
<input type="hidden" name="submitted" value="true" />
</div>
<div class="form_right">
<label><span class="requiring">*</span>Message:</label>
<textarea name="comments" rows="20" cols="30" class="required requiredField"></textarea>
<?php if($commentError)?><span class="error"><?=$commentError;?></span>
</form>
</div>
<?php }} ?>
答案 0 :(得分:0)
有两种语法错误:
<?php }else{
if(isset($hasError)){?>
1,
<?php } ?>
<?php else{?>
您无法关闭PHP标记,然后在使用括号时从else
开始再次打开它。如果你使用替代if / then语句,那么这是可能的
2,
if(isset($hasError)
你错过了if语句的右括号
全:
<?php
if(isset($emailSent) && $emailSent == true){
echo '<div class="thanks"><p>Thanks, your email was sent successfully.</p></div>';
}else{
if(isset($hasError)){?>
<p class="error">Sorry, an error occured.<p>
<div class="form_left">
<form action="<?php the_permalink();?>" method="post">
<label><span class="requiring">*</span>Name:</label>
<input type="text" name="contactName"/>
<?php if($nameError)?> <span class="error"><?=$nameError;?></span>
<label><span class="requiring">*</span>Email</label>
<input type="text" name="email"/>
<?php if($emailError)?><span class="error"><?=$emailError;?></span>
<label>Phone</label><input type="text" name="phone"/>
<input type="submit" value="Send your message"/>
<input type="hidden" name="submitted" value="true" />
</div>
<div class="form_right">
<label><span class="requiring">*</span>Message:</label>
<textarea name="comments" rows="20" cols="30" class="required requiredField"></textarea>
<?php if($commentError)?><span class="error"><?=$commentError;?></span>
</form>
</div>
<?php
}
} ?>