Форма PHP не принимает числовые значения

Я получил этот код электронной почты 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

Решение

вы выполняете эту строку:(!preg_match($string_exp,$[INPUT]) на каждый вход вставлен

если вы посмотрите на $string_expпеременная regex, которую вы используете для этого теста, она допускает только буквы: "/^[A-Za-z .'-]+$/" (от А до Я, пробелы или нет, пробелы и тире)

для каждого входа, который является изменением числа if(!preg_match($string_exp,$school_year)) в if(!is_numeric($school_year)) и это должно сделать свое дело

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector