只要我一值分配给我的PHP变量$msg
...当我为PHP变量赋值时,是否可以调用JavaScript函数?
$msg = 'Some message';
...我要调用JS函数是这样的:
function showSuccess() {
alert('success')
}
这可能吗?
只要我一值分配给我的PHP变量$msg
...当我为PHP变量赋值时,是否可以调用JavaScript函数?
$msg = 'Some message';
...我要调用JS函数是这样的:
function showSuccess() {
alert('success')
}
这可能吗?
如果我理解正确,这将是您的问题的答案。
$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>';
}
从我的理解,我想你想在一个理想值分配给$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>"
}
或许真的在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>