Send without giving Refresh and using onchange

Asked

Viewed 565 times

1

I want to send without refreshing my page and using on change I’m using the code below

Form:

<form action="" id="ajax_form" method="post" >
      <input type="text" name="resultado1" size="5" onChange="envia()">
</form>

Sending Script

jQuery('#ajax_form')(function envia() {
    var dados = jQuery(this).serialize();
    jQuery.ajax({
        type: "POST",
        url: "resultados.php",
        data: dados,
        success: function(data) {
            alert(data);
        }
    });
    return false;
});
  • Hello Matheus, welcome! Want to send the data when which/which inputs change(m)? Do you have a button that sends the form?

  • I send it without the use of the button, with the onChange command for every time the value is changed it send, so it is used Submit(). he refresh the page, so I called a function to send, I tried using javascript and on change but I could not set the code

  • Yes, if you use submit() it refreshes the page. You can make a jsFiddle to be clearer as your code is?

  • I edited the code in the publication, and so it is at the moment

  • This code is a little strange... there’s nothing missing here: '#ajax_form')(function?

  • it is so , and I do not use much jscript so I do not know if there is something wrong

Show 1 more comment

1 answer

0


If you want to recover the input value, put an identifier in it. For example id="meu_input"

<input id="meu_input" type="text" name="resultado1" size="5" onChange="envia()">
    </form> 

Then search for the value of this input by the ID. So:

$.post("resultados.php",{resultado1: $("#meu_input").val()},function(){});

I only modified your code, I’m not sure if use this way the event, will work, since you are using Jquery, I recommend you take the event change, thus (After defining the ID):

$("#meu_input").change(function(event){
    alert($(event.target).val());
});

Applying the sending of your code:

$("#meu_input").change(function(event){
    $.post("resultados.php",{resultado1: $(event.target).val()},function(){});
});
  • I am already using id value, and n Union

  • But does the event trigger? Already put an "Alert" inside the send function, to see if the problem is in the send, or in the function call?

  • the problem is in the function call, I’ve done this

  • I modified the answer, so it should work.

  • worked 100% vlw even

Browser other questions tagged

You are not signed in. Login or sign up in order to post.