[英]How I send PHP parameter/variable to JS function?
I want to get URL parameter with PHP and send the parameter (ID) to a JS function. 我想用PHP获取URL参数,并将参数(ID)发送给JS函数。
This is what I tried. 这就是我尝试过的。
PHP: PHP:
$param = $_GET['param'];
if($param != "" || $param == NULL || (!empty($param))){
echo '<script type="text/javascript">',
'onparam($param);',
'</script>';
}
JS: JS:
function onparam(param) {
$.ajax({url: "AJAX.php", data: 'id=' + param + '&switch_content=details', success: function(result){
$("#data-table").html(result);
}});
document.getElementById("overlay").style.display = "block";
}
I tried to put the parameter in a variable and call the JS function with it. 我试图将参数放在变量中,然后使用它调用JS函数。 In the next step I tried to catch the variable in the JS function to put it in the string of "data:".
在下一步中,我尝试在JS函数中捕获变量,以将其放入“ data:”字符串中。
You want to use double quote instead of singles quotes (as suggested by gogaz). 您想使用双引号而不是单引号(由gogaz建议)。 You also want to put quotation marks around the variable if it's a string.
如果变量是字符串,您还想在变量周围加上引号。
One more thing - You can remove $param != "" || $param == NULL
还有一件事-您可以删除
$param != "" || $param == NULL
$param != "" || $param == NULL
as both ""
& null
are considered to be empty. $param != "" || $param == NULL
因为""
和null
都被认为是空的。 I am assuming you mean $param != "" && $param != NULL
here, if not, just ignore. 我假设您的意思是
$param != "" && $param != NULL
,如果不是,则忽略。 But that may also be causing the issue if $param is null. 但是,如果$ param为null,那也可能会引起问题。
$param = $_GET['param'];
if(!empty($param)) {
echo "<script type='text/javascript'>",
"onparam('$param');",
"</script>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.