Php 带有必填字段的html表单

Php 带有必填字段的html表单,php,forms,Php,Forms,我有一个表单,目前正在工作,但希望将php代码添加到“post”文件中,因此html表单中的某些字段是必需的,否则会显示一条消息 这是我目前的html代码 <form action="contact.php" method="post"> <input type="text" class="main" name="cf_name" value="Name (Required)" size="31" /> <br /> <

我有一个表单,目前正在工作,但希望将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
}
?>

警报('谢谢您的消息。我们将很快与您联系');
window.location='index.html';
警报('消息失败。请发送电子邮件至craig@colmandesigns.co.nz');
window.location='index.html';
我想有姓名,电话和电子邮件字段的要求

谢谢,
Callum使用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:

//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.";
}
任用

onBlur
当用户在字段中完成键入或使用

onClick
当用户尝试提交表单时

您可以找到一个很好的教程。

//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>";
        }
    }

?>
在HTML中,您只需输入“required”属性,即可使输入标记成为必需的!
比如说

<input type="text" id="someId" name="IP" placeholder="IP" required tabindex="1">



<>但是考虑一下,IE和Safari到现在为止还没有支持这个标签! 代码可以工作,但似乎有点不直观。也许更简单一点?创建一个空字符串数组,检查每个输入值是否存在(并且符合业务规则),如果验证失败,则向字符串数组中添加一条错误消息,如果数组不为空,则在结束时停止处理并向用户显示错误。@David自您读取后,该值已被更新,但我只是想给他一个如何做的想法…事实上,因为你的更新代码将不再工作
$done
只有在最后一个值验证失败的情况下才会为false。在您的示例中,如果
var1
验证失败,但
var2
通过验证,则代码不会对此作出响应。@MihirSingh:现在应该可以了:)但我仍然强烈建议采取更“干净的代码”方法
$done
作为一个变量名并没有传达太多信息,并且条件并没有真正表达它们的意图。也许我只是吹毛求疵,但为了让这个世界成为一个更好的编码之地,付出一点额外的努力是值得的:)@David我实际上是在编写更干净的代码。。。你一点也不挑剔。我也喜欢干净的代码:)告诉我它是否有效(交叉手指)。没有理由它不应该。。。为我工作:)快到了。你收到电子邮件了吗?因为我没有(我已经正确地添加了电子邮件)是的,我收到了他们。。。邮件功能启用了吗?是的,我想我收到了。谢谢你,伙计。很快我就复制了你给我的所有代码,我不需要担心你刚才在index.php文件中添加的//表单代码?耶!拍手祝你有一个美好的夜晚,巴德:)
<input type="text" id="someId" name="IP" placeholder="IP" required="true" tabindex="1">