1

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?

4 Answers 4

1

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; ?>
Sign up to request clarification or add additional context in comments.

Comments

1

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 } ?>

Comments

1

Can you try this

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

2 Comments

@AnaesArias not exactly, he/she closed the php tag
you missed to add the PHP tags, check the PHP condition STARTING and END section
0

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

Comments

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.