Jump to content

Posting to PHP page using Ajax


Tom99

Recommended Posts

I am trying to post the contents of a dynamic html table to a php page using AJAX. The array gets populated, but on the target page it shows up as empty. Here's the code of the source page:

 

<script>

function getData(){
var table = document.getElementById("input_table");
var tableArr = [];
for (var i = 1; i < table.rows.length; i++ ) {
tableArr.push({
field1: table.rows[i].cells[0].innerHTML,
field2: table.rows[i].cells[1].innerHTML,
});
}

// post to php page
$.ajax({
type: "POST",
url: "update.php",
data: tableArr,
cache: false,
success: function(response) {
alert('Successfully called');
console.log(response);
setTimeout(function() {
window.location = 'update.php';
},10);
}
});
};
</script>

 

 

On the target page:

<?php
var_dump
($_POST);
?>

 

Any idea what's going wrong?

Thanks

Link to comment
Share on other sites

Are you saying the console.log output was empty, or that the update.php you were redirected to was empty?

 

The first shouldn't happen. The second totally would: you did a redirect to that page. That is not AJAX. That is regular page navigation - one that doesn't include POST data.

Link to comment
Share on other sites

The point of ajax is that you don't load the page, you just send it data. Then, usually, display something on a success or failure.

 

The code here, I think, would load update.php twice. Once with a post to it and a second with a redirect. With ajax, you won't see the var_dump() unless you're in the console and look at the response of the request.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.