Computer Science Canada

Help on passing variables from php to javascript

Author:  nonamedude [ Sat Apr 09, 2011 4:43 pm ]
Post subject:  Help on passing variables from php to javascript

Hey guys, I am trying to get a value from a php script and into my JavaScript function but nothing seems to be printing out. Right now i have
code:

 <?php
require ("connect.php");

echo "connected!<br>";

$extract = mysql_query("SELECT * FROM articles")or die ("Not working");
//$numrows = mysql_num_row($extract);
while ($row = mysql_fetch_assoc($extract)) {
    $id = $row['id'];
    $article = $row['article'];
    $thumbsup= $row['thumbs_up'];
    $thumbsdown = $row['thumbs_down'];
    $date = $row['date_time'];
    $username = $row['username'];

}
$thumbsup++;
mysql_query("UPDATE articles SET thumbsup = $thumbsup");
$Response = array( 'Id' => $id,  'Article' => $article,'ThumbsUp' => $thumbsup);

echo json_encode($Response);
exit;

If i echo, it does echo the values of the variables, but then when i transfer it over.

code:
xmlhttp.onreadystatechange=function()
                {
                    if (syndLinkRequest.readyState != 4)
                        {
                    return;
                        }
                    //if (xmlhttp.readyState==4)
                   // {
                        var resp = xmlhttp.responseText;
                        document.getElementById('c').innerHTML= resp;
                   // }
                }
                xmlhttp.open("GET", "http://localhost/ajax/folder2/includes/mysql.php", true);
                //xmlhttp.open("GET","http://localhost/ajax/folder2/includes/a.txt",true);
                xmlhttp.send(null);
            }


I am trying to print out the results to a div which is in the same area as the javascript

code:
<a
                            onclick ='changevalandcolup();'
                             >
                             <img name=imgName border=0 src='images/thumbup_gray.png'
                               ></a>
                             </td>
                            <td>
                         <div id='c'>0</div>


And the function changevalandcolup, basically changes the color of the image, and then calls the function to change the value. Anyone know how i can do this :/

Author:  nonamedude [ Sat Apr 09, 2011 4:50 pm ]
Post subject:  Re: Help on passing variables from php to javascript

*bump*

Author:  Amailer [ Sat Apr 09, 2011 4:54 pm ]
Post subject:  RE:Help on passing variables from php to javascript

http://api.jquery.com/jQuery.ajax/
jQuery will make your life much easier.

Author:  nonamedude [ Sat Apr 09, 2011 5:33 pm ]
Post subject:  Re: Help on passing variables from php to javascript

Ty, i started using jquery, and its like a blessing from heaven lol

Author:  nonamedude [ Sat Apr 09, 2011 7:11 pm ]
Post subject:  Re: Help on passing variables from php to javascript

Hey guys,

I just wanna know, how do you use mysql with jquery, meaning, how do you print out something from a database.

Author:  Amailer [ Sat Apr 09, 2011 7:41 pm ]
Post subject:  RE:Help on passing variables from php to javascript

Take a look at:
http://www.electrictoolbox.com/json-data-jquery-php-mysql/


: