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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -