如何使用 PHP 使用 javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19068311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:46:24  来源:igfitidea点击:

How to use javascript using PHP?

javascriptphpjquery

提问by user2826499

Scenario:

设想:

I created a filtering using IF ELSEwhere when the user click BOX A AND BOX B... Then assuming the BOX A is already full, after the user hit the "SAVE BUTTON", a confirmation message will display saying that... "The Box you chosen is already full. Do you want to save the other items?"

我创建了一个使用过滤IF ELSE,其中当用户点击BOX A和盒子B ......然后,假设箱A已经满了,用户点击“保存按钮”,经过确认信息将显示说...... “箱子“你选择的已经满了。要保存其他项目吗?”

Here's my Code:

这是我的代码:

<?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>";

    }
?>

I have a problem with the code and when I try to trace it using echo "enter here";its always stop after the program read this code

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

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

Then it will jump it in here:

然后它会跳到这里:

 echo "</script>";

RESULT:

结果:

its not displaying the pop up message ... So all I need is to display the pop up message and I dont know why, why its not displaying... There is no button involve for the displaying of confirmation message...It will just pass by in if else and then it will prompt up if the user wants to continue the saving if one of the box is already full if not he/she will press the cancel...

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

NOTE: I'm only a beginner for PHP and Javascript..

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

Thank you in advance.

先感谢您。

回答by davidkonrad

This is because you start a new <?/ <?phptag inside another

这是因为您在另一个内部开始了一个新的<?/<?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>";
    ?>
}

should be

应该

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>";
}

But personally I think

但个人认为

    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>
<?
    }

is much cleaner and more readable. Keep javascript out of PHP, dont echo it out, if you can avoid it. It can be hard to survey in 6 months.

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

回答by Jenson M John

Or you can do something like this.

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

    <?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
    }
    ?>

回答by Vereos

I'm assuming that you have put a php tag and an if statement before this line

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

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

The fact is that you don't need to open another php tag. You are already writing in php so you don't need to specify it again since it will cause your code to fail. You could write something like this instead.

事实是您不需要打开另一个 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
}

回答by GroovyCarrot

Your problem is basically because when you're echo'ing your javascript you're forgetting that the ;character still needs to proceed JS lines:

您的问题基本上是因为当您回显您的 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>";

    }
?>

Little trick I use is that PHP echo allows new lines without breaking so you can always just echo a block of code and look over it easier

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

回答by Youssef

$str = <<<MY_MARKER
<script type="text/javascript">
document.write("Hello World!");
</script>
MY_MARKER;
echo $str;