On the site a list of cities

<li class="post_btn">Москва</li> 

The user clicks on a specific city and this value is sent by post-request to the server. Task: as soon as the server receives a new value, there is a selection from the base, taking into account the new city. On the site in the field "Your city .." the value of the city changes and the plate of offers for this city is displayed.

 if (isset($_POST["city"])) { $city = $_POST["city"]; select_db_function(); } 

Here is how the data is sent:

 $(document).ready(function(){ $(".post_btn").click(function() { city= $(this).text(); $.post("/lending2/index.php", {city: city}); }); }); 

But the site does not make any changes. The select_db_function() function itself is working, because when a city is sent via an input form, everything works. I can not understand what the problem is, I ask for help.

  • @sof_ka, To format the code, select it with the mouse and click on the {} button of the editor. - Photon
  • And what do you return in response? POST-request data is gone, processed, it would be necessary to return the result or something ... Yes, and processing the response in JS also does not hurt. - Deonis

2 answers 2

And where is the function that handles the server response?

 $.post("__url__", { __данные__}, function(result) { /* result - данные, которые отдаёт сервер Здесь их надо куда-то вывести */ } ); 
  • Can you please tell me how to correctly write a function that processes the response from the server? It is necessary to insert the city into the contents of the <div id = "status"> block of your city .. </ div>. I tried it like this, but it didn't work: function (result) {$ ("# status"). Html (result); } - devmonkey
  • But it should have turned out .. Use the debugger: for Firefox - Firebug add-on, in the rest - Ctrl + Shift + I I repeat the question: what does the file / lending2/index.php return? - Photon

Why so wise. JQuery has a load () function;

It works like this

  $(document).ready(function(){ $(".post_btn").click(function() { city= $(this).text(); $('#status').load("/lending2/index.php", {city: city}); }); }); 

// in the index.php file, find the necessary records in the database and just output the html code with the name of the desired city

  echo '<div class="city">ваш код</div>'; 

The load () function in our example loads the html code received from the server inside a block with status

in the end it turns out like this

  <div id="status"><div class="city">ваш код</div></div>