简体   繁体   中英

Want php form validation errors to pop up in a window using jquery or javascript

I Tried to figure this one out and I found out that it is not possible with PHP alone. What I would like to know is how I can adapt my current form located at http://www.kaimeramedia.com/derek/Website/contact.php (omit the contact.php to get the full site) using the following mailform.php code to popup an error message if the validation does not hold true. Currently it just refreshes the entire window. I'd rather not refresh the page at all and allow them to fix the error after they close the popup. I have omitted my email on this code, for now but it is on the actual mailform.php page. For further analysis the mailform.php code is as follows:

 <?PHP
 session_start();
try{
$check = new check();
if(!isset($_REQUEST['email']))
    throw new exception('You did not enter an email address.');

if(!isset($_REQUEST['message']))
    throw new exception('You did not enter a message.');

if(!isset($_REQUEST['name']))
    throw new exception('You did not enter a name');

$sender = $_REQUEST['email'];
$message = $_REQUEST['message'];
$name = $_REQUEST['name'];
$recipient = 'text@text.com';

$subject = 'Regarding Your Portfolio';

if($check->captcha('userpass') == FALSE)
    throw new exception('Your captcha is incorrect. Word must be CAPITALIZED');

if($check->spam($sender) == FALSE)
    throw new exception('Your email field contains spam.');

if($check->spam($name) == FALSE)
    throw new exception('Your name field contains spam.');

if($check->length($sender, 10) == FALSE)
    throw new exception('Your email field does not satisfy the minimum character 
count.');

if($check->length($message, 8) == FALSE)
    throw new exception('Your message field does not satisfy the minimum character 
count.');

if($check->length($name, 3) == FALSE)
    throw new exception('Your name field does not satisfy the minimum character 
count.');

mail($recipient, $subject, $message, "From: $name <$sender>" );
include 'thankyou.php';
 }catch (Exception $E){
die($E->getMessage());
}




 class check{

function captcha($field){
    if(isset($_REQUEST[$field])==FALSE){ return false; }
    if($_SESSION['pass'] != $_REQUEST[$field]){ return false; }
    return true;
}

function email($email){
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ return false;}
    return true;
}

function spam($field){
    if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) ||    
eregi("\n",$field) || eregi("%0A",$field)){ return false; }
    return true;
}

function length($field, $min){
    if(strlen($field) < $min){ return false; }
    return true;
}
}
?>

I suggest 2 things - either you AJAX-ify your contact form or have a jQuery form validation plugin to work before your PHP validation.

You can find a lot of websites that showcase a wide variety of contact forms. Here's one: http://www.freewebelements.com/jquery-contact-forms-for-better-contact/

Keep your PHP validation. It will come in handy when JS is disabled (which I doubt anyone would do that at this time where we already have modern browsers), kinda like your safety net.

Validating a form trough AJAX is not safe because data can always be modified after the post is made. That means that you need to double check the data with PHP.

jquery.validationEngine supports everything you want. From alert pop-up's to ajax validation and custom messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM