spenceddd Posted July 10, 2011 Share Posted July 10, 2011 Hi Guys, I've been doing some research and this to be a popular way to tackle this but for some reason it's not working for me: <?php $test01=$_POST['test']; $test02=$_POST['test2']; echo($test01); ?> <html> <head> <title>Portfolio item logger</title> <script src="AC_RunActiveContent.js" language="javascript"></script> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script type="text/javascript"> //var existingPortfolioItems = new Array(); alert(<? $_POST['test']; ?>); var a = {}; a['test'] = 1; a['test2'] = 2; $.ajax({ url: "http://www.spencercarpenter.co.uk/portfolioAppFiles/jsArrayToPhpTest.php", data: a, type: 'post', success: function(data) { }}); </script> <script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script> </head> <body> <form action="jsArrayToPhpTest.php" method="post"> <input name="" type="submit"> </form> </body> </html> If anyone could help me I would be very grateful. I think I'm missing something fundamental here... Thanks Spencer Quote Link to comment https://forums.phpfreaks.com/topic/241610-passing-js-array-back-to-php-on-the-same-page/ Share on other sites More sharing options...
.josh Posted July 10, 2011 Share Posted July 10, 2011 Okay, it looks like you are trying to make an ajax request to the same script as the ajax request. This creates a blackhole in the internets. Basically the idea is when you make an AJAX call, it makes a request to some script, and if you want to "test" what is received, you have to output it (which you did with your echos)..but this just gets returned to your success callback function in your data variable. You have to then actually display it somewhere, like updating the innerHTML of a div or something. So basically it should be working for you, but you aren't "testing" it correctly. Here is a more simplified version, using a single script URL, with some comments: <?php // check if something is posted if ($_POST) { // dump out what is posted. This is what is going to be output for the AJAX call echo "<pre>";print_r($_POST); echo "</pre>"; // exit the script so that it doesn't output the rest of the stuff in the script exit(); } ?> <html> <head> <script src="jquery.js"></script> <script type="text/javascript"> var a = {}; a['test'] = 1; a['test2'] = 2; $.ajax({ url: "test.php", data: a, type: 'post', // this is your callback function success: function(data) { // update the contents of a div with the results of the ajax call $('#someDiv').html(data); } }); </script> </head> <body> <!-- div to update with AJAX results --> <div id='someDiv'></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/241610-passing-js-array-back-to-php-on-the-same-page/#findComment-1240994 Share on other sites More sharing options...
spenceddd Posted July 11, 2011 Author Share Posted July 11, 2011 Thanks for your help Crayon. I have tried to test your code but the php echo doesn't seem to echo. I also had to add the submit button on the page so I could trigger and test that php was picking up the array on reloading the page: <?php // check if something is posted if ($_POST) { // dump out what is posted. This is what is going to be output for the AJAX call echo "<pre>";print_r($_POST); echo "</pre>"; // exit the script so that it doesn't output the rest of the stuff in the script exit();} ?> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script src="jquery.js"></script> <script type="text/javascript"> var a = {}; a['test'] = 1; a['test2'] = 2; $.ajax({ url: "test.php", data: a, type: 'post', // this is your callback function success: function(data) { // update the contents of a div with the results of the ajax call $('#someDiv').html(data); } }); </script> </head> <body> <form action="test.php" method="post"> <input name="" type="submit"></form> <!-- div to update with AJAX results --> <div id='someDiv'> </div> </body> </html> Sorry if I'm missing something obvious here... Thanks again for any help on this! Quote Link to comment https://forums.phpfreaks.com/topic/241610-passing-js-array-back-to-php-on-the-same-page/#findComment-1241156 Share on other sites More sharing options...
spenceddd Posted July 11, 2011 Author Share Posted July 11, 2011 Just to elaborate on what I am trying to achieve here (incase I have overcomplicated it in the first place). I am updating a mysql database with information on the page which ultimiately ends up in a multidimensional js array. So when the page reloads it needs to update the mysql table before the same page then creates the table in html further down in the code. I have used JSON encode to pass info from php to js with great success but through my research I am coming up with mainly this ajax jquery function to pass from js to php via POST. Hope that makes sense. Please let me know if there is a simpler way to achieve this. Thanks for any advice here. Spencer Quote Link to comment https://forums.phpfreaks.com/topic/241610-passing-js-array-back-to-php-on-the-same-page/#findComment-1241157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.