我正在处理这个PHP表单,我一直在遇到这个问题。
<?php
// User settings
$to = "myemail@yahooo.com";
$subject = "Test Form";
// Include extra form fields and/or submitter data?
// false = do not include
$extra = array(
"form_country" => true,
"form_stateSelect" => true
);
// Process
$action = isset($_POST["action"]) ? $_POST["action"] : "";
if (empty($action)) {
// Send back the contact form HTML
$output = "<div style='display:none'>
<div class='contact-content'>
<h1 class='contact-title'>Send us a message:</h1>
<div class='contact-loading' style='display:none'></div>
<div class='contact-message' style='display:none'></div>
<form action='#' style='display:none'>
<label for='contact-name'>Name</label>
<input type='text' id='contact-name' class='contact-input' name='name' />";
if ($extra["form_country"]) {
$output .= "
<label for='contact-country'>Servicio</label>
<table>
<tr>
<td>
<select id='contact-country' name='country' onchange='populateState()'>
</select>
</td>
</tr>
</table>";
}
if ($extra["form_stateSelect"]) {
$output .= "
<label for='contact-stateSelect'>Sub-servicio</label>
<table>
<td>
<select id='contact-stateSelect' name='stateSelect'>
</select>
<script language='javascript'>initCountry('US'); </script>
</td> </table>
";
}
$output .= "
<label> </label>
<button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
<button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
<br/>
<input type='hidden' name='token' value='" . smcf_token($to) . "'/>
</form>
</div>
</div>";
echo $output;
}
else if ($action == "send") {
// Send the email
$name = isset($_POST["name"]) ? $_POST["name"] : "";
$country = isset($_POST["country"]) ? $_POST["country"] : $country;
$stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : $stateSelect ;
$token = isset($_POST["token"]) ? $_POST["token"] : "";
// make sure the token matches
if ($token === smcf_token($to)) {
smcf_send($name, $country, $stateSelect );
echo "Your message was successfully sent.";
}
else {
echo "Unfortunately, your message could not be verified.";
}
}
function smcf_token($s) {
return md5("smcf-" . $s . date("WY"));
}
// Validate and send email
function smcf_send($name, $country, $stateSelect) {
global $to, $extra;
// Filter and validate fields
$name = smcf_filter($name);
$country = smcf_filter($country);
$stateSelect = smcf_filter($stateSelect);
// Set and wordwrap message body
$body = "From: $name\n\n";
$body .= "Servicio: $country\n\n";
$body .= "Sub-servicio: $stateSelect";
// Build header
$headers = "From: $name\n";
$headers .= "X-Mailer: PHP/SimpleModalContactForm";
// UTF-8
if (function_exists('mb_encode_mimeheader')) {
$country = mb_encode_mimeheader($country, "UTF-8", "B", "\n");
$stateSelect = mb_encode_mimeheader($stateSelect, "UTF-8", "B", "\n");
}
else {
// you need to enable mb_encode_mimeheader or risk
// getting emails that are not UTF-8 encoded
}
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\n";
// Send email
@mail($to, $subject, $body, $headers) or
die("Unfortunately, a server issue prevented delivery of your message.");
}
// Remove any un-safe values to prevent email injection
function smcf_filter($value) {
$pattern = array("/\n/","/\r/","/content-type:/i","/to:/i", "/from:/i", "/cc:/i");
$value = preg_replace($pattern, "", $value);
return $value;
}
exit;
?>
当我尝试发送电子邮件时,它说第81行我有一个未定义的变量:
$stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : $stateSelect ;
当我删除最后一个$stateSelect
并添加""
时,它会删除错误,但是当我发送表单时,该字段显示为空。
$country
和$stateSelect
是下拉列表。我在$country
中选择的任何内容都会影响$stateSelect
下拉列表。
答案 0 :(得分:3)
$stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : $stateSelect ;
^^^^^ undefined at this point
如果未设置POST值,则尝试将stateSelect设置为等于自身。此时,stateSelect
尚未定义,因此您要为自己分配一个未定义的变量。
答案 1 :(得分:0)
看起来有两个问题:
send
阶段未发送stateSelect
请求参数当发生这种情况时,在第81行,您尝试将at-that-time-undefined变量$stateSelect
分配给自身
$ stateSelect = isset($ _ POST [“stateSelect”])? $ _POST [“stateSelect”]:$ stateSelect;
我会检查以确保stateSelect下拉列表的name
为stateSelect
(而不仅仅是id
),以解决第一个问题。
第二个问题,我想办法做到这一点:
$stateSelect = isset($_POST["stateSelect"]) ? $_POST["stateSelect"] : "Select One" ;