开发者

re-fetch request in php and javascript

开发者 https://www.devze.com 2022-12-14 23:34 出处:网络
here it goes; echo \"<a href=\'#\' class=\'thumb\'><img class=\'thumb-img\' value = \".$row->aid.\" onclick=\'getVote(\".$row->aid.\", \\\"\".$row->atitle.\"\\\")\' src=\'images/roa

here it goes;

echo "<a href='#' class='thumb'><img class='thumb-img' value = ".$row->aid." onclick='getVote(".$row->aid.", \"".$row->atitle."\")' src='images/roadies/th".$row->aid.".jpg' /> </a>";

the above function sends the "$row->aid" value to a javascript function through ajax.

in the javascript however, i want to make a function that needs the ++value of the $row->aid variable. i want the php to get the new value and then pass it again to javascript.

how do i 开发者_开发问答do it without a page reload?

to make things more clear, i just need to get the next incremented value of the php variable. i want php to get the next ++ value from the DB and pass it back to JS.

please help me do this. ;))


Maybe I'm misunderstanding,
But can't you just have a PHP script that returns a string, or a JSON object containing the value(s) you need, and then have the javascript function grab it from the server and update the properties in the link/image?

Also, typically you would get $row from mysql_fetch_assoc() or a similar function -- by calling the server again, re-querying the database, and iterating over mysql_fetch_assoc the necessary number of times, you're probably wasting time. Wouldn't it be easier for the PHP script to grab all of the rows, store them in JSON objects, and then (rather than using AJAX) having the javascript iterate through the JSON object and update the properties on each click?

update:

JSON is a text-based way to represent an object in JavaScript. Think of it like a multi-demensional array. http://en.wikipedia.org/wiki/JSON

You'll have a PHP script write a JSON object to a variable, which you would echo into a <script> tag like echo "var rows = {$JSON_obj};";. In javascript you would start with rows[1] and then each time the link is clicked, move to the next rows[i] or whatever.

jQuery is your friend here. It'll make AJAX (for wherever else you use it) and changing HTML elements much easier. I moved to jQuery and have never, ever looked back to the old ways. http://jquery.com/

I don't have an example on hand right now, but it's a pretty simple concept. I'm sure you'll be able to code this up with little effort.


you can write it to the standard output in PHP via echo $value; and finish PHP script there. javascript will then fetch the value and return it in the AJAX processing function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号