mercredi 25 mars 2015

jQuery script isn't posting to database


This is a jQuery script I'm working on to post inputs to a database, assuming the user submitting the data is logged in, and hasn't made an existing entry for the item the user is submitting data for.


Currently it's not producing any results, and I'm not entirely sure why since I don't work with jQuery that often.


Here is a flow chart illustrating what I'm trying to accomplish:


enter image description here



jQuery(document).ready(function() {
var data = {
action: 'is_user_logged_in'
};
jQuery.post(ajaxurl, data, function(response) {
if (response == 'yes') {
// user is logged in
jQuery.ajax({
type: 'POST',
url: MyAjax.ajaxurl,
data: {
"action": "check_db",
"user": user,
"ItemID": ItemID
},
function(data) {
if (data.exists) {
//existing entry found, update entry
function UpdateRecord(id) {
jQuery.ajax({
type: 'POST',
url: MyAjax
.ajaxurl,
data: {
"action": "update_entry",
"ItemID": ItemID,
"Price": Price,
"user": user
},
success: function(
response
) {
alert
(
"Price successfully submitted."
);
}
});
}
} else {
//existing entry not found, create new entry
jQuery("#submit").click(
function() {
var name = jQuery(
"#ItemID").val();
jQuery.ajax({
type: 'POST',
url: MyAjax
.ajaxurl,
data: {
"action": "post_entry",
"ItemID": ItemID,
"Price": Price,
"user": user
},
success: function(
response
) {
alert
(
"Price successfully submitted."
);
}
});
});
});
}
}, 'JSON');
} else {
// user is not logged in
alert("You must be logged in to submit prices.");
}
}
});




Aucun commentaire:

Enregistrer un commentaire