How do I receive the value of Variables Separately from PHP?

Asked

Viewed 60 times

-1

This is the code of Ajax

$('#FormEs').submit(function(e){
    e.preventDefault();

    $.ajax({
        url: "VerificarPerson.php",
        type: "POST",
        dataType:"html",
        data: {
            'metodo': $('#metodo').val(),
            'idp': $('#idp').val(),
            'personName': $('#personName').val()
        }
    }).done(function(data){
        alert(data); // <------------------------------------------------

    });

});

and This is PHP

<?php
    if(strcasecmp('Teste', $_POST['metodo']) == 0){
        $html1 = $_POST['personName'];
        $html2 = $_POST['personName2'];
        echo $html1; /

  }
?>

Well I wanted to show the two variables like alert('html1') and then alert('html2') like this someone knows how?

  • Honestly, I used the PHP manual to see what this was strcasecmp(), but your question is simple. : P

  • I’m impressed, people are negative without at least trying to help or at least criticize what you’re wrong about.

  • because it is a friend I signed up one day this time I knew that here at Stackoverflow I could ask for help more apparently some people do not understand this

1 answer

1


You can use JSON, it is your friend.

Javascript:

In Javascript/Jquery you will need to declare some things, first the dataType and then create a loop to read all the data from JSON.

$.ajax({
//...
dataType: "json",
//...
}).done(function(data){

for(var i = 0; i < data.length; i++) {
    alert(data[i]);
}

});

PHP:

PHP also needs to quit in JSON, so use json_encode(), but make what you want into a array(), this way will have more than one data.

<?php

        $html[] = $_POST['personName'];
        $html[] = $_POST['personName2'];
        // Este [] irá tornar o html em uma array
        // Será: array('personName', 'personName2');

        echo json_encode($html);
        // Irá sair: ["personName","personName2"]
        // Que será lido pelo data[i] no Javascript

?>
  • Thanks buddy, it was the way I wanted xD

Browser other questions tagged

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