Unable to transfer data. There is a list of cities:

<li><a href="#" id="post_btn">Белгород</a></li> ... <li><a href="#" id="post_btn">Рязань</a></li> 

When you click on the link to the server, a post request must be sent containing the city you clicked on. I tried to do this:

 <a href="#" onclick='$.post("index.php", {name: "Рязань"});'>Рязань</a> 

Does not work. Tell me, please, how to do it.

  • what gives? what's in the console? What about xhr queries that can be viewed with the firebug plugin? - thunder

2 answers 2

First, the id of the element must be unique, so in your case you need to use the class:

 <li><a href="#" class="post_btn">Белгород</a></li> ... <li><a href="#" class="post_btn">Рязань</a></li> 

Next, hang the handler for links with the desired class:

 $(document).ready(function(){ $(".post_btn").click(function() { name = $(this).text(); $.post("index.php", {name: name}); }); }); 
  • Thanks, I understood that I don’t need to place a handler in the tag, but I didn’t know how to pass the current city value to a function. Could you please tell me whether the absolute url or relative is indicated? In this case, the request is processed by the page that sends. - devmonkey
  • The path is prescribed either relative or from the root. Absolute is definitely not needed. - Indifferent

As rightly noted in the answer above, this should work. I would advise you to use the wonderful Firefox extension called Firebug - it makes debugging and testing client code a lot easier. Your error most likely lies in something that is not indicated in your piece of code. And a little advice - do not put event handlers inside HTML tags, it’s still better to separate javascript code from markup.