2012-06-30 24 views
0

我正在使用我从另一个创建的网站上更改过的表单,该表单可以正常工作,并且由于某种原因它不在此处工作。我已经完成了一百次代码,并且错过了重要的线索!请帮忙!我的表单遗漏了什么?

该表格包含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 = "[email protected]"; 
$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

是的,只需点击一个按钮,就可以更快地将所有代码放入一个块中。只需选择所有代码并点击编辑器上的{}按钮。这将把选定的文本块合并为代码格式,您不必手动完成。 – Devner

+0

当你在它的时候,确保你正确地编码变量,像这样'value =“<?php echo htmlspecialchars($ company)?>”' –

回答

0

我没有看到其中$名称,$电子邮件等的来源。您是否将$ _POST数据分配给您的变量? 这样

$name = $_POST['name'] 
$email = $_POST['email'] 
$phone = $_POST['phone'] 
$comments = $_POST['comments'] 

//etc 
+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... 
+0

谢谢你,它现在正在工作!我很高兴我已经解释了为什么它是一个工作,而不是其他......可能一直很混乱:)干杯 –

0

你需要拉从$ _POST数组变量,以利用它们在PHP。您的HTML很好,但要检索表单提交,您必须使用类似于以下代码:

$name = $_POST['name']; 
$company = $_POST['company']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$comments = $_POST['comments']; 
+0

谢谢你!得到它了 :) –

相关问题