3

设想:

我创建了一个过滤器IF ELSE,当用户单击 BOX A 和 BOX B ...你选择的已经满了,要不要保存其他的东西?”

这是我的代码:

<?php
//else if......
    else if($box== "BOX A" && $row[item]  == "100")
    {

        echo "<script type='text/javascript'>";
        echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
        echo "if (r==true){";
        echo "alert('You pressed OK!')}";
        echo "else{";
        echo "alert('You pressed Cancel!')}";
        echo "</script>";

    }
?>

我的代码有问题,当我尝试使用echo "enter here"; 它跟踪它时,程序读取此代码后总是停止

  echo "<script type='text/javascript'>";

然后它会跳到这里:

 echo "</script>";

结果:

它不显示弹出消息...所以我只需要显示弹出消息,我不知道为什么,为什么它不显示...没有用于显示确认消息的按钮...它将只需传入 if else 然后它会提示用户是否要继续保存如果其中一个框已满,如果没有,他/她将按取消...

注意:我只是 PHP 和 Javascript 的初学者。

先感谢您。

4

5 回答 5

9

这是因为你在另一个里面开始了一个新的<?/<?php标签

else if($box== "BOX A" && $row[item]  == "100")
{
    <?php
    echo "<script type='text/javascript'>";
    echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
    echo "if (r==true){";
    echo "alert('You pressed OK!')}";
    echo "else{";
    echo "alert('You pressed Cancel!')}";
    echo "</script>";
    ?>
}

应该

else if($box== "BOX A" && $row[item]  == "100")
{
    echo "<script type='text/javascript'>";
    echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
    echo "if (r==true){";
    echo "alert('You pressed OK!')}";
    echo "else{";
    echo "alert('You pressed Cancel!')}";
    echo "</script>";
}

但我个人认为

    else if($box== "BOX A" && $row[item]  == "100")
    {
?>
<script>
var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
if (r==true) {
    alert('You pressed OK!');
} else {
    alert('You pressed Cancel!');
}
</script>
<?
    }

更干净,更具可读性。将 javascript 排除在 PHP 之外,如果可以避免的话,不要回显它。在 6 个月内进行调查可能很困难。

于 2013-09-28T15:06:49.193 回答
3

或者你可以做这样的事情。

    <?php
    //some PHP code
    if($box== "BOX A" && $row[item]== 100)
    {
    ?>

        <script type='text/javascript'>
        var r=confirm('The Box you chosen is already full. Do you want to save the other items?');
        if (r==true){
        alert('You pressed OK!')}
        else{
        alert('You pressed Cancel!')}
        </script>

    <?php
    }
    ?>
于 2013-09-28T15:15:13.710 回答
2

我假设您在此行之前放置了一个 php 标签和一个 if 语句

else if($box== "BOX A" && $row[item]  == "100")

事实上,您不需要打开另一个 php 标签。您已经在用 php 编写代码,因此您无需再次指定它,因为它会导致您的代码失败。你可以写这样的东西。

else if($box == "BOX A" && $row[item] == "100")
{ ?>
    <script type='text/javascript'>
    var r=confirm('The Box you chosen is already full. Do you want to save the other items?');
    if (r==true)
        alert('You pressed OK!');
    else
        alert('You pressed Cancel!');
    </script>
  <?php
}
于 2013-09-28T15:17:01.547 回答
0
$str = <<<MY_MARKER
<script type="text/javascript">
document.write("Hello World!");
</script>
MY_MARKER;
echo $str;
于 2013-09-28T15:14:39.130 回答
0

您的问题基本上是因为当您回显您的 javascript 时,您忘记了;角色仍然需要继续 JS 行:

<?php
//else if......
    else if($box== "BOX A" && $row[item]  == "100")
    {

        echo "<script type='text/javascript'>
                var r = confirm('The Box you chosen is already full. Do you want to save the other items?'); // <- this was missing
                if (r == true){
                    alert('You pressed OK!');
                }
                else{
                    alert('You pressed Cancel!');
                }
                </script>";

    }
?>

我使用的小技巧是 PHP 回显允许换行而不会中断,因此您始终可以回显一段代码并更轻松地查看它

于 2013-09-28T15:16:01.717 回答