Post not sent immediately with ajax

user3079616
<script src="http://www.ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<form>
<label>Campo</label>
<input id="campo" name="campo" placeholder="Campo" required="" type="text">
</form>  

<script language="JavaScript" type="text/javascript">
        $(document).ready(function() {
            $.ajax({
            url: "control.php",
            type: "POST",
            data: "campo=" + $("input#campo").val(),
            success : function(data) {
                if (data == 1) {
                    $("#div").html("Campo non disponibile");
                }
                else {
                    $("#div").html("Campo disponibile :)");
                }
            }
            });
        });
</script>

I saw with Firebug that the POST contains the value of the input field is sent only updating the page and not immediately. Why?

nietonfir

You don't have a proper click/submit handler registered for the form submission. Instead, you call $.ajax only on document ready. That's why you only see the submission on page load (or to be more accurate, document ready).

You need to listen to the submit event, so change your code to something like the following

$(document).on("submit", "form", function(e) {
    e.preventDefault();
    $.ajax({
    …
    });
});

You don't need to wait for the document to be ready if your javascript is at the end of the page (it's always ready at that point). And I noticed that you include both v1.9 and v.1.10 of jQuery and that's generally not needed nor advisable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My ajax POST is not being sent to my servlet

How to save image on server sent with POST ajax

Retrieve post variable sent via Ajax in Django

Ajax post - Sent array as data to PHP

Not recieving values sent via AJAX POST to PHP

Create and download PDF based on JSON sent by AJAX POST request

jQuery Ajax POST request being sent as GET with Laravel

Ajax post data null when being sent to controller

How to get data out of $_POST after it was sent with $.ajax

Action method is not called from $.ajax POST whenever the sent text contains "<"

Object not received in Backend POST method as sent through $.ajax

Ajax vanilla Js sent post request is showing a blank body

Laravel 5 : Retrieve sent POST data through ajax

jQuery.ajax int is being sent but $_POST returns NULL

Immediately tell which output was sent to stderr

Can't get access to data sent from a server as POST AJAX in JSON

How to retrieve data on server side with node.js sent by frontend ajax via post method?

NodeJS How to get Data in server, sent from jquery ajax call via POST

XML sent using ajax POST not being received server side (Laravel 5)

after ajax form submit value from ckeditor textarea is not sent through post

AJAX JQuery PHP Convert name to match query columns with the data sent with POST variable

jQuery Ajax PHP POST method with response array[] instad sent data. How to get send values in response?

POST request is not sent

jquery .post() is sent as .get()

Headers not sent with ajax request

Ajax check sent data

Ajax fail but message is sent

Data sent in ajax call

ajax result not populating element immediately