Extract data from a . serialize

Asked

Viewed 997 times

0

I am sending data from a form using the $.get or $.post method The data is picked up by

 var dados = $(this).serialize();

My code:

$(".formResposta").submit(function(){

        var dados = $(this).serialize();

        $.get('sys/GRAVA.resposta.php',
            {
                dados : dados 

            }, function(retorno){
                $('.retorno4').show('400');
                $('#alert1').prepend(retorno);
            });
});

I would like to know how to extract the data, to be able to save in the database. Below the code you receive in GRAVA.resposta.php

 $dados  = $_GET['dados'];
  • This $data = $_GET['data'] is already picking up the amount sent. Exactly what you need?

  • I used to use it like this: $name = $_POST['name']; $reply = $_POST['reply']; $id_comment = $_POST['btnIDComent']; $Ipres = $_POST['btnIPres']; I know it sends name= and such. But how do I extract this data

2 answers

1


you can receive your data this way:

$dados  = unserialize($_GET['dados']);

dai cara information passed by form ira as an array

to see how your form is passing the instructions do the following command:

var_dump($dados);

Since php does not serialize in the same way as jquery we have 2 resolutions for the problem

1- function

function unserializeForm($str) {
$returndata = array();
$strArray = explode("&", $str);
$i = 0;
foreach ($strArray as $item) {
    $array = explode("=", $item);
    $returndata[$array[0]] = $array[1];
}
 return $returndata;
}

just use it like this

$dados  = unserializeForm($_GET['dados']);

or use php command for this

parse_str($_GET['dados'], $dados);

then $data will already be an array with your form data can give the var_dump in it.

  • With var_dump, it is passing like this: string 'name=Jos%C3%A9+Luis&reply=Teste&btnipres=%3A%3A1&btnIDComent=70'

  • so jquery serialize is not the same as php. I’ll see how to solve this give me 1 min

  • Okay, I’ll test it. Thank you

  • Perfect. Worked right with parse_str. Thank you very much

0

To be clear, I will post the exact codes. My form has the following values: Name, Answer, IP and Id_comment.

Function:

$(".formResposta").submit(function(){

        var dados = $(this).serialize();

        $.get('sys/GRAVA.resposta.php',
            {
                dados : dados 

            }, function(retorno){
                $('.retorno4').show('400');
                $('#alert1').prepend(retorno);
            });
});

SAVE.resposta.php:

      $dados  = unserialize($_GET['dados']);


  if(isset($_GET['nome']) && isset($_GET['resposta']) && isset($_GET['btnIDComent']) && isset($_GET['btnIPres'])){

$nome = $_GET['nome'];
$resposta = $_GET['resposta'];
$id_comentario = $_GET['btnIDComent'];
$ipRes = $_GET['btnIPres'];

        $db_resposta->setNome($nome );
        $db_resposta->setResposta($resposta);
        $db_resposta->setID_Comentario($id_comentario);
        $db_resposta->setIpRes($ipRes);


         if($db_resposta->inserirResposta()){
            $db_resposta->getNome();
            $db_resposta->getResposta();
            $db_resposta->getID_Comentario();
            $db_resposta->getIpRes();
         }
         else{
            echo 'Não foi possivel salvar a resposta!';

            }
  }
else{
   echo 'Error';
 }

I think it’s clearer. I want to take the data that comes through get and insert into the database. unserialize($_GET['data']) didn’t work

  • edited my answer above.

Browser other questions tagged

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