在 javascript 的 IF 语句中使用 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8379058/
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-24 05:45:58  来源:igfitidea点击:

Using PHP in javascript's IF Statement

javascriptphpif-statement

提问by sarkolata

I am trying to make a confirm box which will desire which php code will be executed. Heres my code:

我正在尝试制作一个确认框,它将希望执行哪个 php 代码。这是我的代码:

<script type="text/javascript">
    var answer = confirm('Are you sure?');

    if(answer==true)
       {
          <?php $confirmation = 1; ?>
       } 
    else 
       { 
          <?php define("CONFIRMATION", 1, true); ?>
       }
         alert('<?php echo $confirmation; ?>')
         alert('<?php echo defined("CONFIRMATION"); ?>')
</script>

The problem is , even if i click YES, $confirmation and boolean from defined() function returns 1. Whatever I click, (cancel or ok) one of them should be 0 (I've already declared $confirmation before) But both of codes at if and else blocks are used! Normally it works like this

问题是,即使我点击 YES,$confirmation 和来自 defined() 函数的 boolean 返回 1。无论我点击什么,(取消或确定)其中一个应该是 0(我之前已经声明了 $confirmation)但是两者使用 if 和 else 块中的代码!通常它是这样工作的

回答by Francis Avila

You fundamentally misunderstand what PHP is doing.

您从根本上误解了 PHP 在做什么。

PHP is evaluated on the server before the page is sent to your browser. By the time the browser sees it and executes the javascript, all the PHP is gone.

在将页面发送到您的浏览器之前,会在服务器上评估 PHP。当浏览器看到它并执行 javascript 时,所有的 PHP 都消失了。

Use your browser's "view source" on the browser window with this code in it. You'll see it looks like this:

在包含此代码的浏览器窗口中使用浏览器的“查看源代码”。你会看到它看起来像这样:

<script type="text/javascript">
var answer = confirm('Are you sure?');

if(answer==true)
   {
             } 
else 
   { 
             }
     alert('1')
     alert('1')
</script>

You either need to implement what you want to do in javascript to run on the browser, or you need to send a new request to the server and get a new page back (either directly or indirectly) with your response.

您要么需要在 javascript 中实现您想要在浏览器上运行的操作,要么需要向服务器发送一个新请求并通过您的响应(直接或间接)返回一个新页面。

回答by Yes Barry

That will never work because PHP is processed beforethe output is sent to the browser. If you really need to modify something in PHP then try using an AJAX call.

这永远不会奏效,因为 PHP在输出发送到浏览器之前被处理。如果您确实需要修改 PHP 中的某些内容,请尝试使用 AJAX 调用。

http://ajaxpatterns.org/XMLHttpRequest_Call

http://ajaxpatterns.org/XMLHttpRequest_Call

Or try using jQuery's $.ajax();function. Start by looking here.

或者尝试使用 jQuery 的$.ajax();函数。从这里开始

Here is a quick example:

这是一个快速示例:

<script type="text/javascript">
var answer = confirm('Are you sure?');
$.ajax({
    type: 'GET',
    url: '/path/to/script.php',
    data: 'answer=' + answer,
    success: function(response) {
        alert(response);
    }
});
</script>

Contents of script.php:

script.php 的内容:

<?php
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) 
        && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
    ) {
        // AJAX request
        $answer = $_GET['answer'];
        // ...
    }

回答by danicotra

You can't trigger a PHP code exection without a post/get request. For your needs, you should choose between a form submisssion or load a page-link with parameters stuffed in the query string on confirmation.

没有 post/get 请求,您无法触发 PHP 代码执行。根据您的需要,您应该在表单提交或加载页面链接之间进行选择,在确认时将参数填充在查询字符串中。

P.S. the query string parameters are the ones following the "?" in the format variable=value

PS查询字符串参数是“?”后面的那些 在格式变量=值

for example:

例如:

index.php?answered=1

you will be then able to retrieve these vatiable/values using PHP $_POST, $_GET or $_REQUEST variables in a way like this:

然后,您将能够以如下方式使用 PHP $_POST、$_GET 或 $_REQUEST 变量检索这些变量/值:

if ($_REQUEST['answered'] == 1) { //confirmed
...
}

回答by Steve Rukuts

You are misunderstanding the order of what will happen here.

你误解了这里发生的事情的顺序。

Firstly, PHP will output the javascript layer. Your if block will then look like this:

首先,PHP 会输出 javascript 层。您的 if 块将如下所示:

if (answer == true)
{
}
else
{
}

The javascript engine should then optimise that out and totally ignore it. Consider using AJAX if you need to get PHP to process something with an input from the javascript layer.

然后 javascript 引擎应该优化它并完全忽略它。如果您需要让 PHP 使用来自 javascript 层的输入处理某些内容,请考虑使用 AJAX。

回答by Amir Raminfar

Normally it works like this

通常它是这样工作的

No, it never works like this. PHP is executed before the javascript so it will never work like this.

不,它永远不会像这样工作。PHP 在 javascript 之前执行,所以它永远不会像这样工作。

I think from what I see you would want something like

我想从我所看到的你会想要类似的东西

<a href="?confirmation=1" onclick="return confirm('Are you sure?')">Your link</a>

This will go to the current page with $_GET['confirmation']set to "1".

这将转到当前页面$_GET['confirmation']设置为"1"

回答by Mohit Bumb

php executed before javascript so you can't do this because when you check it via javascript if else statement php is already executed so you can't do it but however you can use ajax for it

php 在 javascript 之前执行,所以你不能这样做,因为当你通过 javascript 检查它时,如果 else 语句 php 已经执行,所以你不能这样做,但是你可以使用 ajax