Jquery problem in handling ajax data coming as array

2

My array returns from php like this (post):

Array
(
    [0] => Array
        (
            [O email digitado é invalido! Por favor insira um email correto.] => 1
        )

    [1] => Array
        (
            [O telefone digitado está num formato invalido por favor siga o modelo: (DDD) XXXX-XXXX] => 1
        )

    [2] => Array
        (
            [Sua senha deve ser maior que 4 caracteres e menor que 20] => 1
        )

)

My goal is to make Jquery use the main array to identify the ex phrase:

1. Typed Email is ...  2. The typed Telephone is ...  3. Your password should be ....

And the internal array of the message identifies the type of css I'm going to use. 1 - serious error, 2 - common error, 3 - correctness.  I just do not know how. When I try to use a foreach, I come across an error in lenght, which returns undefined.  All help is welcome ...

============ ================ CODE PHP

<?php
$jq = filter_input_array(INPUT_POST, FILTER_DEFAULT);

if($jq['acao'] == 'cadastro'):
    unset($jq['acao']);
    require ('../_app/Models/Cadastro.class.php');
    require '../_app/Config.inc.php';

    $cadastra = new Cadastro;
    $cadastra->ExeCadastro($jq);

    if(!$cadastra->getResult()):
        print_r($cadastra->getError());
    else:
        print_r($cadastra->getResult());
    endif;
endif;

PHP gets jquery data from fields stored via post in variable jq and sends to class.

PHP CLASS ============== ===================== Filmography

private function CheckData() {
    if (!Check::Email($this->Data['email'])):
        $this->Result = FALSE;
        $this->Error[] = ["O email digitado é invalido! Por favor insira um email correto." => 1];
    endif;
    if (!Check::Telefone($this->Data['telefone'])):
        $this->Error[] = ["O telefone digitado está num formato invalido por favor siga o modelo: (DDD) XXXX-XXXX" => 1];
        $this->Result = FALSE;
    endif;
    if (strlen($this->Data['senha']) < 5 || strlen($this->Data['senha']) >= 20):
        $this->Error[] = ["Sua senha deve ser maior que 4 caracteres e menor que 20" => 1];
        $this->Result = FALSE;
    endif;
}

private function CheckCadastro() {
    $CheckExistUser = new Read;
    $CheckExistUser->ExeRead('cadastro', "WHERE email = :e", "e={$this->Data['email']}");

    if ($CheckExistUser->getResult()):
        $this->Error[] = ["O email {$this->Data['email']} já está cadastrado no sistema. Se você for ... clique aqui. Se não tente um outro email" => 1];
        $this->Result = FALSE;
    endif;
}

The class is larger a little more, follow this principle. If error in the data it stores received the $ this- > error and will not let proceed the registration in the database.  As given to see in PHP I recover the error by $ cadastra- > getErro, if you send him a print_f to print the message.  My goal now is to handle the message I received to display to the client. So that the main array is the identifier of the amount of error and the message of internal atibuição identify the type, if one is a mistake, I'll see the number type and identify which CSS will use to send the final message.

    
asked by anonymous 24.11.2016 / 17:01

2 answers

1

Use forEach.

   $.each(array, function( index, value ) {
         alert( index + ": " + value );
   });
    
24.11.2016 / 17:17
1

Do you have access to PHP code? The best (and simplest) way to handle this data is to prepare it for javascript using the json_encode($array); function. Now it will have to use regular expression and will lose performance and integrity in this data.

    
24.11.2016 / 17:22