PHP表单不接受数值

时间:2017-09-11 10:25:34

标签: php forms

我从互联网上获得了这个php电子邮件表单代码。

它适用于文本输入字段,但不适用于数字。

我已经尝试将表单上的所有输入类型都放到文本中,我在所有字段中输入错误信息。

所以我将数字字段的输入类型更改为数字和电话等 - 如果我提交数字,仍然会出错。电话号码。

但如果我用文本aaaaaa或gsdsjdaj等进行测试,它就有效。

为什么您认为我的表单将数字视为错误?

我已将所有字段的输入类型保留为文本。

但我确实尝试将它们更改为日期,电话,号码等,但这并没有奏效。

提前致谢。

这是我的代码:

对于php位 - registration_form_email.php

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "aoife@lonedog.co.uk";
    $email_subject = "Website Registration 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['email']) ||
        !isset($_POST['birth_date']) ||
        !isset($_POST['school_year']) ||
        !isset($_POST['add_comments']) ||
        !isset($_POST['mphone']) ||
        !isset($_POST['wphone']) ||
        !isset($_POST['hphone']) ||
        !isset($_POST['alt_name']) ||
        !isset($_POST['alt_tel']) ||
        !isset($_POST['rel_ship']) ||
        !isset($_POST['con_comments']) ||
        !isset($_POST['req_comments']) ||
        !isset($_POST['con_firm'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }        

    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $birth_date = $_POST['birth_date']; // required
    $school_year = $_POST['school_year']; // required
    $add_comments = $_POST['add_comments']; // required
    $mphone = $_POST['mphone']; // required
    $wphone = $_POST['wphone']; // required
    $hphone = $_POST['hphone']; // required
    $alt_name = $_POST['alt_name']; // required
    $alt_tel = $_POST['alt_tel']; // required
    $rel_ship = $_POST['rel_ship']; // required
    $con_comments = $_POST['con_comments']; // required
    $req_comments = $_POST['req_comments']; // required
    $con_firm = $_POST['con_firm']; // 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 Name you entered does not appear to be valid.<br />';
  }

    if(!preg_match($string_exp,$birth_date)) {
    $error_message .= 'The Birth date you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$school_year)) {
    $error_message .= 'The School Year you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$add_comments)) {
    $error_message .= 'The Address you entered does not appear to be valid.<br />';
  }

    if(!preg_match($string_exp,$mphone)) {
    $error_message .= 'The Mobile Phone Number you entered does not appear to be valid.<br />';
  }

    if(!preg_match($string_exp,$wphone)) {
    $error_message .= 'The Work Phone number you entered does not appear to be valid.<br />';
  }

      if(!preg_match($string_exp,$hphone)) {
    $error_message .= 'The Home Phone number you entered does not appear to be valid.<br />';
  }

        if(!preg_match($string_exp,$alt_name)) {
    $error_message .= 'The Alternative Name you entered does not appear to be valid.<br />';
  }

        if(!preg_match($string_exp,$alt_tel)) {
    $error_message .= 'The Alternative Telephone you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$rel_ship)) {
    $error_message .= 'The Relationship to the child you entered does not appear to be valid.<br />';
  }

    if(!preg_match($string_exp,$con_firm)) {
    $error_message .= 'Please tick confirmation to be valid.<br />';
  }


  if(strlen($con_comments) < 2) {
    $error_message .= 'Please fill out any conditions your child may have.<br />';
  }

  if(strlen($req_comments) < 2) {
    $error_message .= 'Please fill out any requirements s your child may have.<br />';
  }


  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Somebody has registered for Ballet Classes see details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Birthdate: ".clean_string($birth_date)."\n";
    $email_message .= "Schoolyear: ".clean_string($school_year)."\n";
    $email_message .= "Mobile: ".clean_string($mphone)."\n";
    $email_message .= "Work: ".clean_string($wphone)."\n";
    $email_message .= "Home: ".clean_string($hphone)."\n";
    $email_message .= "AlternativeName: ".clean_string($alt_name)."\n";
    $email_message .= "AlternativeTel: ".clean_string($alt_tel)."\n";
    $email_message .= "Relationship: ".clean_string($rel_ship)."\n";
    $email_message .= "Conditions: ".clean_string($con_comments)."\n";
    $email_message .= "Requirements: ".clean_string($req_comments)."\n";
$email_message .= "Confirm: ".clean_string($con_firm)."\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);  
?>

<?php

}
?>

和html表单:

<form id="registration" action="registration_form_email.php" method="post">
<div class="col-md-3">
  <fieldset>
    <input name="first_name" type="text" class="form-control" id="first_name" placeholder="Childs Full Name..." required>
  </fieldset>
  </div>
   <div class="col-md-3">
  <fieldset>
    <input name="birth_date" type="text" class="form-control" id="birth_date" placeholder="Childs D.O.B D/M/YYYY..." required>
  </fieldset>
</div>
<div class="col-md-6">
  <fieldset>
    <input name="school_year" type="text" class="form-control" id="school_year" placeholder="Childs School Year..." required>
  </fieldset>
  </div>
<div class="col-md-12">
  <fieldset>
    <textarea name="add_comments" rows="6" class="form-control" id="add_comments" placeholder="Address Including Postcode..." required></textarea>
  </fieldset>
</div>
<div class="col-md-3">
  <fieldset>
    <input name="mphone" type="text" class="form-control" id="mphone" placeholder="Mobile Number in case of Emergencies" required> </fieldset></div>
    <div class="col-md-3">
  <fieldset>
    <input name="wphone" type="text" class="form-control" id="wphone" placeholder="Work Number in case of Emergencies" > </fieldset></div>
    <div class="col-md-3">
  <fieldset>
    <input name="hphone" type="text" class="form-control" id="hphone" placeholder="Home Number in case of Emergencies" > </fieldset></div>
<div class="col-md-3">
  <fieldset>
    <input name="email" type="email" class="form-control" id="email" placeholder="Your email..." required>
  </fieldset>
</div>
 <div class="col-md-3">
  <fieldset>
    <input name="alt_name" type="text" class="form-control" id="alt_name" placeholder="Alternative Contact Name.." required>
  </fieldset>
</div>
<div class="col-md-3">
  <fieldset>
    <input name="alt_tel" type="text" class="form-control" id="alt_tel" placeholder="Alternative Contact Number.." required>
  </fieldset>
</div>
<div class="col-md-3">
  <fieldset>
    <input name="rel_ship" type="text" class="form-control" id="rel_ship" placeholder="Relationship to Child..." required>
  </fieldset>
</div>
<div class="col-md-12">
  <fieldset>
    <textarea name="con_comments" rows="6" class="form-control" id="con_comments" placeholder="Details of any known conditions, allergies etc. (Asthma, Diabetes, Epilepsy)..." ></textarea>
  </fieldset>
</div>
<div class="col-md-12">
  <fieldset>
    <textarea name="req_comments" rows="6" class="form-control" id="req_comments" placeholder="Any other special needs, requirements or directions that would be helpful for us to know..." ></textarea>
  </fieldset>
</div>
<div class="col-md-3">
  <fieldset>
    <h4>I confirm that the above details are correct to the best of my knowledge</h4>
    <input name="con_firm" type="checkbox" required class="checkbox-inline" id="con_firm" value="confirmed">
  </fieldset>
</div>

<div class="col-md-12">
  <fieldset>
    <button type="submit" id="form-submit" class="btn">Register</button>
  </fieldset>
  <br>
</div>

1 个答案:

答案 0 :(得分:0)

你执行这一行:在每个输入的

输入(!preg_match($string_exp,$[INPUT])

如果你查看用于此测试的正则表达式变量$string_exp,它只允许使用字母:"/^[A-Za-z .'-]+$/"(a到z,大写或否,空格和破折号)

对于每个输入,数字更改为if(!preg_match($string_exp,$school_year))if(!is_numeric($school_year)),它应该可以解决问题