I'm unable to call my modal in this PHP file to be called when and if statement is valued to be true
Modal
<div id="myModal" role="dialog">
<div >
<!-- Modal content-->
<div >
<div >
<button type="button" data-dismiss="modal">×</button>
<h4 >Modal Header</h4>
</div>
<div >
<p>Some text in the modal.</p>
</div>
<div >
<button type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
PHP code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<?php
// trying to target modal
if (true){
// not able to call
echo "$( document ).ready(function(){ $('#form').modal('show')";
}
else{
echo "alert(\"haha\")";
}
?>
Please help thank you, so it should open a pop up when the if statement is true but it is not working.
Thanks!
CodePudding user response:
You will just need to change a couple of items to get your popup to work. First don't forget your closing braces in your script tags and second, be sure to use the same ID so that it knows what one to look for.
//Your ID should match the ID of the modal you would like to open
<div id="myModal" role="dialog">
<div >
<!-- Modal content-->
<div >
<div >
<button type="button" data-dismiss="modal">×</button>
<h4 >Modal Header</h4>
</div>
<div >
<p>Some text in the modal.</p>
</div>
<div >
<button type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
And in your script, be sure to add the same ID from the myModal DIV
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<?php
// trying to target modal
if (true){
// not able to call
echo "
<script type='text/javascript'>
$(document).ready(function(){
$('#myModal').modal('show'); //myModal is ID of div
}); //Don't forget your closing braces
</script>
";
}
else{
echo "alert(\"haha\")";
}
?>
CodePudding user response:
we can write php code anywhere on the page so try to write php condition inside javascript like this way
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
<?php
if (true){
?>
$( document ).ready(function(){ $('#form').modal('show')});
<?php
}
else{
?>
alert("haha");
<?php
}
?>
</script>