Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    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);
                }  
                });

  • #3
    New Coder
    Join Date
    Jan 2008
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •