我正在处理一个表单,我已经从我创建的另一个网站进行了更改,这个表单有效,但出于某种原因,它在这里无效。我已经完成了一百次代码并且错过了重要的线索!
表单包含php验证。如果未填写必填字段,则会再次显示该表单,并将已填充的任何内容自动放入新表单中。除非它没有自动填充那些已完成的字段,并且它没有验证有效的提交。
我猜测数据没有正确发布,但我看不出问题出在哪里!
这是最初的形式:
<form name="userform" method="post" action="send_form_email.php">
<table>
<tr>
<td class="form_item_name">Name:</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td class="form_item_name">Company name:</td><td><input type="text" name="company"> </td>
</tr>
<tr>
<td class="form_item_name">Email:</td><td><input type="text" name="email"></td>
</tr>
<tr>
<td class="form_item_name">Phone:</td><td><input type="text" name="phone"></td>
</tr>
<tr>
<td class="form_item_name"></td>
<td>
<select name="subject">
<option value="Requesting A Quote" selected="selected">Requesting A Quote</option>
<option value="Product/Service Support">Product/Service Support</option>
<option value="General Enquiry">General Enquiry</option>
</select>
</td>
</tr>
<tr>
<td class="form_item_name">Your comments:</td><td><textarea style="resize:none; width:350px;" name="comments" rows="10" wrap="hard"></textarea><br />
<br />
<input class="button_send" type="image" value=" " src="images/transparent.gif"></form>
这是动作php(send_form_email.php):
<?php
if(strlen($name) > 2 &&
strlen($email) > 2 &&
strlen($comments) > 2) {
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_to = "sales@happytobevisuals.com";
$email_from = "Website: Contact Us";
if (strpos($subject,'General Enquiry') !== false) {
$email_subject = "General Enquiry";
}
elseif (strpos($subject,'Product/Service Support') !== false) {
$email_subject = "Product/Service Support";
}
elseif (strpos($subject,'Request A Quote') !== false) {
$email_subject = "Quote Request";
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
"Reply-To: ".$email."\r\n" .
"X-Mailer: PHP/" . phpversion();
@mail($email_to, $email_subject, $email_message);
?>
<font face="times new roman" color="#FFF" size="+3">Thanks for that, your message has been sent!</font><br>
<a href="http://www.happytobevisuals.co.nz" class="nav_ingredients">Click here to go back to the home page</a>
<?php
}else{
?>
<font face="times new roman" color="#FFF" size="+3">Woops! Missed something ...</font>
<br>
<br>
<form name="userform" method="post" onsubmit="return validate();" action="send_form_email.php">
<table>
<tr>
<td class="form_item_name">Name:</td><td><input type="text" name="name" value="<?php echo $name;?>"></td>
</tr>
<tr>
<td class="form_item_name">Company name:</td><td><input type="text" name="company" value="<?php echo $company;?>"></td>
</tr>
<tr>
<td class="form_item_name">Email:</td><td><input type="text" name="email" value="<?php echo $email;?>"></td>
</tr>
<tr>
<td class="form_item_name">Phone:</td><td><input type="text" name="phone" value="<?php echo $phone;?>"></td>
</tr>
<tr>
<td class="form_item_name"></td>
<td>
//this next bit's about autofilling the dropdown to reflect their previous selection
<?php
if (strpos($subject,'General Enquiry') !== false) {
echo '<select name="subject">
<option value="General Enquiry">General Enquiry</option>
<option value="Product/Service Support">Product/Service Support</option>
<option value="Complaint">Complaint</option>
</select>';
}
if (strpos($subject,'Product/Service Support') !== false) {
echo '<select name="subject">
<option value="General Enquiry">General Enquiry</option>
<option value="Product/Service Support" selected="selected">Product/Service Support</option>
<option value="Complaint">Complaint</option>
</select>';
}
if (strpos($subject,'Complaint') !== false) {
echo '<select name="subject">
<option value="General Enquiry">General Enquiry</option>
<option value="Product/Service Support">Product/Service Support</option>
<option value="Complaint" selected="selected">Complaint</option>
</select>';
}
}
?>
</td>
</tr>
<tr>
<td class="form_item_name">Your comments:</td><td><textarea style="resize:none; width:350px;" name="comments" rows="10" wrap="hard"><?php echo $comments ?></textarea><br />
<br />
<input type="submit" value="valid"></form>
所以......你能看到吗?所有帮助表示赞赏!
PS。有没有更快的方法在stackoverflow上将代码放入块中,而不是在每行之前手动添加四个空格?干杯:)
答案 0 :(得分:0)
你可能在这台主机上关闭了register_globals(从安全的角度来看,这是一件非常好的事情 - 请参阅http://php.net/manual/en/security.globals.php!),这解释了为什么这在你的其他主机上工作但不在这里。如果是这种情况,那么您引用的变量在全局范围内不存在。引用发布的变量时使用$ _POST数组。
这在脚本的顶部应该可以正常工作:
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// etc...
答案 1 :(得分:0)
我看不到$ name,$ email等来自哪里。您是否已将$ _POST数据分配给变量? 这样
$name = $_POST['name']
$email = $_POST['email']
$phone = $_POST['phone']
$comments = $_POST['comments']
//etc
答案 2 :(得分:0)
您需要从$ _POST数组中提取变量,以便在PHP中使用它们。您的HTML很好,但要检索表单提交,您必须使用类似于以下内容的代码:
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];