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?
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?
Share Improve this question asked Mar 5, 2015 at 17:36 chris.cavagechris.cavage 8811 gold badge17 silver badges38 bronze badges 2-
1
why don't you return some invalid id like
-1
when there's no entry in your DB ? Then check for that likeif(popupid !== -1) { do your call }
– Arkantos Commented Mar 5, 2015 at 17:38 -
1
your just checking to see if popupid is a variable try something like
if(popupid > 0) {
, you could also just wrap all of your js in your php if statement and not have your js if – cmorrissey Commented Mar 5, 2015 at 17:39
3 Answers
Reset to default 2So, why render the javascript at all if there is no popup_id in your database?
<?php
if ($contact->findPopup()) {
?>
<!-- javascript/html/whatever goes here -->
<?php
}
?>
Then your javascript only gets rendered if there is a valid popup_id in the database.
var popupid = "<?php if ($contact->find_popup()) { echo $contact->popup()->id; } else { echo false;} ?>";
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() {
}
}
}
});
}
Thanks for the suggestions. It wouldn't work properly until I put this line in quotes!
var popupid = "<?php if ($contact->find_popup()) { echo $contact->popup()->id; } else { echo false;} ?>";
Siliconrockstar's suggestion also works, but I am using the above because as it checks for find_popup, the popup id is stored as a variable which I need for the ajax url. Still works though...
Appreciate the help.
var popupid = <?php if ($contact->find_popup()) { echo $contact->popup()->id; } ?>;
Here, once the PHP is executed, you have :
// If a popup exists
var popupid = 123;
// If there is no popup
var popupid = ;
The second line will make your JS crash.
I suggest:
var popupid = <?php if ($contact->find_popup()) { echo $contact->popup()->id; } else { echo false; } ?>;
Or as Seth suggested:
var popupid = <?php echo (($contact->find_popup()) ? $contact->popup()->id : false); ?>;