PHP / JS联系表 - 验证

时间:2014-08-11 02:29:53

标签: javascript php contact-form

我无法使用此表格。我所做的一切都只是出现了错误,并且说电话号码无效,但是当我查看代码时,我真的无法理解为什么它不起作用。

P.S。我对PHP或JS没有任何了解,所以在发生错误时,我真的不知道自己在寻找什么。

感谢您的帮助。

这是HTML

<form action="contact.php" method="post" id="cform" name="cform">
          <ul id="homehireus" class="hireform contactform">
            <li>
              <label>Name:<span class="required">*</span></label>
              <input name="name" id="name" type="text" value="" tabindex="1">
            </li>
            <li>
              <label>Phone:</label>
              <input name="phone" id="phone" type="text" value="" tabindex="2">
            </li>
            <li>
              <label>Email:<span class="required">*</span></label>
              <input name="email" id="email" type="text" value="" tabindex="3">
            </li>
            <li>
              <label>Subject:</label>
              <input name="subject" id="subject" type="text" value="" tabindex="4">
            </li>
            <li>
              <label>Message:<span class="required">*</span></label>
              <textarea name="message" id="message" tabindex="5"></textarea>
            </li>
            <li>
              <input type="button" id="send-message" value="Send Details To iPhone Repairs" tabindex="6">
              <div id="output" class="contactpage-msg"></div>
            </li>

          </ul>
        </form>

PHP

 <?php 

$send_email_to = "naomi.deluca@me.com";

function send_email($name,$email,$phone,$subject,$message)
{
  global $send_email_to;  
  if($message=='message')$message='';
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";
  $message = "<strong>Email = </strong>".$email."<br>";  
  $message .= "<strong>Name = </strong>".$name."<br>";     
  $message .= "<strong>Phone = </strong>".$phone."<br>";     
  $message .= "<strong>Message = </strong>".$message."<br>";
  @mail($send_email_to, $subject, $message,$headers);
  return true;
}

function validate($name,$email,$phone,$message,$subject)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';  
  $return_array['email_msg'] = '';
  $return_array['phone_msg'] = '';
  $return_array['message_msg'] = '';
  $return_array['subject_msg'] = '';

 if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'email is required';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'Enter valid email.';  
    }
  }

  if($name == '')
  {
    $return_array['success'] = '0';
    $return_array['name_msg'] = 'Name is required';
  }
  else
  {
     $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $name)) {
      $return_array['success'] = '0';
     $return_array['name_msg'] = 'Enter valid Name.';
    }
  }

  if($phone == '')
  {
    $return_array['success'] = '0';
    $return_array['phone_msg'] = 'Phone is required';
  }
  else
  {
    $string_exp = "/^[A-Za-z .'-]+$/";
     if (!preg_match($string_exp, $phone)) {
       $return_array['success'] = '0';
     $return_array['phone_msg'] = 'Enter valid Phone.';
    }
  }

  if($subject == '')
  {
    $return_array['success'] = '0';
    $return_array['subject_msg'] = 'Subject is required';
  }

  if($message == '')
  {
    $return_array['success'] = '0';
    $return_array['message_msg'] = 'Message is required';
  }
  else
  {
    if (strlen($message) < 2) {
      $return_array['success'] = '0';
      $return_array['message_msg'] = 'Enter valid Message.';
    }
  }  

  return $return_array;
}

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];

$return_array = validate($name,$email,$phone,$message,$subject);
if($return_array['success'] == '1')
{
  send_email($fname,$email,$phone,$subject,$message);
}

header('Content-type: text/json');
echo json_encode($return_array);
die();

?>

JS

$(document).ready(function () {
    $('div#output').hide();
    //bind send message here
    $('#send-message').click(sendMessage);
    $('button.close').live('click', function () {
        $(this).parent().find('p').html('');
        $(this).parent().hide();
    });
});

/* Contact Form */
function checkEmail(email) {
    var check = /^[\w\.\+-]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/;
    if (!check.test(email)) {
        return false;
    }
    return true;
}

function sendMessage() {

    // receive the provided data
    var name = $("input#name").val();
    var email = $("input#email").val();
    var subject = $("input#subject").val();
    var phone = $("input#phone").val();
    var message = $("textarea#message").val();

    message = 'message';
    // check if all the fields are filled
    if (name == '' || phone == '' || email == '' || subject == '' || message == '') {
        $("div#output").show().html('<button type="button" class="close" data-dismiss="alert-close">x</button><p class="alert-close">You must enter all the fields!</p>');

        return false;
    }

    // verify the email address
    if (!checkEmail(email)) {
        $("div#output").show().html('<button type="button" class="close" data-dismiss="alert">x</button><p>Please enter a valid Email Address</p>');
        return false;
    }

    // make the AJAX request
    var dataString = $('#cform').serialize();
    $.ajax({
        type: "POST",
        url: 'contact.php',
        data: dataString,
        dataType: 'json',
        success: function (data) {
            if (data.success == 0) {
                var errors = '<ul><li>';
                if (data.name_msg != '')
                    errors += data.name_msg + '</li>';
                if (data.email_msg != '')
                    errors += '<li>' + data.email_msg + '</li>';
                if (data.phone_msg != '')
                    errors += '<li>' + data.phone_msg + '</li>';
                if (data.message_msg != '')
                    errors += '<li>' + data.message_msg + '</li>';
                if (data.subject_msg != '')
                    errors += '<li>' + data.subject_msg + '</li>';

                $("div#output").removeClass('alert-success').addClass('alert-error').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p> Could not complete your request. See the errors below!</p>' + errors);
            }
            else if (data.success == 1) {

                $("div#output").removeClass('alert-error').addClass('alert-success').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p>You message has been sent successfully!</p>');
            }

        },
        error: function (error) {
            $("div#output").removeClass('alert-success').addClass('alert-error').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p> Could not complete your request. See the error below!</p>' + error.statusText);
        }
    });

    return false;
}

4 个答案:

答案 0 :(得分:0)

验证名称的正则表达式与验证电话号码的正则表达式相同,非常奇怪,不是吗?

要验证各种电话号码,您只需检查$phone是否仅包含ctype_digit()的数字。

答案 1 :(得分:0)

如何使用

{p> /^[0-9]+$/而不是/^[A-Za-z .'-]+$/的{​​{1}}?

$string_exp = "/^[A-Za-z .'-]+$/";代替if (preg_match($string_exp, $phone)) {

答案 2 :(得分:0)

您用于验证电话号码的正则表达式:

/^[A-Za-z .'-]+$/

不接受数字。鉴于电话号码通常包含数字,此正则表达式在此上下文中很可能无效。

答案 3 :(得分:0)

验证电话号码的正则表达式不正确。 替换行:

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

使用

$string_exp = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";

上述正则表达式接受以下格式的电话号码:

555-555-5555
5555425555
555 555 5555
1(519) 555-4444
1 (519) 555-4422
1-555-555-5555
1-(555)-555-25555