javascript - Json jQuery and php doesn't process value's correct -
i have : html + jquery + ajax post , php file process form values , returning error(true or false) , message html markups.
my javascript code:
$(document).ready(function() { var form = $('#form'); // contact form var submit = $('#submit'); // submit button var alert = $('.alert'); // alert div show alert message // form submit event form.on('submit', function(e) { e.preventdefault(); // prevent default form submit $.ajax({ url: 'contact.php', // form action url type: 'post', // form submit method get/post datatype: 'json', // request type html/json/xml data: form.serialize(), // serialize form data beforesend: function() { alert.fadeout(); submit.html('sending....'); // change submit button text }, success: function(result) { if(result.error){ /* on error stuff */ alert(result.html).fadein(); }else{ /* on success stuff */ alert(result.html).fadein(); } } }); }); });
and @ last php:
if( isset( $_server['http_x_requested_with'] ) ){ $result = array("error" => false, "html" => null); $vars = array('name', 'email','telefoonnummer', 'message'); $verified = true; foreach($vars $v) { if(!isset($_post[$v]) || empty($_post[$v])) { $verified = false; } } if(!$verified) { $result["error"] = true; $result["html"] = "<b>error11</b>"; echo json_encode($result); exit; } $name = filter_var($_post['name'], filter_sanitize_string); $email = filter_var($_post['email'], filter_sanitize_email); $tel= filter_var($_post['telefoonnummer'], filter_sanitize_string); $message = filter_var($_post['message'], filter_sanitize_string); $to = ''; $sent = email($to, $email, $name, $tel, $message); if ($sent) { $result["error"] = false; $result["html"] = "<b>success</b>"; echo json_encode($result); } else { $result["error"] = true; $result["html"] = "<b>error</b>"; echo json_encode($result); } return; } /** * email send headers * * @return bool | void **/ function email($to, $name, $email, $tel, $message){ $header = array(); $header[] = "mime-version: 1.0"; $header[] = "from: <".$name."> <".$email.">"; /* set message content type html*/ $header[] = "content-type:text/html; charset=iso-8859-1"; $header[] = "content-transfer-encoding: 7bit"; if( mail($to, $tel, $message, implode("\r\n", $header)) ) return true; }
ok.. that's clear know goes wrong error returning, show , goes away again n html don't know happens there..
i don't want "fixes" can copy paste code explanation of happened , goes wrong , how solve (at least learn little)
you using alert
in 2 different ways... 1 time object, 1 time function. latter causes undesired effect. closely @ brackets after alert
;
alert()
functionalert.
signafies it's object
Comments
Post a Comment