Value is not being taken via POST

Asked

Viewed 39 times

0

In the index.php has 1 field input that sends a value per ajax, which must be read by cdb.php.

index php.:

<script type="text/javascript">
        $(document).ready(function(){
            $("#formulario").on("submit", function(e){
                e.preventDefault();
                var cdb = $("#cdb").val();
                $.ajax({
                    url: "cdb.php",
                    method: "POST",
                    dataType: "html",
                    data: cdb
                }).done(function(data){
                    $("#cdb").val("");
                    $("#cdb").focus();
                    listar();
                }).fail(function(data){
                    alert("Erro");
                });
            });
        });
</script>

...

<form id="formulario" method="POST" action="" accept-charset="utf-8">
        <div>
            <?php echo rand(1, 100) ?>
            <input id="cdb" type="text" name="cdb" class="validate" required autofocus onfocus="this.value='';">
            <button type="submit">GRAVAR</button>
        </div>
</form>

cdb.php

$cdb = filter_input(INPUT_POST,'cdb');
  • 1

    In the ajax request try the following: data: {cdb: cdb},. in cdb.php does echo 'YOO ' .$_POST['cdb'];die(); to see if it returns this as a response

  • @Miguel just put data: {cdb: cdb} and it worked ! Could you please publish the answer and explain why you have to do it this way ? Thank you very much !

  • I can’t right now, I’m in the car, but I believe some colleague might be able to do this favor

  • This request leads to a certain php controller?

  • 1

    @Miguel tranquil ! They already answered ! But thank you for your attention !

  • Gabriel, no controller, just a basic script ! But thank you !

Show 1 more comment

1 answer

1


The problem is that you are passing information without any reference. The property data of ajax gets a array of the kind key and value, that is, for each item it is necessary to have a key and a value, in your code you are only passing the value of cdb, to pass the key too you could do something like:

$("#formulario").on("submit", function(e){
    e.preventDefault();
    var cdb = $("#cdb").val();
    $.ajax({
    url: "cdb.php",
        method: "POST",
        dataType: "html",
        data: {
            cdb: cdb
        }
    }).done(function(data){
        $("#cdb").val("");
        $("#cdb").focus();
        listar();
    }).fail(function(data){
        alert("Erro");
    });
});

But if your form had more fields, you would have to do them one by one. To our delight there is a function that is responsible for taking all the fields of a form and transforming into an array of type key and value, in case the key will be the property name of the field and value will be the very one value field. This function is the serialize() to use it would simply be like this:

$("#formulario").on("submit", function(e){
    e.preventDefault();
    var cdb = $("#cdb").val();
    $.ajax({
    url: "cdb.php",
        method: "POST",
        dataType: "html",
        data: $("#formulario").serialize()
    }).done(function(data){
        $("#cdb").val("");
        $("#cdb").focus();
        listar();
    }).fail(function(data){
        alert("Erro");
    });
});
  • Perfect guy ! This kind of answer with fundamental complements (serialize) are awesome ! Congratulations and thanks !

Browser other questions tagged

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