【问题标题】:PHP mail redirect header location based on nested if statement基于嵌套 if 语句的 PHP 邮件重定向标头位置
【发布时间】:2016-05-19 10:47:17
【问题描述】:

您好,我正在为客户设计网站。 我设计了这个表格

    <form action="inc/email.php" method="POST" onSubmit="alert('Email send successfully!');">
    <div class="col-md-6">
        <input type="text" id="contact-name" name="contact-name" value="" placeholder="Your name" required/>
        <input type="email" id="contact-email" name="contact-email" value="" placeholder="Your email" required/>
        <input type="number" id="contact-phone" name="contact-phone" value="" placeholder="Your phone" required/>
    </div>
    <div class="col-md-6">
        <select name="cars">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
            <option value="audi">Audi</option>
        </select>
    </div>
    <div class="col-md-12">
        <input type="submit" value="Send message"/></td>
    </div>
</form>

它有 3 个选项可供选择,从第 1 辆沃尔沃、第 2 辆萨博和第 3 辆奥迪
现在我想根据用户选择将表单提交重定向到不同的网站
例如facebook.com 如果用户选择 Volvo,Twiiter.com 如果他选择 Saab。

我为相同的

设计了以下PHP代码
<?php
/*Getting submitted values from html form*/

$name = $_POST['contact-name'];
$email = $_POST['contact-email'];
$phone = $_POST['contact-phone'];
$cars= $_POST['cars'];
/* Generating Output HTML */

$output = '<html><body><div>';
$output .= '<h2 style="color: #49A020;">Newsletter Email</h2>';
$output .= '<p style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;">Name: '.$name.'</p>';
$output .= 'Phone: <a style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;" href="tel: '.$phone.'">'.$phone.'</a><br><br>';
$output .= 'Email: <a  style="line-height: 28px;font-size: 16px; text-align: justify; color: #333;" href="mailto:'.$email.'">'.$email.'</a>';
$output .= '</div><div>';

/*Signature */

$output .= '<hr style="border:1px solid #49A020;">';
$output .= '<p style="margin-top: 0px; margin-bottom: 0px;">Thanks & Regards,</p>';
$output .= '<p style="margin-top: 0px; margin-bottom: 0px;">South Post Fitness,</p>';
$output .= 'Phone: <a href="tel:416843 5925">(416) 843 5925</a><br>';
$output .='Email: <a href="maito: info@xx.com">info@xx.com</a>';
$output .= '</div></body></html>';

 $to = 'my email address here';  /* Receivers Email*/

$headers = "From: $email \r\n"; /*Headers*/
$headers .= 'MIME-Version: 1.0' . "\n"; /*Headers*/
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n"; /*Headers*/

$subject = 'my subject here'; /* Subject */
mail($to, $subject, $output, $headers); /*Sending Mail*/
  if(mail) {
    if($cars=="Volvo"){
          header("Location: http://facebook.com");  /*Redirect to home after sucessfull submission*/
    }
    elseif($cars=="Saab"){
      header("Location: http://twitter.com");  /*Redirect to home after sucessfull submission*/
    }
    elseif($cars=="Audi"){
        header("Location: http://pintrest.com");  /*Redirect to home after sucessfull submission*/
    }
  }
  else {
  echo 'Error sending email'; /*If failed display error message */
  }

?>

但无论选择哪个选项,此代码都只会将页面重定向到第一个链接,即 facebook.com

【问题讨论】:

    标签: php wordpress email redirect


    【解决方案1】:
    mail($to, $subject, $output, $headers); /*Sending Mail*/
    if(mail) {
    

    应该是

    $mail = mail($to, $subject, $output, $headers); /*Sending Mail*/
    if($mail) {
    

    请注意您的条件中缺少的变量赋值和缺少的$ 符号。

    您检查大写字符串,但您的实际字符串是小写的。

    if($cars=="Volvo")
    

    不一样
    if($cars=="volvo")
    

    【讨论】:

    • 是的,它解决了问题,我将实际值称为“Volvo”和“Saab”,但不是 html value="saab" 我已将代码替换为 $cars2=$cars;然后 if($cars2=="volvo") 带有小“v”并且它现在正在工作,我没有替换任何其他代码。
    【解决方案2】:

    非常简短的答案,但是您正在从 $_POST['cars'] 设置 $cars 并且您的选择正在发布所有值,因此请更改

     <select name="cars">
    

     <select name="cars[]">
    

    这样您将获得$_POST['cars'] 数组,其中包含您选择的所有值。否则它只会先发布&lt;option&gt;

    编辑

    正如另一个答案所说,您绝对应该这样做$mail_success = mail(); 因为函数 mail() 返回 bool。 然后你可以做一个if($mail_success) {}

    编辑 2header() 之后添加die; - 否则脚本可能会继续执行并导致过于意外的行为。

    【讨论】:

      【解决方案3】:

      mail 函数的返回类型为 bool,因此您应该使用结果进行检查:

      $result = mail($to, $subject, $output, $headers);
      if($result) {
          ...
      }
      

      应避免与== 进行字符串比较。使用strcmp('string1', 'string2') == 0 进行相等检查。

      如果您将代码的最后一部分更改为使用strcmp,它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-02
        • 2017-05-17
        • 1970-01-01
        相关资源
        最近更新 更多