简体   繁体   English

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

[英]PHP echo javascript doesnt work in if statement

So the code looks like this:所以代码看起来像这样:

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

..so in the if-statement where I check the file exists the echo doesnt work, but the mkdir($folder) command runs successfully and it is a bit confusing for me. ..so 在我检查文件是否存在的 if 语句中,echo 不起作用,但是 mkdir($folder) 命令成功运行,这让我有点困惑。 Why echo doesnt work if it in an if-statement?如果它在 if 语句中,为什么 echo 不起作用?

The <script> tags will only be executed if you put them into the HTML of a DOM element. <script>标签只有在你将它们放入 DOM 元素的 HTML 时才会被执行。 That doesn't happen automatically, you need to do it in your success function.这不会自动发生,你需要在你的success function 中做到这一点。

function createFolder(folder){
    $.ajax({
        url: "index.php",
        type: "POST",
        data: {'folder':folder},
        success: function(data) {
            console.log("successful post");
            $("#somediv").html(data);
        }
    });
}

Ok you're trying to get the value from a php server using Ajax with JavaScript, then I'm guessing you want to alert to the page when received好的,您正在尝试使用 Ajax 和 JavaScript 从 php 服务器获取值,然后我猜您想在收到时提醒页面

The problem is that问题是

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

Only is true in the actual Ajax request itself, which only fetches the data as a string from the server, but doesn't actually execute it只有在实际的 Ajax 请求本身中才为真,它只从服务器获取数据作为字符串,但实际上并不执行它

If you want the code to be executed on the page, you have to do that on the Ajax on success call on the client side, so如果要在页面上执行代码,则必须在客户端成功调用时在 Ajax 上执行此操作,所以

<script>
function createFolder(folder){
    $.ajax({
    url: "index.php",
    type: "POST",
    data: {'folder':folder},
    success: function(data) {
        document.body.innerHTML+=data
         // Or maybe data.responseTezt or something idk 
        // Look up in the API how to get the text content
        console.log("successful post");
        }
    });
}
</script>

Then on the server side only echo the JavaScript if "folder" is not set,然后在服务器端只回显 JavaScript 如果“文件夹”没有设置,

Also in the client side in order to actually execute you JavaScript you may have to make a new Dom parser同样在客户端,为了实际执行你 JavaScript 你可能需要制作一个新的 Dom 解析器

so the whole php file is basically所以整个 php 文件基本上是

<?php
if(isset($_POST["folder"])) {
    //All of your other code
} else {

?>
<!--all of your HTML code-->
<script>
function createFolder(folder){
    $.ajax({
    url: "index.php",
    type: "POST",
    data: {'folder':folder},
    success: function(data) {

      
         // Or maybe data.responseTezt or something idk 
        // Look up in the API how to get the text content
        var dp= new DOMParser()
        var doc=dp.parseFromString(data,"text/html")
        Array.from(doc.children).forEach(t=>
            document.body.appendChild(t)
        )
  
        console.log("successful post");
        }
    });
}
</script>
<?php } ?>

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

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