javascript - How to get link specific php varaible to pass through ajax -
i dont know ajax or jquery yet have ajax script send variable through , work properly.
--the way have set loop:
<?php $tt= mysql_query("select * monsters"); while ($row= mysql_fetch_array($tt)) { $namee= $row['name']; echo "<a id='namee' onclick='post();'>$namee</a>", "</br>"; }
which echos:
horseman dragon
---the results make list of names table shown above , clickable working great
my ajax request this:
<script type="text/javascript"> function post(){ var hr = new xmlhttprequest(); var url = "mess.php"; var vars =document.getelementbyid("namee").innerhtml; hr.open("post", url, true); hr.setrequestheader("content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if(hr.readystate == 4 && hr.status == 200) { var return_data = hr.responsetext; document.getelementbyid("new").innerhtml = return_data; } } hr.send(vars); // execute request alert(vars); } </script>
the alert when click on horseman returns "horseman" when click on dragon still alerts "horseman"
i specific variable ( when click horseman says horseman , when click dragon says dragon etc) can send through ajax update database ( know to set up)
please me , show me if can full code can learn , see how works , understand response since im new said :)
thanks in advance:
if have questions feel free ask
thats because ids should unique, jquery first element id, suggestion pass value parameter:
echo "<a class='namee' onclick='post($namee);'>$namee</a>", "</br>";
and js:
function post(vars) { var hr = new xmlhttprequest(); var url = "mess.php"; hr.open("post", url, true); hr.setrequestheader("content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function () { if (hr.readystate == 4 && hr.status == 200) { var return_data = hr.responsetext; document.getelementbyid("new").innerhtml = return_data; } } hr.send(vars); // execute request alert(vars); }
Comments
Post a Comment