只要我一值分配給我的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>