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

-1

This is the Ajax code

$('#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 type alert('html1') and then alert('html2') type does anyone know how?

    
asked by anonymous 14.02.2016 / 20:16

1 answer

1

You can use JSON, it's your friend.

  

JavaScript:

In Javascript / JQuery you will need to declare some things, first the dataType and then create a loop to read all 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 exit in JSON, so use json_encode() , but make what you want in a array() , that way you 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

?>
    
14.02.2016 / 20:36