2

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?

2
  • 1
    why don't you return some invalid id like -1 when there's no entry in your DB ? Then check for that like if(popupid !== -1) { do your call } 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 Commented Mar 5, 2015 at 17:39

3 Answers 3

2

So, 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.

Sign up to request clarification or add additional context in comments.

Comments

2
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.

Comments

1
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); ?>;

5 Comments

Perfect case for Ternary operator. Much more legible that way.
Is the ternary operator better for execution time/resources? (Real question.) Anyway I agree it would have been more readable here, thanks.
Right sorry. I edited my post with that suggestion too.
No, it doesn't have a performance disadvantage as far as I know. However, you didn't echo the ternary statement in your second example.
@Seth Whoops, fixed! Thank you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.