简体   繁体   English

PHP 显示在错误的页面上

[英]PHP is shown on wrong page

I'm recently working on a website project.我最近在做一个网站项目。 Therefor I have a website.php with all html code, a function.php and saveArray.js .因此,我有一个包含所有 html 代码的 website.php、一个 function.php 和 saveArray.js。 In website.php I'm printing a html table with a button at the bottom.在 website.php 中,我正在打印一个底部有一个按钮的 html 表格。 Through the button click I'm getting to the saveArray.js, where I save all the table data in an array.通过单击按钮,我将进入 saveArray.js,我将所有表数据保存在一个数组中。

With this code有了这个代码

var arrString = JSON.stringify(tableData);  
var request = new XMLHttpRequest();
   request.open('post', 'function.php', true);
   request.setRequestHeader('Content-Type', 'application/x-www-form- 
   urlencoded');
   request.send('daten=' + arrString);

I post the JS array to function.php.我将 JS 数组发布到 function.php。 In function.php I do something with the array and in an if statement I want to show a modal.在 function.php 中,我对数组进行了处理,并在 if 语句中显示了一个模态。

The modal itself works, but I want to show it on website.php page.模态本身有效,但我想在 website.php 页面上显示它。 Which doesn't happends, because I'm currently on function.php .这不会发生,因为我目前在 function.php 上。

How can I solve this ?我该如何解决这个问题?

EDIT: In my array is an ID and I want to check if this ID is already in my database or not.编辑:在我的数组中是一个 ID,我想检查这个 ID 是否已经在我的数据库中。 Depending on this result I want to show the modal and upload the data if necessary.根据这个结果,我想显示模态并在必要时上传数据。 All the checking is happening in function.php所有检查都在 function.php 中进行

I suppose you want to inject the string returned (the modal PHP code) by your function in function.php in your current page ('website.php').我想你想在当前页面('website.php')的 function.php 中注入你的函数返回的字符串(模态 PHP 代码)。 To do this, you'll have to inject the response given by the XMLHttpRequest when the request is finished.为此,您必须在请求完成时注入 XMLHttpRequest 给出的响应。

Let's suppose we want to add all the contents within假设我们要添加其中的所有内容

  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };

See, You are not handling the response of the request.So handle the response.and restuern the status of the request from function.php and if data is saved the open the model.看,您没有处理请求的响应。所以处理响应。并从 function.php 恢复请求的状态,如果保存了数据,则打开模型。 You need not go to the function.php page.您无需转到 function.php 页面。 See the code看代码

   var xhttp = new XMLHttpRequest();
          xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {

             // this is response of the request  //now check it

             //Suppose you returned " data saved" as response from function.php

             if(this.responseText='data saved'){
            //Open model here
    }

            }
          };
          xhttp.open("POST", "function.php", true);
          xhttp.send();

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

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