【问题标题】:PHP Contact form script with if else带有if else的PHP联系表单脚本
【发布时间】:2015-04-22 11:38:00
【问题描述】:

我有以下代码,取自基本 html 联系表单的 php 端:

<?php
session_start();

if(isset($_POST['fullname_1'])) {

include 'formsettings.php';

function died($error) {
    echo "Sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();

}

if(!isset($_POST['fullname_1']) || !isset($_POST['fullname_1'])) {
    died('Sorry, there appears to be a problem with your form submission.');        
}

$name_from = $_POST['fullname_1']; // required
$ip = $_SERVER['REMOTE_ADDR'];  
$error_message = "";
$email_message = "Form details below.\r\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:");
  return str_replace($bad,"",$string);
}

$email_message .= "Full name is: ".clean_string($name_from)."\r\n"; 
$email_message .="IP Address: ".clean_string($ip)."\n\n"; 

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message, $headers);

header("Location: $thankyou");

?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>

它采用输入全名的文本框 fullname_1 的值并发送电子邮件。并在点击提交后将用户带到感谢页面。

但是我想添加一个条件,它根据输入的文本的前三个字母来决定感谢页面,所以我有以下代码:

$value = substr($name_from,0,3);
if (strtolower($value) === "abc"){
    $thankyou = "../abc.html"; // male
}
elseif($value === "def"){
    $thankyou = "../def.html"; // female
}
else{
    $thankyou = "../xyz.html"; // no male, no female
}

当我添加此代码时,脚本停止运行。任何帮助,将不胜感激。谢谢。

【问题讨论】:

标签: php forms contact


【解决方案1】:

试试这个,通过header调用其他页面

<?php
    session_start();

    if(isset($_POST['fullname_1'])) {

    include 'formsettings.php';


    function died($error) {

        echo "Sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }

    if(!isset($_POST['fullname_1']) || 

              !isset($_POST['fullname_1'])

        ) {

        died('Sorry, there appears to be a problem with your form submission.');        

    }

    $name_from = $_POST['fullname_1']; // required


    $ip = $_SERVER['REMOTE_ADDR'];  

    $error_message = "";

    $email_message = "Form details below.\r\n";

    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:");

      return str_replace($bad,"",$string);
    }

    $email_message .= "Full name is: ".clean_string($name_from)."\r\n"; 

    $email_message .="IP Address: ".clean_string($ip)."\n\n"; 

    $headers = 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .

    'X-Mailer: PHP/' . phpversion();

    mail($email_to, $email_subject, $email_message, $headers);

    $value = substr($name_from,0,3);
    if (strtolower($value) === "abc"){
        $thankyou = "../abc.html"; // male
    }
    elseif($value === "def"){
        $thankyou = "../def.html"; // female
    }
    else{
        $thankyou = "../xyz.html"; // no male, no female
    }

    header("Location: $thankyou");
    ?>

    <?php
    }
    die();
    ?>

【讨论】:

    【解决方案2】:
    • 我添加了一个函数getThankYouUrl()。 这在 header() 内部使用以获取重定向位置。
    • 我不确定,如果您真的需要 javascript 重定向,如果您之前执行过 PHP 标头重定向 - 所以我放弃了它。如果需要,请阅读。
    • 您必须确保前 3 个字符 ($value = strtolower(substr($name_from, 0, 3));) 的匹配确实有效,因此我为 $name_from$value 添加了一个 var_dump(),以便于调试。注释掉这条线,看看那里发生了什么......
    • 重定向位置本身不再使用亲属“../somewhere.html”,而是绝对位置$website+页面(根据条件)。根据您的需要调整 $website
    • 另外,这也可能有所帮助:header('location: ..') not working

    <?php
    
    session_start();
    
    if (isset($_POST['fullname_1'])) {
    
        include 'formsettings.php';
    
        function died($error)
        {
            echo "Sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error . "<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
    
        if (!isset($_POST['fullname_1']) || !isset($_POST['fullname_1'])) {
            died('Sorry, there appears to be a problem with your form submission.');
        }
    
        $name_from     = $_POST['fullname_1']; // required
        $ip            = $_SERVER['REMOTE_ADDR'];
        $error_message = "";
        $email_message = "Form details below.\r\n";
    
        function clean_string($string)
        {
            $bad = array("content-type", "bcc:", "to:", "cc:");
            return str_replace($bad, "", $string);
        }
    
        function getThankYouUrl($name_from)
        {
            $website = 'http://your.domain.com/';
    
            $value = strtolower(substr($name_from, 0, 3));
    
            // for debugging the strings
            //var_dump($value, $name_from);
    
            if ($value === "abc") {
                $url = $website . "abc.html"; // male
            } elseif ($value === "def") {
                $url = $website . "def.html"; // female
            } else {
                $url = $website . "xyz.html"; // no male, no female
            }
    
            return $url;
        }
    
        $email_message .= "Full name is: " . clean_string($name_from) . "\r\n";
        $email_message .= "IP Address: " . clean_string($ip) . "\n\n";
    
        $headers = 'From: ' . $email_from . "\r\n" .
            'Reply-To: ' . $email_from . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
    
        mail($email_to, $email_subject, $email_message, $headers);
    
        header("Location: " . getThankYouUrl($name_from));
        exit;
    }
    

    【讨论】:

    • 我如何使用相对网址而不是绝对网址,您可以编辑它吗?
    • 如果你有这个$url = $website . "abc.html";,那么只需删除$website .(域的串联),你就会得到$url = "abc.html";。然后调整它,就像你之前的"../abc.html";
    【解决方案3】:
    <?php
    
    session_start();
    
    if (isset($_POST['fullname_1'])) {
    
        include 'formsettings.php';
    
        function died($error)
        {
            echo "Sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error . "<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
    
        if (!isset($_POST['fullname_1']) || !isset($_POST['fullname_1'])) {
            died('Sorry, there appears to be a problem with your form submission.');
        }
    
        $name_from     = $_POST['fullname_1']; // required
        $ip            = $_SERVER['REMOTE_ADDR'];
        $error_message = "";
        $email_message = "Form details below.\r\n";
        $website = 'http://your.domain.com/';
        $value = strtolower(substr($name_from, 0, 3));
    
    
        function clean_string($string)
        {
            $bad = array("content-type", "bcc:", "to:", "cc:");
            return str_replace($bad, "", $string);
        }
    
        $email_message .= "Full name is: " . clean_string($name_from) . "\r\n";
        $email_message .= "IP Address: " . clean_string($ip) . "\n\n";
    
        $headers = 'From: ' . $email_from . "\r\n" .
            'Reply-To: ' . $email_from . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
    
        $mail_send=mail($email_to, $email_subject, $email_message, $headers);
    
         if($mailsend)
        {
             $url="";
    
            if ($value === "abc") {
                    $url = $website . "abc.html"; // male
                } elseif ($value === "def") {
                    $url = $website . "def.html"; // female
                } else {
                    $url = $website . "xyz.html"; // no male, no female
                }
    
            header("Location: ".$url;);
            exit;
        }
    }
    

    为什么我们可以为 20 到 40 行代码使用多个功能,检查它是否工作...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      相关资源
      最近更新 更多