Pergunta

I have a simple <input> and when user writes there something I need to refresh php inside div so it will look like some.php?key=thevaluefrominput. How would I do that? I guess I need to use query but I'm not sure how. I want something like this when you write something to Type to find tags it changes the the tags bellow.

Thanks for the answers.

Foi útil?

Solução

This sounds like a job for AngularJS :) However this is jQuery solution:

$(function () { 
  $('form').submit(function (e) {
    e.preventDefault();       
    $.ajax({
        type: 'POST',
        url: 'some.php',
        data: 'key=' + escape($.trim($('#myinputfield').val())),
        dataType: 'html'
    }).done(function (data) {
        if (data) {
            $('#divtopresent').html(data);
        }
    });
  });
}); 

If you mean that while user types (before submission), div content changes? Then remove

  $('form').submit(function (e) {
    e.preventDefault();

and instead put

$('#myinputfield').keydown(function () { 

There is a setInterval method that I mentioned in comment, so the first chunk of code I posted, replace it with this one:

$(function () { 

  var old_val = '';
  var curr_val = '';
  setInterval(function () {

    curr_val = $.trim($('#myinputfield').val());
    if (old_val != curr_val) {
      old_val = curr_val;
      $.ajax({
        type: 'POST',
        url: 'some.php',
        data: 'key=' + escape(curr_val),
        dataType: 'html'
      }).done(function (data) {
        if (data) {
            $('#divtopresent').html(data);
        }
      });
    }

  }, 2000);

}); 

It checks if value of the field changed every 2s, please replace amount in ms (2000) if you like.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top