pull information via php ajax

Asked

Viewed 279 times

0

good afternoon, I am studying ajax and I am trying to make an input pass to a file via post, but nothing happens.. I am following via Chrome firebug and I see that always the type: 'post' is in error.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script>
function ajax(nome){
    info = {"nNome" : nome};

    $.ajax({
        type: 'post';
        url: "arquivo.php";
        data: info,
    }).done(function(data) {
        data = $.parseJSON(data);
        $(".resultado span.nome").text(data.nNome);
    });
}

    $(document).ready(function(){

        $("input[name=Enviar]").click(function(){
            var nome = $("input[name=nome]").val();
        });

    });

</script>

<form>
<label>Entre com seu nome:</label>
<input type="text" name="nome"><br>
<input type="button" name="Enviar" value="Enviar">
</form>

<div class="resultado">
Olá, <span class="nome"></span>
</div>
  • 1

    The mistake are the ; inside object, you should be using commas.

  • OK, I made the changes and really that was the mistake, but I still can’t display the result...

  • Please show what your.php file does.

  • <?php&#xA;&#xA;echo json_encode($_POST);&#xA;&#xA;?>

  • data.nNome should also not be data.nome? Anyway, it will be hard to see the value change if what you return in PHP is the same as what you just sent. The .text() will trade six for half a dozen.

  • then as to the date.nName it is pulling via Indice.. which comes through the info = {"nNome" : nome }; , it should appear in the div below... it’s just a test to learn and apply in other files...

Show 1 more comment

1 answer

2


There are several small errors in your code, try this modified version:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<form>
<label>Entre com seu nome:</label>
<input type="text" name="nome"><br>
<input type="button" name="Enviar" value="Enviar">
</form>

<div class="resultado">
Olá, <span class="nome"></span>
</div>

<script>
function ajax(nome){
    var nome = ;
    var info = {"nNome" : nome};

    $.ajax({
        type: 'post',
        url: "arquivo.php",
        data: info,
    }).done(function(data) {
        data = $.parseJSON(data);
        $(".resultado span.nome").text(data.nNome);
    });
}

$(document).ready(function(){
    $("input[name=Enviar]").click(function(){
      ajax( $("input[name=nome]").val() );
    });
});
</script>
  • thank you very much.. just have to remove the name variable... and it works normal, thank you very much! now I’ll buy the codes to see what my mistakes were...

  • Its name variable only existed inside that click function (because it had been declared there). Also, the function ajax was never called!

  • right, next I tested this code with a schema to pull the address, it is passing the correct post however it is not returning the value...

Browser other questions tagged

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