Tom99 Posted September 29, 2017 Share Posted September 29, 2017 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: <?phpvar_dump ($_POST);?> Any idea what's going wrong? Thanks Link to comment Share on other sites More sharing options...
requinix Posted September 29, 2017 Share Posted September 29, 2017 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 More sharing options...
Sepodati Posted October 1, 2017 Share Posted October 1, 2017 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 More sharing options...
sumitkp Posted October 1, 2017 Share Posted October 1, 2017 send the text as string format. it will work. Link to comment Share on other sites More sharing options...
requinix Posted October 1, 2017 Share Posted October 1, 2017 send the text as string format. it will work.Nope. Link to comment Share on other sites More sharing options...
phpmillion Posted October 2, 2017 Share Posted October 2, 2017 As requinix correctly suggested, remove redirect to update.php first. It will make easier to debug. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.