php - Using returned data to perform additional javascript tests with ajax -
i have mail.php file gets called ajax script. completeness, ajax script attached below. mail.php file perform server-side validation, , if passes, sends email mail() , data sent ajax request.
what want perform additional javascript actions based on response of request. can see @ current state, response of request echo'd screen, fine. want modify html elements based on response.
for example, want append image called "ok" page if mail sent, , else append image called "false" page if mail not sent. i'd (in pseudocode) this:
if ( request okay ) $('.contact').append('<img src"ok"'); else ( $('.contact').append('<img src="bad''); is there way perform this?
thanks!
the ajax script shown below:
$('.submit').click(function() { $('div.load').html('<img src="images/load.gif" alt="loading..." id="loading" />'); //creation of variables send var name = $('#name').val(); email = $('#email').val(); phone = $('#phone').val(); $.ajax({ url: 'mail.php', type: 'post', data: 'name=' + name + '&email=' + email + '&phone=' + phone, success: function(result) { $('p.error,p.correct').remove(); $('.contact').append(result); $('#loading').fadeout(500, function() { $(this).remove(); }); } }); return false; });
assuming php responds text "okay":
if (result==="okay") $('.contact').append('<img src"ok" />'); else $('.contact').append('<img src="bad" />'); or:
$('.contact').append('<img src"' + (result==="okay"?'ok':'bad') + '" />'); an alternative update php returns html includes both message , image, e.g., on success return:
your request successful. <img src="ok" /> "i should mention i'd keep current response there make sure non-js users appropriate message"
non-js users not getting because ajax code uses js...
Comments
Post a Comment