Jump to content
Larry Ullman's Book Forums

Passing Php Post Array To Javascript


Recommended Posts

Hello,

 

I have PHP file (file1.php) asking for a date:

echo '<form action="charts.php" id="form_chart_date" method="post">
<div id="form_chart_date">
<p>Please select date. </p>
<p><input name="date_looking" type="text">
<input type="submit" value="Display data" name="display_chart"></p>
</div>
</form>';

After submission file1.php handles the process but there is reference to JavaScript file which reads PHP generated JSON file.

JavaScript file looks like:

(document).ready(function() {
		var date = "<?php echo json_encode($_POST); ?>";
	$.ajax({
		data: {date_looking: date},
		url: "http://localhost:8888//live_site_local/www/json.php",
		dataType: "JSON",
		type: "POST",
		cache: false,
		success: function(pieData) {
				var ctx = $("#chart-area").get(0).getContext("2d");
				new Chart(ctx).Pie(pieData);
		}
	   });		
   });

If I replace variable date with something like:

var date = "2015-01-01";

then everything works ok.

It looks like the problem is that I can not pass PHP $_POST array to JavaScript.

 

Here is the line from PHP file (json.php) that generates JSON object.

$date_looking = $_POST['date_looking'];

Does anyone know what I am doing incorect?

 

Thanks

Link to comment
Share on other sites

Apply some debugging. If you look at your source code, you'll see that the data you try to echo to var date is not correct. Creating JSON out of $_POST will never get you a date.

 

You are looking for a date on the form "YYYY-MM-DD". To get that, you'll need something more like this:

var date = "<?php echo $_POST['date_looking']; ?>";

Some tips. Check you source HTML code and use the Firefox/Chrome debug console to catch any errors.

Link to comment
Share on other sites

 Share

×
×
  • Create New...