View Full Version : Can't figure out how to turn PHP variable into JQuery output

11-16-2011, 08:10 PM
Right, I apologise well in advance for coming to you all with the most simple question you've probably ever seen. I can not for the life of me get my head around this.


// this is ajax.php

$newRateNum = "1";
echo json_encode($newRateNum);


// this is index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript" charset="utf-8">

// What we want to do here is
// Grab the ID
// Generate the variables
// Get a return from the PHP
// Update $('span#'+id+' a.rateNum') with that return


$("a.rateDn").click(function() {
// we want to store the values from the form input box, then send via ajax below
type: "POST",
url: "ajax.php",
data: "id="+this.parentNode.id+"&v=d",
success: function(){
return false;


<span class="rate" id="i79341"><a href="#" class="rateDn">[-]</a><a class="rateNum">0</a><a href="#" class="rateUp">[+]</a></span>

Seriously, clearly the problem is with the "$('a.rateNum').html($newRateNum);". All I want to know is, what the hell goes in the second set of brackets?

I simply want to replace the existing "0" in a.rateNum with whatever I output as $newRateNum in PHP.

Again, sorry because I know this is a question fit for a 6 year old - I just kind of suck at the whole client side coding :(

EDIT: Sticking an alert("whatever") in the success{} bit will do its job correctly, so I am getting that far for sure.

11-16-2011, 08:16 PM
The sticking point is that you cannot just presume that the variable name from PHP will be available to jQuery/Javascript "just like that".

PHP creates output, this output is sent to the browser and will then automatically be available as the first parameter of the success callback

type: "POST",
url: "ajax.php",
data: "id="+this.parentNode.id+"&v=d",
success: function(data){
var newRateNum = data;

11-16-2011, 08:41 PM
Thank you so very much. I'm sure I'll have some more questions in good time but this is exactly what I needed to progress :)