Reputation: 818
I have this ajax call at the moment:
$.ajax({
url: "misc/sendPM.php",
type: "POST",
data: data,
success: function(stuff) {
if (typeof stuff == "object") {
var err = confirm(stuff.error);
if (err) {
alert('You pressed OK'); // MAKE AJAX CALL HERE HERE HERE HERE
} else {
$("#pmResponse").text("Mottagare saknas!").show().fadeOut(4500);
$('#sendPM_btn').attr('disabled', false);
$('#txt').attr('readonly', false);
$('#title').attr('readonly', false);
}
} else {
$(stuff).prependTo('#PrivateMessages').fadeIn(1000);
$('#txt').val("");
$("#pmResponse").text("Skickat!").show().fadeOut(4500);
$('#sendPM_btn').attr('disabled', false);
$('#txt').attr('readonly', false);
$('#title').attr('readonly', false);
}
}
});
I am making an "Did you mean?" If the user hasn't typed in right name for the recipient. The "did you mean name" comes in a confirm, so you could press OK or Cancel -> to type again a new name.
If you press ok right now, you only get a alert (see above code). But what I want to do when you press OK is make a change in 'recipient' data name, and send back the same data. Is this possible? How?
So conclusion what I am doing now is:
Sending request to sendPM.php with data, sendPM.php grabs the data and if it finds the recipient name is unclear(eg. Megan F) then it returns JSON "error" with: Did you mean Megan Fox?, the $.ajax on success grabs it, and make it as a confirm box. If you press OK on the confirm box(if you did mean megan fox), then it should send back "Megan Fox" to sendPM.php along with all the previous data, In order to complete the process fully.
Is there a better way to do this?
If no, how can I then send back "megan fox" to sendPM.php along with the previous data in order to complete the process fully?
Upvotes: 0
Views: 643
Reputation: 561
Make them into functions instead.
function sendRequest(data) {
$.ajax({
...
data: data
success: process
});
}
function process(stuff) {
...
if(err && confirm("Did you mean " + stuff.suggestion + "?")) {
sendRequest([make your new data here]);
}
...
}
Upvotes: 1