0

我决定为我的 Wordpress 博客硬编码一个联系表格。我创建了一个模板,如下所示。尽管花了一些时间看,我还是看不到它的问题。任何帮助将不胜感激。另外,我没有忘记清理输入。当我在这里看到问题时,我会添加该功能。提前致谢...

顺便说一句,我收到这条消息: Parse error: syntax error, unexpected 'else' (T_ELSE) in F:\xampp\htdocs\new_theme\wp-content\themes\twentytwelve_child\contact.php 在第 54 行

<?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;
}

?>

<?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 }} ?>       
4

1 回答 1

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
    }
} ?>  
于 2013-02-14T18:03:57.540 回答