联系表格php验证邮政编码

问题描述 投票:-1回答:1

您好,我是php的新秀,虽然我应该想象,但这可能是基本的。我想验证此联系表的邮政编码,请问如何实现?另外,如果可能的话,可以从用户在表单上输入的电子邮件中发送表单吗?

    <?php
if(isset($_POST["action"])) {
    $name = $_POST['name'];        // Sender's name
    $email = $_POST['email'];      // Sender's email address
    $phone  = $_POST['phone'];     // Sender's phone number
    $postcode  = $_POST['postcode'];     // Sender's postcode
    $availability  = $_POST['availability'];     // Sender's availability
    $message = $_POST['message'];  // Sender's message
    $headers = 'From: Accelerate Driving School <[email protected]>' . "\r\n";

    $to = '[email protected]';     // Recipient's email address
    $subject = 'Enquiry from Accelerate website '; // Message title

    $body = " From: $name \n E-Mail: $email \n Phone : $phone \n Postcode : $postcode \n Availability : $availability \n Message : $message"  ;

    // init error message
    $errmsg='';
    // Check if name has been entered
    if (isset($_POST['name']) && $_POST['name'] == '') {
        $errmsg .= '<p>Please enter your name.</p>';
    }
    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errmsg .= '<p>Please enter a valid email address.</p>';
    }
    //Check if phone number has been entered
    if ( isset($_POST['phone']) && $_POST['phone'] == '') {
        $errmsg .= '<p>Please enter your phone number.</p>';
    }

    //Check if postcode number has been entered
    if ( isset($_POST['postcode']) && $_POST['postcode'] == '') {
        $errmsg .= '<p>Please enter your postcode.</p>';
    }
        //Check if availability has been entered
    if ( isset($_POST['availability']) && $_POST['availability'] == '') {
        $errmsg .= '<p>Please enter your availability.</p>';
    }

    //Check if message has been entered
    if ( isset($_POST['message']) && $_POST['message'] == '') {
        $errmsg .= '<p>Please enter your message.</p>';
    }

    /* Check Google captch validation */
    if( isset( $_POST['g-recaptcha-response'] ) ){
        $error_message = validation_google_captcha( $_POST['g-recaptcha-response'] );
        if($error_message!=''){
            $errmsg .= $error_message;
        }
    }   

    $result='';
    // If there are no errors, send the email
    if (!$errmsg) {
        if (mail ($to, $subject, $body, $headers)) {
            $result='<div class="alert alert-success">Thank you for contacting Accelerate Driving School. Your message has been successfully sent. We will contact you very soon!</div>';
        }
        else {
          $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
    else{
        $result='<div class="alert alert-danger">'.$errmsg.'</div>';
    }
    echo $result;
 }

/*
 * Validate google captch
 */
function validation_google_captcha( $captch_response){

    /* Replace google captcha secret key*/
    $captch_secret_key = '6Leuf60UAAAAADwKeB3TlvrtKfoYvyNuNDzn87M1';

    $data = array(
            'secret'   => $captch_secret_key,
            'response' => $captch_response,
            'remoteip' => $_SERVER['REMOTE_ADDR']

我的表格在这里:https://www.robsteele.co.uk/devdrive/

非常感谢您的帮助。谢谢罗伯

php forms validation contacts
1个回答
0
投票

[如果您只想验证“邮政编码”值,请在测试中使用正则表达式:

5位长(美国)

if(isset($ _ POST ['postcode'])&&!preg_match('/ ^ \ d {5} $ /',$ _POST ['postcode'])){

英国邮政编码区域(有效代码的连续列表(|| ...)

if(isset($ _ POST ['postcode'])&&!preg_match('/ ^(AB | AL | B | BA | BB | BD | ...)$ / i',$ _POST ['postcode'])){

调整preg_match中的正则表达式以匹配要验证的值的类型。

您可能希望再次查看您的代码。确定传递了一个“动作”后,您要为每个字段设置单独的变量(例如$ postcode = $ _POST ['postcode'];),但不检查是否有一个“邮政编码”(或字段)在_POST数组中。再往下而不是使用field变量,您要检查_POST数组是否设置了“邮政编码”。问题是,如果未设置,您将已经发出警告,试图访问在设置字段变量的位置未定义的索引。也许将您正在执行的操作更好的方法是将字段变量设置为:

$ postcode = isset($ _ POST ['postcode'])? $ _POST ['postcode']:“”; //“”;如果未传递,则为默认值

并简化使用变量的验证条件:

if(!preg_match('/ ^ \ d {5} $ /',$ postcode)){

© www.soinside.com 2019 - 2024. All rights reserved.