HTML表单与必填字段

3
我可以帮你进行翻译。以下是内容:

我有一个目前正在工作的表格,但想要在“post”文件中添加PHP代码,以便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
}
?>

我希望将姓名、电话和电子邮件字段设置为必填项。
谢谢, Callum
3个回答

5
在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都不支持这个标签!


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: 可以使用

代码可以运行,但似乎有点不直观。也许需要更简单明了的方法?创建一个空字符串数组,检查每个输入值是否存在(并符合业务规则),如果验证失败,则将错误消息添加到字符串数组中,最后如果数组不为空,则停止处理并向用户显示错误信息。 - David
1
@MihirSingh:现在应该可以工作了 :) 但我仍然强烈建议采用更“清洁代码”的方法。$done作为变量名并没有传达太多信息,条件语句也没有真正表达它们的意图。也许我只是挑剔,但值得花费一点额外的努力来使编码世界变得更好 :) - David
1
@David 我实际上正在努力编写更整洁的代码...你一点也不挑剔。我也喜欢整洁的代码 :) - citruspi
不能运行 - 我一定做错了什么。我已经在我的contact.php文件的顶部添加了表单处理器代码。至于HTML页面,我已将其更改为.php,并在<form action="contact.php" ....>之前添加了"表单代码"。 我用<? ?>把"表单代码"包裹起来,还是把整个文件包起来? - Callum
我的原帖中的所有内容都是完整的形式。 - Callum
显示剩余19条评论

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"标签替换为"placeholder",这样即使用户没有清空该字段,它也不会被标记为已填写。


告诉我它是否有效(交叉手指)。没有理由不行...对我有用 :) - citruspi
快到了。你收到邮件了吗?因为我没有收到(我已经正确添加了电子邮件)。 - Callum
是的,我收到了... 邮件功能启用了吗? - citruspi
是的,我想我已经明白了。无论如何,谢谢你,伙计。只是快速地复制你给我的所有代码,我不需要担心你之前在index.php文件中添加的//Form Code,对吧? - Callum
耶!鼓掌 祝你晚安,伙计 :) - citruspi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接