简体   繁体   English

为什么我的确认“警报”对我的代码不起作用?

[英]Why my confirm “alert” don't work on my code?

This is a page of a web app, and the job is simple:这是一个 web 应用程序的页面,工作很简单:

I can see the " alert " that asks me whether I want to confirm or not.我可以看到询问我是否要确认的“警报”。 Based on the answer, I move to other pages.根据答案,我转到其他页面。

If i sent my " if " cicle, the "alert" it's showed.如果我发送了我的“ if ”cicle,它就会显示“警报”。 This is the code这是代码

<?php
session_start();
include('../Model/gestoreAccessi.php');
include('connect.php');
?>
<script> 
let result = confirm("Sei sicuro di voler procedere?");
</script>
<script>
if(!result)
    result = <?php header('Location: http://www.teamjg.altervista.org/Matteo/navbar.php');?>
else
    <?php
    session_start();
    session_unset();
    session_destroy();
    header('Location: http://www.teamjg.altervista.org/Matteo/index.html');
    ?>
</script>

So, before I answer your question;所以,在我回答你的问题之前; just a note.只是一个注释。 PHP and Js will run on different machines (even though you're testing it on your localhost, still consider them as different machines) PHP 和 Js 将在不同的机器上运行(即使您在本地主机上测试它,仍然将它们视为不同的机器)

All PHP code is run, and executed on the backend before anything is sent to your browser, after which js code is executed.所有 PHP 代码都运行,并在后端执行,然后将任何内容发送到您的浏览器,然后执行 js 代码。 PHP does not understand JS syntax, it will only look at <? PHP 不懂JS语法,只会看<? and ?> tags and execute everything inside them.?>标签并执行其中的所有内容。

<?php
session_start();
include('../Model/gestoreAccessi.php');
include('connect.php');
?>
<script> 
let result = confirm("Sei sicuro di voler procedere?");
</script>
<script>
if(!result)
    result = <?php header('Location: http://www.teamjg.altervista.org/Matteo/navbar.php');?>
else
    <?php
    session_start();
    session_unset();
    session_destroy();
    header('Location: http://www.teamjg.altervista.org/Matteo/index.html');
    ?>
</script>

When this code is being sent by XAMPP (or whatever engine you use), the PHP is first executed which will make the code look like this:当此代码由 XAMPP(或您使用的任何引擎)发送时,首先执行 PHP,这将使代码如下所示:

// Session will be started
// contents of ../Model/gestoreAccessi.php will be copied and run
// contents of connnect.php will be copied and run
<script> 
let result = confirm("Sei sicuro di voler procedere?");
</script>
<script>
if(!result)
    result = //The header is already set
else
    // Location is already set
</script>

So this is undefined behaviour as you are sending two header tags with different locations, my best guess is it will redirect to http://www.teamjg.altervista.org/Matteo/index.html .所以这是未定义的行为,因为您要发送两个具有不同位置的 header 标签,我最好的猜测是它会重定向到http://www.teamjg.altervista.org/Matteo/index.html

One thing you could do as a hotfix is set window.location.href if you want to redirect your user to some other website.如果您想将用户重定向到其他网站,您可以作为修补程序做的一件事是设置window.location.href Read this .这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM