PHP echo javascript 在 if 语句中不起作用

所以代码看起来像这样:


<script>

function createFolder(folder){

    $.ajax({

    url: "index.php",

    type: "POST",

    data: {'folder':folder},

    success: function(data) {

        console.log("successful post");

        }

    });

}

</script>


<?php

if(isset($_POST["folder"])){

    $folder = $_POST["folder"];

    if(!file_exists($folder)) {

        mkdir($folder);                         <--- this code runs

        echo '<script>alert("qwe")</script>';   <--- this code doesnt run 

    }

    else {

        echo '<script>alert("qwer")</script>';  <--- this code doesnt run

    }

    echo '<script>alert("qwert")</script>';     <--- this code doesnt run 

}

echo '<script>alert("qwerty")</script>';        <--- this code runs

?>

..所以在我检查文件存在的 if 语句中,echo 不起作用,但 mkdir($folder) 命令成功运行,这对我来说有点困惑。为什么 echo 在 if 语句中不起作用?


慕尼黑8549860
浏览 105回答 2
2回答

精慕HU

<script>仅当您将标签放入 DOM 元素的 HTML 中时,这些标签才会被执行。这不会自动发生,您需要在您的success函数中执行此操作。function createFolder(folder){&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: "index.php",&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; data: {'folder':folder},&nbsp; &nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("successful post");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#somediv").html(data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}

富国沪深

好吧,您正在尝试使用 Ajax 和 JavaScript 从 php 服务器获取值,那么我猜您想在收到页面时发出警报问题是if(isset($_POST["folder"]))only 在实际的 Ajax 请求本身中为 true,它仅从服务器获取字符串形式的数据,但并不实际执行它如果您希望代码在页面上执行,则必须在客户端的 Ajax on success 调用上执行此操作,因此<script>function createFolder(folder){&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; url: "index.php",&nbsp; &nbsp; type: "POST",&nbsp; &nbsp; data: {'folder':folder},&nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; document.body.innerHTML+=data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Or maybe data.responseTezt or something idk&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Look up in the API how to get the text content&nbsp; &nbsp; &nbsp; &nbsp; console.log("successful post");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}</script>然后,如果未设置“文件夹”,则在服务器端仅回显 JavaScript,同样在客户端,为了实际执行 JavaScript,您可能必须创建一个新的 Dom 解析器所以整个php文件基本上是<?phpif(isset($_POST["folder"])) {&nbsp; &nbsp; //All of your other code} else {?><!--all of your HTML code--><script>function createFolder(folder){&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; url: "index.php",&nbsp; &nbsp; type: "POST",&nbsp; &nbsp; data: {'folder':folder},&nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Or maybe data.responseTezt or something idk&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Look up in the API how to get the text content&nbsp; &nbsp; &nbsp; &nbsp; var dp= new DOMParser()&nbsp; &nbsp; &nbsp; &nbsp; var doc=dp.parseFromString(data,"text/html")&nbsp; &nbsp; &nbsp; &nbsp; Array.from(doc.children).forEach(t=>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.body.appendChild(t)&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; console.log("successful post");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}</script><?php } ?>
打开App,查看更多内容
随时随地看视频慕课网APP