需要勾选复选框以提交php表单

时间:2016-01-22 18:16:16

标签: php html forms validation

我知道经常在这里触及php表单验证,但我完全是php新手,我只是在找到here的php表单脚本,所以我和#39;我不知道如何在这里继续我需要的两件事。

我希望我的表单要求条款和条款条件框在提交之前进行检查(如果未选中该框,则以此脚本处理错误消息的方式返回错误消息)。

我还希望条款&检查条件框是否通过电子邮件发送给我,其余的表单数据。

我尝试过的html的重点是:

<td><input type="checkbox" name="terms-and-conditions" value="terms-and-conditions" /><label for="terms-and-conditions">I have read the <a href="terms.html">Terms & Conditions</a> *</label></td>

我试过的改动的php表单是:

<?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "support@xxxxxxx.com";

    $email_subject = "Order Form";





    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }



    // validation expected data exists

    if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['URL']) ||

        !isset($_POST['date']) ||

        !isset($_POST['comments']) ||

        !isset($_POST['terms-and-conditions'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }



    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $email_from = $_POST['email']; // required

    $URL = $_POST['URL']; // required

    $date = $_POST['date']; // not required

    $comments = $_POST['comments']; // not required

    $terms-and-conditions = $_POST['terms-and-conditions']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }


  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $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 .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "URL: ".clean_string($URL)."\n";

    $email_message .= "Date: ".clean_string($date)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";

    $email_message .= "Terms And Conditions: ".clean_string($terms-and-conditions)."\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);  

?>



<!-- include your own success html here -->

Thank you for submitting the order form.


<?php

}

?>

通过我添加的内容,表单现在会抛出500内部服务器错误。我能够成功地在这个php表单中添加和删除输入字段,但是我不知道如何要求它在提交之前填入一个复选框(如果它已经输出则返回错误消息)没有填写)并通过电子邮件向我发送已填写的其他数据。

1 个答案:

答案 0 :(得分:1)

如果您想验证该复选框是否已勾选并且已经勾选了&#39; (选中)表单发送到服务器之前,你需要一些javascript。 非常(非常!)原油检查看起来像

<form onsubmit="return this.elements['terms-and-conditions'].checked;">

这将(默默地,如此没有反馈)阻止表单提交,如果&#39;条款和条件&#39;复选框未处于选中状态。 如上所述,这是一种非常粗暴的预防措施,根本不可用。 Please see this topic for more elaborate checks and solutions

至于服务器提出500 Internal Server Error的原因: 您不能拥有包含-个字符的变量,因此$terms-and-conditions会引发解析错误。 请考虑使用下划线,例如$terms_and_conditions$termsAndConditions