javascript - Run jQuery function if PHP variable exists - Stack Overflow

I am using a Twitter bootstrap plugin called 'Bootbox' that shows a modal form.I only want t

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 like if(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
Add a ment  | 

3 Answers 3

Reset to default 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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745198407a4616204.html

相关推荐

  • javascript - Run jQuery function if PHP variable exists - Stack Overflow

    I am using a Twitter bootstrap plugin called 'Bootbox' that shows a modal form.I only want t

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信