3 回答
![?](http://img1.sycdn.imooc.com/5333a1920001d36402200220-100-100.jpg)
TA贡献1946条经验 获得超4个赞
PHP 是服务器端,JavaScript 是客户端(不考虑 NodeJS)。
也就是说,当你调用页面时,PHP 完全运行,无需等待你制作的 JavaScript,实际上 PHP 只构建包含脚本的网页,只有当浏览器收到该页面时才会执行 JavaScript,但 PHP 已经完成它的所有执行。
在您的示例代码中,$answer
包含的是字符串<script type='text/javascript'> document.write(answer); </script>
而不是 JavaScript 的执行结果,因为 PHP 无法执行这样的脚本。
如果要在浏览器上下文中执行此 PHP,则需要更改 JavaScript 以调用在查询中传递确认对话框值的 PHP url,并且 PHP 使用类似的内容检查传递的查询,parse_str($_SERVER['QUERY_STRING'], $query);
并且仅在以下情况下构建页面none 被传递并继续执行,如果它收到true
.
@v-prince 的回答让你知道如何做客户端。
![?](http://img1.sycdn.imooc.com/54586431000103bb02200220-100-100.jpg)
TA贡献1830条经验 获得超3个赞
我希望这会给你一些想法..祝你好运
示例.html
<input type='button' class='test'>
<!-- make sure you have a jquery plugin and check the path of your jquery -->
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('.test').click(function(){
var answer = confirm('are you sure you want to click me?');
$.ajax({
url : 'yourPHP.php',
type : 'post',
data : { answerValue : answer },
success : function( result ){
// do something
}
});
});
});
</script>
![?](http://img1.sycdn.imooc.com/533e4cf4000151f602000200-100-100.jpg)
TA贡献1863条经验 获得超2个赞
您的条件将在 PHP 中执行,而不是在 javascript 中执行。
转换为布尔值时,以下值被视为 FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
还要注意
var_dump(0==FALSE); //bool(true)
var_dump(0===FALSE); //bool(false)
添加回答
举报