Anaes Arias
Anaes Arias

Reputation: 49

echo php javascript alert?

How can I echo this javascript if the php error messages is called? I have an error message setting that when a user misses his username or password it triggers an error message. The php error message is called by a php code. Here is the code:

<?php echo showmessage($msg) ?> 

I have an alert message in javascript that when called it will make a javascript css pop up alert box. IF the javascript code is present it will show the alert box right away after reload. Here is code:

<script type="text/javascript">  
  $(document).ready(function () {  
   jqxAlert.alert('Alert Message');  
  })  
</script>  

How can I incorporate so that when the php echo message comes up it will trigger the javscript alert message. I was trying an if in php, so something like this code:

if ( showmessage($msg) ) {
   <script type="text/javascript">  
     $(document).ready(function () {  
         jqxAlert.alert('Alert Message');  
     })  
   </script>
}

How can I echo my javascript message on the php call?

Upvotes: 1

Views: 15696

Answers (4)

You can create a function, I think this is the most practice way to do it:

1.define a function:

<?php function phpAlert($msg) {  echo '<script type="text/javascript">alert("' .$msg. '")</script>';}  ?>

2. call it like this:

<?php phpAlert(   "Hello world!\\n\\nPHP has got an Alert Box"   );  ?>

Upvotes: 0

aretecode
aretecode

Reputation: 334

This could be written like this:

endif

endif-stackoverflow

<?php if (showmessage($msg)): ?>
    <script>
        $(document).ready(function (){
            jqxAlert.alert('Alert Message');
        });
    </script> 
<?php endif; ?>

Or like this:

<?php if (showmessage($msg)) { ?>
    <script>
        $(document).ready(function (){
            jqxAlert.alert('Alert Message');
        });
    </script> 
<?php } ?>

Or like this:

<?php 

    if (showmessage($msg)) { 
        echo
        '
            <script>
                $(document).ready(function (){
                    jqxAlert.alert(\'Alert Message\');
                });
            </script> 
        ';
    } 

?>

short tags

short hand comparison (ternary)

ternary 2

Or even this (probably not the best idea though):

<?= (showmessage($msg)) ? '<script>$(document).ready(function (){jqxAlert.alert(\'Alert Message\');});</script>' : ""; ?>

Alternatively, if you mean you would like to put the error message in that alertbox

<?php 

    if (showmessage($msg)) { 
        echo
        '
            <script>
                $(document).ready(function (){
                    jqxAlert.alert('.showmessage($msg).');
                });
            </script> 
        ';
    } 

?>

and again in the first style:

<?php if (showmessage($msg)): ?>
    <script>
        $(document).ready(function (){
            jqxAlert.alert(<?= showmessage($msg) ?>);
        });
    </script> 
<?php endif; ?>

Upvotes: 1

Krish R
Krish R

Reputation: 22711

Can you try this

     <?PHP if ( showmessage($msg) ) {  ?>
         <script type="text/javascript">
               $(document).ready(function () {
                   jqxAlert.alert('Alert Message');
                });
           </script> 
     <?PHP } ?>

Upvotes: 1

Ronak Patel
Ronak Patel

Reputation: 390

something like this.. close your php tag before starting to write javascript..

<?php if ( showmessage($msg) ) { ?>
   <script type="text/javascript">  
       alert('Alert Message');
   </script>
<?php } ?>

Upvotes: 1

Related Questions