Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2011, 08:10 PM   PM User | #1
nxzmplty
New Coder

 
Join Date: Jan 2008
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
nxzmplty is an unknown quantity at this point
Can't figure out how to turn PHP variable into JQuery output

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.

PHP Code:
<?php 

// this is ajax.php

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

?>
PHP Code:
// 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

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



        </script>

        <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.
nxzmplty is offline   Reply With Quote
Old 11-16-2011, 08:16 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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
Code:
        $.ajax({  
            type: "POST",  
            url: "ajax.php",  
            data: "id="+this.parentNode.id+"&v=d",  
            success: function(data){  
               var newRateNum = data;
               $('a.rateNum').html(newRateNum);
            }  
            });
devnull69 is offline   Reply With Quote
Old 11-16-2011, 08:41 PM   PM User | #3
nxzmplty
New Coder

 
Join Date: Jan 2008
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
nxzmplty is an unknown quantity at this point
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
nxzmplty is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:32 PM.


Advertisement
Log in to turn off these ads.