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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.