javascript - Unable to print success message using Jquery Ajax -
I am using junky Ajax to post a form data and display success message. Everything is fine, but I am not able to display success message. The code below is: Javascript
& lt; Script & gt; $ (Document) .ready (function () {$ ('form'). Submit (function (event) {// Submit to Trigger Form $ ('# stage'). Empty (); var postForm = {// Val (), "element_4_2": $ ("# element_4_2"). Val (), "data": $ ("#name"). "Element_4_3": $ ("# element_4_3" ) Val (), "email": $ ("# email"). Val (), "input4": $ ("# input4"). Val (),}; $ .ajax ({// $ .ajax Process the form using the () type: 'POST', // method type URL: 'contact.php', // Your form processing file URL data: Type the postform, // form name data: 'Jason' , Success: Work (data) {console.log ("Success" 3 "inside), warning (data); $ (" # step ") .html (data); if (! Data.success) {// if fails (data. Errors) {// any Error returned to Process.php $ ('. Throw_error'). FadeIn (1000) .html (data.errors); // related error throw console.log ("inside failure");}} and {console. Log ("inside success"); $ ('# step'). Feedin (1000) .append ('& lt; p & gt;' + data.posted + '& lt; / p & gt;'); Console.log ("Inside success 2"); }}}); Event.preventDefault (); // stop default submit}); }); & Lt; / Script & gt;
PHP:
& lt ;? Php ini_set ('display_errors', 'on'); Error_reporting (E_ALL); $ Errors = Array (); $ Form_data = array (); Header ('content-type: application / jason'); Echo json_encode ($ form_data); $ Name = $ _ post ['name']; $ Phone = Cut ($ _ Post ['element_4_1']); . $ Phone = Cut ($ _ Post ['element_4_2']); . $ Phone = Cut ($ _ Post ['element_4_3']); $ Email = delete ($ _ post ['email']); $ Message1 = Cut ($ _ Post ['Input4']); If ($ name & amp; amp; amp; phone & amp; e-mail;) {$ header = "MIME-version: 1.0 \ r \ n"; $ Headers = "Content-type: text / plain; charset = ISO-8859-1 \ r \ n"; $ Headers = "To: sales@test.com \ n"; $ Recipient = "test@test.in"; $ Theme = "online inquiry"; $ Message = "\ nName: $ name \ n"; $ Message = "\ N Phone: $ phone \ n"; $ Message = "\ N Email ID: $ email \ n"; $ Message = "Message \ n $ message1 \ n"; // Send auto answer $ subject_reply = "Thank you for contacting us"; $ Message_reply = "Thank you for contacting us. We'll be back to you soon."; // mail ($ email, $ subject_reply, $ message_reply, $ header); // Send Mail // ========== If (isset ($ recipient, $ theme, $ message, $ header)) {error_log ($ message); $ Form_data ['status'] = 'success'; Error_log ($ form_data ['status']); } And {$ form_data ['status'] = 'error'; Error_log ($ form_data ['status']); }? & Gt;
HTML
& lt; Div id = "stage" & gt; & Lt; / Div & gt;
I have a message of success
how can you print it At the beginning of your PHP script:
echo json_encode ($ form_data);
Where $ form_data is an empty array at that time.
You should remove it and keep it at the end.
Comments
Post a Comment