1

I have a very long form in a php template, all data are send by a jquery ajax in serialize mode to a php page to send an email.

I have this problem: How I can unserialize the data in the php page, and send them by email ?

The php template is completed with different data. So I can't use for each fields just urldecode in this way $nome = urldecode($_POST['nome']);

I have to find a way for take all the data arrive in the php page and send them in an email. I read about parse_str But I don't know if can works and how use it.

At moment this is my ajax code:

var datiform = $("#FormWorkspace").serialize();    

$.ajax({

        type: "POST",
        url: ".../form-cf/engine.php",
          data: datiform,
          dataType: "html",
        success: function(datiform)
            {
          alert("success" + datiform);
            },
        error: function()
            {
          alert("no success " + datiform);
            }
        });
Marco Romano
  • 272
  • 1
  • 14
  • 1
    you'd use parse_str like this: `parse_str($_POST['data'], $myPostVar)` and then you can use `$myPostVar` like `$_POST` - `$myPostVar['name']` – treyBake Apr 18 '18 at 08:23
  • 1
    https://stackoverflow.com/questions/1792603/how-do-i-php-unserialize-a-jquery-serialized-form – deEr. Apr 18 '18 at 08:25
  • Thank you @ThisGuyHasTwoThumbs in this way I have to write `$myPostVar['name']` for each fields? can I make a loop for generate automatically the data? – Marco Romano Apr 18 '18 at 08:26
  • @ajax yes I was reading this, but if i make echo `json_encode($data);` it will return all the data? but for send it in the email, I have to add one by one. no? – Marco Romano Apr 18 '18 at 08:29
  • 1
    [Have you read this?](https://stackoverflow.com/a/3817220/2569323) – deEr. Apr 18 '18 at 08:31
  • You can `loop` over `$_POST`. – deEr. Apr 18 '18 at 08:32
  • thank you @AjAX. ok I start to understand that i can use ` $params = array(); parse_str($_GET, $params);` like in the post, but I don't have clear how I can use it, and how sue for send the email. How I can extract the data make the loop and send it by email. Sorry I try to learn is first time i do this. – Marco Romano Apr 18 '18 at 08:38
  • 1
    First you do, what @ThisGuyHasTwoThumbs said. As in the answer i linked. Then you do `foreach ($myPostVar as $item) { /*Do what you want in your email.*/}`. http://php.net/manual/en/control-structures.foreach.php. – deEr. Apr 18 '18 at 08:43
  • @AjAX. thank you! I think to start to understand. I will try ti. If you want you can answer below. – Marco Romano Apr 18 '18 at 08:46
  • Pleasure. I hope everything works out. – deEr. Apr 18 '18 at 08:48
  • @AjAX. In my case I will use `parse_str($_POST['datiform'], $myPostVar)` ad in the loop i can just do `
  • $item
  • `. in this way i will read all the data in the string in a list. – Marco Romano Apr 18 '18 at 08:53
  • Sounds fair. %)P – deEr. Apr 18 '18 at 08:54