简体   繁体   中英

PHP Contact form script with if else

I have the following code which is taken from the php side of a basic html contact form:

<?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();
?>

It takes the value of the textbox fullname_1 where the fullname is entered and sends an email. and takes the user to a thank you page once submit is clicked.

However i want to add a condition where it decides the thank you page depending on the first three letters of the text enters so i have the following code:

$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
}

When i add this code, the script stops functioning. Any help would be appreciated. Thanks.

  • I've added a function getThankYouUrl() . This is used inside header() to get the redirect location.
  • I'm not sure, if you really need the javascript redirect, if you do a PHP header redirection before - so i dropped it. If it's needed, please readd.
  • You have to make sure that the matching of the first 3 chars ( $value = strtolower(substr($name_from, 0, 3)); ) really works, so i've added a var_dump() for $name_from and $value for easier debugging. Comment the line out, to see whats going on there...
  • The redirect location itself is not longer using relatives "../somewhere.html", but an absolute location $website + page (based on the condition). Adjust $website to your needs.
  • additionally this might help, too: 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;
}

Try this, call other page through 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();
    ?>
<?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;
    }
}

Why we can take mutiple function for 20 to 40 line code,Check it's working...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM