0

As soon as I assign a value to my PHP variable $msg...

$msg = 'Some message'; 

... I want to call a JS function like this :

function showSuccess() { 
    alert('success')
}

Is it possible ?

3 Answers 3

1

If I understand you correctly, this will be the answer for your question.

$msg = 'some value your are going to assign';

if($msg) //checking $msg variable has some value or not
{
   print '<script type="text/javascript">showSuccess()</script>'; 

   /* or */

   print '<script type="text/javascript">alert("succeess")</script>'; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

From what I understood, I think you would like to display a JS alert box when a desirable value is assigned to $msg. The following code may help.

<?php
$msg="Hello";  
  /*this is just to give you an idea 
  and so I am considering value of 
  $msg as hardcoded*/

if($msg!="")
/*may vary with the variable type,for
example if($msg!=0) in case of int*/
{
echo "<script>alert('successfull');</script>"
  /*echo adds stuff to the html
  document on the go*/
  /*the above JS code will be added to
  the HTML document and executed
  to display an alert box*/
}
?>

The basic structure remains the same even if the value of $msg is not hardcoded in your case. Only the condition in if may vary depending on the variable type.

You could also use if($msg). This simply checks if $msg has been assigned a value or not.

 if($msg!)
 //universal-checks if value is assigned
 {
 echo "<script>alert('successfull');</script>"
 }

Comments

0

Maybe something in DOM like this, to keep php and javascript separate:

<?php
    //assign your value   
    $value = true;
?>

<input id="value" type="hidden" value="<?php if ($value) { echo 'true'; } ?>" />

   <script>
       var value = document.getElementById('value').value;
       if (value) {
          alert(value);
        }
    </script>

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.