I need to trigger a Bootstrap modal if a PHP GET url is set but I don't know how to.
The PHP code look's like this:
<?php
if ($_GET['activate']) {
$activate = $_GET['activate'];
mysql_query("UPDATE `users` SET `active` = 1 WHERE `email_code` = '$activate'");
}
?>
After the mysql query but still in the if I need the modal to trigger. I've considered json but don't know enough to create my own code. The javascript to show the modal is this line:
$('#myModal').modal('show')
How would I translate that to work with my PHP trigger?
I need to trigger a Bootstrap modal if a PHP GET url is set but I don't know how to.
The PHP code look's like this:
<?php
if ($_GET['activate']) {
$activate = $_GET['activate'];
mysql_query("UPDATE `users` SET `active` = 1 WHERE `email_code` = '$activate'");
}
?>
After the mysql query but still in the if I need the modal to trigger. I've considered json but don't know enough to create my own code. The javascript to show the modal is this line:
$('#myModal').modal('show')
How would I translate that to work with my PHP trigger?
Share Improve this question asked Oct 4, 2013 at 9:24 justanotherhobbyistjustanotherhobbyist 2,1904 gold badges29 silver badges40 bronze badges 2- Consider that when php is being processed the page doesn't even exist, and when JS is loaded PHP has already done its work. Might want to use AJAX for that, but the workflow isn't much clear so far – Damien Pirsy Commented Oct 4, 2013 at 9:27
- Good to know, would this be fixed with document.ready and brackets around the modal code? Or should I just set a javascript variable inside php and make the modal trigger on document ready if that variable is set? – justanotherhobbyist Commented Oct 4, 2013 at 9:29
2 Answers
Reset to default 15Add a variable that checks the state,
$show_modal = false;
And in your get set it to true,
if ($_GET['activate']) {
$activate = $_GET['activate'];
mysql_query("UPDATE `users` SET `active` = 1 WHERE `email_code` = '$activate'");
$show_modal = true;
}
Now in your html :
<?php if($show_modal):?>
<script> $('#myModal').modal('show');</script>
<?php endif;?>
This is actually easier than most people realise, just inject your JS call into the page using a PHP if statement:
<?php if (($_GET['activate'])) { ?>
<script type="text/javascript"> $('#myModal').modal('show'); </script>
<?php } ?>
Put this php if statement into the head of your document (or the footer if your considering load times) and it will open your modal as required.