2011-07-27 2 views
0

나는 jQuery AJAX 형식을 가지고 있으며, PHP 쪽에서 입력에 대해 더 나은 유효성 검사를 수행하고 싶습니다. 여기어떻게이 2 개의 PHP 스크립트를 병합 할 수 있습니까?

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 스크립트입니다 : 여기에 AJAX와 함께 작동하는 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._%-][email protected][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 = '[email protected]'; //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를 알고있는 누군가가 나를 도울 수 제발 - 내가 해달라고 이걸 어떻게 집어 넣을 지 단서.

답변

0

시도해보십시오. 나는이 코드만으로도 당신이 원하는 것을 잘 처리 할 수 ​​있다고 생각한다.

$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._%-][email protected][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 = '[email protected]'; //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); 

} 
} 
+0

Hiay Chandresh - 우수 감사합니다. :) – brett

관련 문제