Как я могу объединить эти 2 сценария PHP? - PullRequest
0 голосов
/ 27 июля 2011

У меня есть форма jQuery AJAX, и я хотел бы, чтобы она лучше проверяла ввод - на стороне PHP.Вот PHP-скрипт, который работает с AJAX:

while (true) {
if (empty($_POST['name'])) {
    $return['error'] = true;
    $return['msg'] = 'You did not enter your name.';
    break;
}

if (empty($_POST['email'])) {
    $return['error'] = true;
    $return['msg'] = 'You did not enter your email.';
    break;
}

if (empty($_POST['message'])) {
    $return['error'] = true;
    $return['msg'] = 'You did not enter your message.';
    break;
}

break;
}

if (!$return['error'])
    $return['msg'] = "Thank you: {$_POST['name']}<br />email address: {$_POST['email']}<br />Your Message: {$_POST['message']}";


echo json_encode($return);

А вот PHP-скрипт, который я использую для проверки в другом месте:

<?php

$subject = "Website Contact Form Enquiry";

//If the form is submitted

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

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'info@bgv.co.za'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}
}
?>

Пожалуйста, может кто-нибудь, кто знает их PHP, поможет мне -Я понятия не имею, как это собрать.

1 Ответ

0 голосов
/ 27 июля 2011

попробуй вот так. я думаю, что один этот код работает хорошо для того, что вы хотите.

$subject = "Website Contact Form Enquiry";

//If the form is submitted

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

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
    $return['msg'] = 'You did not enter your name.';
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
    $return['msg'] = 'You did not enter your email.';
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
    $return['msg'] = 'You did not enter valid email.';
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
   $return['msg'] = 'You did not enter your message.';
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'info@bgv.co.za'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}else{
  echo json_encode($return);

 }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...