-5

I am trying to pass an 2D array from JavaScript to PHP using Ajax. The Ajax said it is success but after redirect to PHP page, but no data is set in the PHP page. I have tried two methods but both of them cannot successfully post the array.

The array looks like:

num = [["StringA",0], ["StringB",1], ["StringC",2]];

This is the first method I tried in JavaScript:

$.ajax({
    type: "POST",
    url: "database.php",
    contentType: 'application/json',            
    processData: false,
    data: { array : JSON.stringify(num) },
    success: function()
    {
        window.location = "database.php";
    }
 });

I have also tried this:

$.post("database.php", {array: num})
        .done(function(data){
        window.location = "database.php";
    });

In database.php:

<?php
if (isset($_POST["array"])){
    echo "Data is set";
    print_r($array);
}
?>

Both of them can successfully redirect to database.php, but none of them show something on screen.

I am not familiar with Ajax. How can I post an array to the php page right after the array is sent?

matisetorm
  • 847
  • 8
  • 21
Alice Smith
  • 3
  • 1
  • 3
  • You need to specify full request url of database.php file. – Kapil Yadav Jan 29 '18 at 09:35
  • Why are you using `AJAX` to post the data to then redirect the client to that page? Also redirecting the client to that `php` page isn't going to display the array data because the client isn't posting the array when being redirected. – NewToJS Jan 29 '18 at 09:39
  • `window.location = "database.php"` makes the browser issue a GET request for that URL, so of course there is no _POST_ data ... – CBroe Jan 29 '18 at 09:39
  • @kapilyadav It is still not working. – Alice Smith Jan 29 '18 at 09:46
  • 1
    How do you expect to see something if you redirect and never display what is returned? – Juan Mendes Jan 29 '18 at 09:49
  • @NewToJS I redirect the page because I want to check if there are any data being set on the page. Is there any other method to check if there are data being set? – Alice Smith Jan 29 '18 at 09:49
  • @AliceSmith Instead of redirecting to `database.php` with `window.location`, you should display what is returned by the AJAX call, probably by creating a div where you can do `success: function(response) { console.log(response); div.innerHTML = JSON.stringify(response) }` – Juan Mendes Jan 29 '18 at 09:54
  • Have you tried printing data inside php code? – Gaurav Dave Jan 29 '18 at 09:57
  • If you dont mind me asking, what exactly are you doing with it on the database.php page? – glenn ferns Jan 29 '18 at 09:59
  • @JuanMendes I have tried this code. There are nothing in the console and the div. – Alice Smith Jan 29 '18 at 10:05
  • @glennferns After I get the array, I will pass it to the database. – Alice Smith Jan 29 '18 at 10:06
  • you should learn how to monitor the ajax request, visit https://stackoverflow.com/a/3019085/3452102 to know more. – Praneeth Nidarshan Jan 29 '18 at 10:06
  • ok from what i understand you want to use some data from the current page and then use it on the database.php page. You mentioned that youre new to ajax so i'll break it down. When you make an ajax call to a page, it will create an instance of that page to handle your ajax request. ( so in your case it will save the data which you send in that instance )....When you do a redirect, it is treated as a another request , which in turn creates its own instance If you do want to send data to the database page you can send a form – glenn ferns Jan 29 '18 at 10:13
  • @PraneethNidarshan Thank you. I use this method to check and there are no resources being passed. I am still trying to find what's wrong in my code. – Alice Smith Jan 29 '18 at 10:17
  • @AliceSmith, If you are a beginner, I recommend you to follow some tutorials on PHP and JQuery Ajax. Because you have a conflict in understanding PHP and JQuery. – Praneeth Nidarshan Jan 29 '18 at 10:24
  • @glennferns Thank you for clarifying my concept. I developed this part by sending form at first, but I cannot post the data so I try to use ajax. It turns out fail again. If then, I will try to do it again by form. – Alice Smith Jan 29 '18 at 10:46

1 Answers1

-1

echo "Data is set"; return text to ajax request. You can see it in console

success: function(msg)
    {
        console.log(msg);
    }

When you redirect via window.location to database.php $_POST is empty and don't display anything, because window.location just set Url and not send params. Try to use form submit to send your data in php file.

V. Buriy
  • 1
  • 1
  • 3