Jump to content

Passing js array back to php on the same page


spenceddd

Recommended Posts

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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

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.