I am using a Twitter bootstrap plugin called 'Bootbox' that shows a modal form. I only want the modal form to show up if there is a 'popup' id in a mysql database. Otherwise, I don't want the function to run at all.
Here's what I have:
var popupid = <?php if ($contact->find_popup()) { echo $contact->popup()->id; } ?>;
if(popupid) {
bootbox.dialog({
message: "<?php if ($contact->find_popup()) {
echo $contact->popup()->message;
};
?>",
title: "Contact Pop-Up",
buttons: {
danger: {
label: "Delete...",
className: "red",
callback: function() {
$.ajax({
url: "ajax_delete.php?table=popups&id=" + popupid,
type: "POST",
dataType: 'json',
success: function(response) {
//response here if data response
if (response) {
toastr.info('Successfully deleted popup!');
}
}
});
}
},
main: {
label: "Ok!",
className: "blue",
callback: function() {
}
}
}
});
}
I set a variable called popup that see is there is a popup id present in my DB. My find_popup() method returns true if there is one and false otherwise. If it returns true, the popupid should equal the echoed id I need.
The popup id is then passed into the ajax URL as you can see. I use it to run a delete script that removes the popup if the user selects "Delete...".
Everything works fine right now IF and ONLY IF there is a popup present. If not, my page doesn't work properly. I think it's because the bootbox.dialog is still called.
Maybe I wrote this wrong?
-1when there's no entry in your DB ? Then check for that likeif(popupid !== -1) { do your call }if(popupid > 0) {, you could also just wrap all of your js in your php if statement and not have your js if