我有以下代码,它来自基本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的文本框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
}
当我添加此代码时,脚本将停止运行。任何帮助,将不胜感激。感谢。
答案 0 :(得分:1)
getThankYouUrl()
。
这在header()
中用于获取重定向位置。$value = strtolower(substr($name_from, 0, 3));
)的匹配确实有效,因此我为var_dump()
和$name_from
添加了$value
更容易调试。评论该线路,看看那里发生了什么...... $website
+页面(基于条件)。根据您的需要调整$website
。<?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;
}
答案 1 :(得分:1)
试试这个,通过标题调用其他页面
<?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 :(得分:0)
<?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行代码提供多功能,检查它是否正常工作......