-2

I'm recently working on a website project. Therefor I have a website.php with all html code, a function.php and saveArray.js . In website.php I'm printing a html table with a button at the bottom. Through the button click I'm getting to the saveArray.js, where I save all the table data in an array.

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. In function.php I do something with the array and in an if statement I want to show a modal.

The modal itself works, but I want to show it on website.php page. Which doesn't happends, because I'm currently on 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. Depending on this result I want to show the modal and upload the data if necessary. All the checking is happening in function.php

Eddy_SPL
  • 27
  • 6

2 Answers2

0

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'). To do this, you'll have to inject the response given by the XMLHttpRequest when the request is finished.

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;
    }
  };
CptKicks
  • 441
  • 3
  • 15
  • my function.php has just functional code, no functions with return values. So all code will be executed – Eddy_SPL Jul 01 '19 at 11:47
0

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. You need not go to the function.php page. 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();
CodeBreaker
  • 395
  • 2
  • 9
  • I'm going to function.php, because I am working there with mySQL statements. In my array is an ID and I want to check if this ID is in my database or not. Depending on the result I want to show the modal – Eddy_SPL Jul 01 '19 at 11:36