3

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

我想要填写姓名、电话和电子邮件字段。

谢谢,卡勒姆

4

3 回答 3

5

在 HTML 中,您只需添加“必需”属性即可使输入标签成为必需!例如

<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 直到现在才支持这个标签!

于 2013-04-23T05:09:29.703 回答
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

当用户尝试提交表单时。

可以在这里找到一个很好的教程。

于 2012-04-18T23:32:09.117 回答
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>

我用“占位符”替换了所有“值”标签,这样即使用户没有清除该字段,它也不会被标记为已填充

于 2012-04-19T01:24:13.650 回答