A
A
Andrew2014-04-07 19:15:30
PHP
Andrew, 2014-04-07 19:15:30

Ajax and dynamic select - how to implement?

! Everything is implemented in php + mysql.

There is a select with an option set, the values ​​for which are unloaded from the database.

While the user is browsing the page, the data in the select can be added. How, without reloading the page completely, but simply by clicking on a certain button, you can update the data in the select?

Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yunus Gaziev, 2014-04-08
@BBoyJuss

<select name="my-select">
  <option>...</option>
  <option>...</option>
  <option>...</option>
</select>

// jQuery
var myDynamoSelect = $('select[name="my-select"]');

$.ajax({
    type: "POST",
    url: "some.php",
    data: "name=John&location=Boston",
    success: function ( data ) {
      // empty() чистим HTML в селект
      // html(data) вставляем в select то что ответил сервер (<option>...</option>)
      // пример ответа: <option>...</option><option>...</option><option>...</option>
      myDynamoSelect.empty().html(data);
    }
 });

look for json - php - ajax information

N
Nazar Mokrinsky, 2014-04-07
@nazarpc

Get the data and insert it into the page.
What exactly are you having problems with?

I
Insayt, 2014-04-07
@Insayt

Make an ajax request to the server, get new data - and insert it into the select.
for example

// Пусть в res хранится ответ от сервера с данными
$('#id вашего селекта').append('<option>'+res+'</option>');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question