php - My contact form is working but it won't show the success message -
my contact form working success message doesn't show. developer wrote php code did of jquery , won't display message. i'm pretty sure problem jquery. i'm particularly confused line of code...
$("#result").hide().html(output).slidedown(); }, 'json'); }
i think want utilize .show() since #result needed show html didn't seem trick. total code below.
$(function(){ $(".btn").click(function() { //get input field values var user_name = $('input[name=name]').val(), user_email = $('input[name=email]').val(), user_message = $('textarea[name=message]').val(), proceed = true; //simple validation @ client's end //we alter border color reddish if empty field using .css() if(user_name===""){ $('input[name=name]').css('border','2px solid red').after('<p class="error-msg">(what\'s name?)</p>'); proceed = false; } if(user_email===""){ $('input[name=email]').css('border','2px solid red').after('<p class="error-msg">(please provide valid email)</p>'); proceed = false; } if(user_message==="") { $('textarea[name=message]').css('border','2px solid red').after('<p class="error-msg">(please allow me know enquiry about)</p>'); proceed = false; } //everything looks good! proceed... if(proceed) { //data sent server post_data = {'username':user_name, 'useremail':user_email, 'usermessage':user_message}; //ajax post info server $.post('../../contact-form.php', post_data, function(response){ //load json info server , output message if(response.type === 'error') { output = '<div class="error">'+response.text+'</div>'; }else{ output = '<div class="success">'+response.text+'</div>'; alert('thanks message'); //reset values in input fields $('.form-container input').val(''); $('.form-container textarea').val(''); } $("#result").hide().html(output).slidedown(); }, 'json'); } }); //reset set border colors , hide message on .keyup() $(".form-container input, .form-container textarea").keyup(function() { $(".form-container input, .form-container textarea").css('border-color',''); $("#result").slideup(); }); });
php code
<?php if($_post)
{ $to_email = "alexechaparro@gmail.com"; //replace recipient email address $subject = 'alexsdogvacay.com'; //subject line emails
//check if ajax request, exit if not if(!isset($_server['http_x_requested_with']) , strtolower($_server['http_x_requested_with']) != 'xmlhttprequest') { //exit script outputting json info $output = json_encode( array( 'type'=>'error', 'text' => 'request must come ajax' )); die($output); } var_dump($_request); //check $_post vars set, exit if missing if(!isset($_post["username"]) || !isset($_post["useremail"]) || !isset($_post["usermessage"])) { echo $_post["username"]; $output = json_encode(array('type'=>'error', 'text' => 'input fields empty!')); die($output); } //sanitize input info using php filter_var(). $user_name = filter_var($_post["username"], filter_sanitize_string); $user_email = filter_var($_post["useremail"], filter_sanitize_email); $user_message = filter_var($_post["usermessage"], filter_sanitize_string); //additional php validation if(strlen($user_name)<4) // if length less 4 throw http error. { $output = json_encode(array('type'=>'error', 'text' => 'name short or empty!')); die($output); } if(!filter_var($user_email, filter_validate_email)) //email validation { $output = json_encode(array('type'=>'error', 'text' => 'please come in valid email!')); die($output); } if(strlen($user_message)<5) //check emtpy message { $output = json_encode(array('type'=>'error', 'text' => 'too short message! please come in something.')); die($output); } //proceed php email. /* incase host allows emails local domain, should un-comment first line below, , remove sec header line. of-course need come in own email address here, exists in cp. */ //$headers = 'from: your-name@your-domain.com' . "\r\n" . $headers = 'from: '.$user_email.'' . "\r\n" . //remove line if line above un-commented 'reply-to: '.$user_email.'' . "\r\n" . 'x-mailer: php/' . phpversion(); // send mail service $sentmail = @mail($to_email, $subject, $user_message .' -'.$user_name, $headers); if(!$sentmail) { $output = json_encode(array('type'=>'error', 'text' => 'could not send mail! please check php mail service configuration.')); die($output); }else{ $output = json_encode(array('type'=>'message', 'text' => 'hi '.$user_name .' give thanks email. possible.')); die($output); }
} ?>
so, line of code that's confusing: $('#result').hide().html().slidedown();
works. no need changing that. 1 problem see variable output
isn't beingness defined var
making global variable.
that aside, php code seems kill success response die
function, bc doesn't homecoming output. alter this:
if(!$sentmail) { $output = json_encode(array('type'=>'error', 'text' => 'could not send mail! please check php mail service configuration.')); die($output); }else{ $output = json_encode(array('type'=>'message', 'text' => 'hi '.$user_name .' give thanks email. possible.')); die($output); }
to:
if(!$sentmail) { $output = json_encode(array('type'=>'error', 'text' => 'could not send mail! please check php mail service configuration.')); }else{ $output = json_encode(array('type'=>'message', 'text' => 'hi '.$user_name .' give thanks email. possible.')); } echo $output;
hope helps!
t
php jquery ajax forms
No comments:
Post a Comment