我仍然是非常新的PHP所以这对我来说是一场挣扎。我现在需要这样做,以便在表单中出现一条消息,说谢谢。
这是链接:www.pamper-pad.co.uk/contact.php代码如下:
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "joxxorz@me.com";
$email_subject = "Enquiry for Pamper Pad";
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<?php
}
?>
HTML:
<form name="contactform" method="post" style="font-size:14px; height:35px;">
<table width="250px">
<tr>
<td height="25" valign="top">
<label for="first_name" style="height:35px;"><span class="boldp">Name <span class="asterisk">*</span></span></label>
</td>
</tr>
<tr>
<td height="50" valign="top">
<input class="formbox" type="text" name="name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td height="25" valign="top">
<label for="email" style="height:35px;"><span class="boldp">Telephone Number/Email Address <span class="asterisk">*</span></span></label>
</td>
</tr>
<tr>
<td height="50" valign="top">
<input class="formbox" type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td height="25" valign="top">
<label for="comments"><span class="boldp">Message <span class="asterisk">*</span></span></label>
</td>
</tr>
<tr>
<td height="119" valign="top">
<textarea class="formbox" name="comments" maxlength="1000" cols="38" rows="6"></textarea>
</td>
</tr>
<tr>
<td height="24" colspan="2" style="text-align:right">
<input class="submit" type="submit" value="Submit">
</td>
</tr>
</table>
</form>
有人可以建议我如何用一条消息替换这个表格吗?
编辑:
<?php
if (!isset($_POST['email'])){
//code to display your form here
//for example
echo "<div class="formthing">
<form name="contactform" method="post" style="font-size:14px; height:35px;">
<table width="250px">
<tr>
<td height="25" valign="top">
<label for="first_name" style="height:35px;"><span class="boldp">Name <span class="asterisk">*</span></span></label>
</td>
</tr>
<tr>
<td height="50" valign="top">
<input class="formbox" type="text" name="name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td height="25" valign="top">
<label for="email" style="height:35px;"><span class="boldp">Telephone Number/Email Address <span class="asterisk">*</span></span></label>
</td>
</tr>
<tr>
<td height="50" valign="top">
<input class="formbox" type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td height="25" valign="top">
<label for="comments"><span class="boldp">Message <span class="asterisk">*</span></span> </label>
</td>
</tr>
<tr>
<td height="119" valign="top">
<textarea class="formbox" name="comments" maxlength="1000" cols="38" rows="6"></textarea>
</td>
</tr>
<tr>
<td height="24" colspan="2" style="text-align:right">
<input class="submit" type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>";
}elseif(isset($_POST['email'])){
//code to display thank you message here
//for example
echo "<div>Thank you for submitting your email!</div>";
}
?>
答案 0 :(得分:1)
将表单放入div和另一个div中,并带有感谢信息。
然后检查
<?php if(isset($_POST['name) && email etc){
echo '<div class="thank you"> Thank you </div>}
else
{
echo '<div class="divform"> form </div>
}
?php>
答案 1 :(得分:0)
您应该始终清理帖子值。
例如:
$email_from = $_POST['email']; // required
应该是:
$email_from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL)
要进一步消毒并检查你可以:
if ($_POST['email'] == $email_from && filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
$verified_email_from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}
虽然@PeeHaa可能会进一步指定。
对于这些:
$first_name = $_POST['name']; // required
$comments = $_POST['comments']; // required
您可以使用:
$first_name = filter_var($_POST['name'], FILTER_SANITIZE_STRING)
$comments= filter_var($_POST['comments'], FILTER_SANITIZE_STRING)
您可以在PHP.NET阅读有关消毒的更多信息,并且有一个很好的教程here
除了@terisL声明你可以使用post var根据状态显示不同的页面。您将使用PHP构建每个页面,它将根据帖子状态选择显示。
<?php
if (!isset($_POST['email'])){
//code to display your form here
//for example
echo "<form name='contactform' method='post' style='font-size:14px; height:35px;'>
<table width='250px'>
<tr>
<td height='25' valign='top'>
<label for='first_name' style='height:35px;'><span class='boldp'>Name <span class='asterisk'>*</span></span></label>
</td>
</tr>";
}elseif(isset($_POST['email'])){
//code to display thank you message here
//for example
echo "<div>Thank you for submitting your email!</div>";
}
?>