我创建了一个函数,并设置了onclick提交按钮。当我单击该提交按钮时,确认功能将要求我输入“确定”或“取消”。但是这里没有任何事情发生,而是直接执行else条件。但是,如果我从以下两个条件中删除php部分,所有这些事情都将以适当的方式运行。这是什么错误?
带有php部分的代码包括:-
function confirmation(){
var r = confirm('Are you sure you want to save this thing into the database?');
if (r)
{
<?php
$confi = 1;
echo "<script type='text/javascript'>alert('$confi');</script>";
?>
}
else {
<?php
$confi = 0;
echo "<script type='text/javascript'>alert('$confi');</script>";
?>
}
}
无php部分的代码:-
function confirmation(){
var r = confirm('Are you sure you want to save this thing into the database?');
if (r)
{
alert('1');
}
else {
alert('0');
}
}
答案 0 :(得分:-1)
您不能在Javascript中编写<script>
标签。如果您查看view-source
部分,就可以理解。应该是
function confirmation(){
var r = confirm('Are you sure you want to save this thing into the database?');
if (r)
{
<?php
$confi = 1;
echo "alert('$confi');";
?>
}
else {
<?php
$confi = 0;
echo "alert('$confi');";
?>
}
}