我有一个目前正在运行的表单,但是想要将php代码添加到“post”文件中,因此需要html表单中的某些字段,否则会显示一条消息。
此刻是我的HTML代码。
<form action="contact.php" method="post">
<input type="text" class="main" name="cf_name" value="Name (Required)" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_company" value="Company" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_phone" value="Phone (Required)" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_email" value="Email (Required)" size="31" />
<br />
<br />
<textarea type="text" name="cf_text0" cols="34" rows="3" class="main">Message</textarea>
<br />
<input type="image" src="images/send.jpg" height="16" width="41" border="0"
alt="Submit Form" />
</form>
并且我的PHP代码
<?php
$field_name = $_POST['cf_name'];
$field_company = $_POST['cf_company'];
$field_phone = $_POST['cf_phone'];
$field_email = $_POST['cf_email'];
$field_message = array($_POST["cf_text0"],);
$mail_to = 'callum@colmandesigns.co.nz';
$subject = 'A & J Print - Contact Form '.$field_name;
$field_message="From: {$field_name}
Company: {$field_company}
Phone: {$field_phone}
Email: {$field_email}
Message: {$field_message[0]}";
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $field_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to craig@colmandesigns.co.nz');
window.location = 'index.html';
</script>
<?php
}
?>
我想要名称,电话和电子邮件字段。
谢谢, 卡勒姆
答案 0 :(得分:4)
在HTML中,您只需输入“required”属性即可生成输入标记! 例如
<input type="text" id="someId" name="IP" placeholder="IP" required tabindex="1">
或
<input type="text" id="someId" name="IP" placeholder="IP" required="true" tabindex="1">
但请注意,IE和Safari直到现在才支持此标记!
答案 1 :(得分:3)
使用PHP:
//Form Processor Code
$filled = true;
$required = array("cf_name", "cf_email", "cf_phone"); //all the required fields
//Cycle through each field and make sure its filled
foreach ($required as &$value) {
if(!isset($_POST[$value])){$filled=false;}
}
//If there are any fields not filled out, send the user back to the form
if (!$filled){
header("Location: http://mysite.com/form.php?error=true");
}
//Form Code
//Notify the user that the form needs to be filled completely
if(isset($_GET['error'])){
echo "Sorry, but the form was not properly filled out.";
}
这个问题是您需要在返回之前将表单值存储在会话中,否则用户将不得不重新输入整个表单。如果你使用Javascript,对用户来说会更好,但要注意禁用JS的浏览器。
使用Javascript:
使用
onBlur
当用户输入字段或使用
时onClick
当用户尝试提交表单时。
可以找到关于此的好教程here。
答案 2 :(得分:2)
// contact.php
<?php
$filled = true;
$required = array("cf_name", "cf_email", "cf_phone"); //all the required fields
//Cycle through each field and make sure its filled
foreach ($required as &$value) {
if($_POST[$value]==""){
$filled = false;
}
}
//If there are any fields not filled out, send the user back to the form
if (!$filled){
header("Location: http://ajprint.co.nz/index.php?error=true");
}
else{
$field_name = $_POST['cf_name'];
$field_company = $_POST['cf_company'];
$field_phone = $_POST['cf_phone'];
$field_email = $_POST['cf_email'];
$field_message = array($_POST["cf_text0"],);
$mail_to = ''; //put your email
$subject = 'A & J Print - Contact Form '.$field_name;
$field_message="From: {$field_name}
Company: {$field_company}
Phone: {$field_phone}
Email: {$field_email}
Message: {$field_message[0]}";
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $field_message, $headers);
if ($mail_status) {
echo "
<script language=\"javascript\" type=\"text/javascript\">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>";
}
else {
echo "
<script language=\"javascript\" type=\"text/javascript\">
alert('Message failed. Please, send an email to craig@colmandesigns.co.nz');
window.location = 'index.html';
</script>";
}
}
?>
//的index.php
<form action="handler.php" method="post">
<input type="text" class="main" name="cf_name" placeholder="Name (Required)" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_company" placeholder="Company" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_phone" placeholder="Phone (Required)" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_email" placeholder="Email (Required)" size="31" />
<br />
<br />
<textarea type="text" name="cf_text0" cols="34" rows="3" class="main">Message</textarea>
<br />
<input type="image" src="images/send.jpg" height="16" width="41" border="0" alt="Submit Form" />
</form>
我用“占位符”替换了所有“value”标签,这样即使用户没有清除该字段,它也不会被标记为已填充