2016-02-12 32 views

回答

1

如果我理解正确,这将是您的问题的答案。

$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>'; 
} 
0

从我的理解,我想你想在一个理想值分配给$msg显示JS警告框。以下代码可能会有所帮助。

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

的基本结构保持不变,即使$msg值没有你的情况进行硬编码。只有if中的条件可能因变量类型而异。您可以使用if($msg)。这只是检查是否$msg已分配值。

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

或许真的在DOM这样,让PHP和JavaScript分开:

<?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> 
相关问题