0

I have a form where the user can fill it and then if he need can add more field (I did it with js) by pushing a button. Now I would like to post via php the result but I don't know how to write the php side. I did try to follow directions in multi-dimensional array post from form but I could not sort it out. My html form:

<form role="form" method="POST" action="form.php">
<label>Ice Age Gelato</label>
<div class="multi-field-wrapper">
<div class="multi-fields"><div class="multi-field">
<input type="text" name="user[]">
<button type="button"   class="remove-field">Remove</button>
</div></div>
<button type="button" class="add-field">Add field</button>
</div>
<input type="submit" value="Send"> 
</form>

And this the relative part in form.php that I don't know how to write:

$user = $_POST['users'] = array(
array()
);

and:

user: $user,

Where is wrong? Thank you

Community
  • 1
  • 1
Massimiliano Rubino
  • 279
  • 1
  • 2
  • 16

3 Answers3

0

Change this

$user = $_POST['users'] = array(
   array()
);

To

$user = $_POST['user'];
  1. You are replacing the post values with your array() definition.

  2. You used user as input name and trying to get users from posted data.

Note: By defining input name as user[], you have already changed the value format to array. So no need to take an extra action to make it an array when getting it from posted data. You will get $_POST['user'] as an array with all input values as items. For example if you have two input values with "userA" and "userB". var_dump($_POST['user']) will give you like this -

Array (
 [0] => "userA",
 [1] => "userB",
)

EDIT

As you are trying to print $users as string to send to email, but it is an array. So, just join the array elements with comma to make it a string. Like this:

<? $user = $_POST['user']; mail("info@ice-cream-showcase.com" , "test" , "user: ".join(",",$user) , "From: $email\n "); ?>
  • I fill 3 field with 3 different random words and this is what I get in the email: user: Array, – Massimiliano Rubino Jan 18 '16 at 07:08
  • You cannot just echo / print an array, you need to iterate over each item in array and then print. Pls show your codes for sending email, so that we can better help. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 18 '16 at 07:13
  • $user = $_POST['user']; mail("info@ice-cream-showcase.com" , "test" , "user: $user," , "From: $email\n "); // email mail($email, "Ice", "Thanks", "From: info@ice-cream-showcase.com"); // user email ?> http://iceagethai.com/form_php.txt – Massimiliano Rubino Jan 18 '16 at 07:32
0

As your input name is user[] so replace

$user = $_POST['users'] = array(
    array()
);

to

$user = $_POST['user'];
AnkiiG
  • 3,468
  • 1
  • 17
  • 28
  • In this way, even if I fill 4 fiels I just receive the word "Array" in the email – Massimiliano Rubino Jan 18 '16 at 06:57
  • If you have 5 fields then when you `print_r($_POST);` then you will get `Array ( [user] => Array ( [0] => test1@gmail.com [1] => test2@gmail.com [2] => test3@gmail.com [3] => test4@gmail.com [4] => test5@gmail.com ) )` – AnkiiG Jan 18 '16 at 07:02
0

If you are using input field as a string as:

<input type="text" name="user[]">

Than you will result the result as like that:

echo "<pre>";
print_r($_POST);

Array
(
    [user] => Array
        (
            [0] => test
        )    
    [submit] => Submit Query
)

Now how can you get the users from this this multidimensional array:

As mentioned in other answers you will get the values as:

$user = $_POST['user'];
print_r($user);

// Result
Array
(
    [0] => test
)

Now how can you get this value from an array, follow this:

foreach ($user as $value) {
    echo $value. "<br/>"; // test
}
devpro
  • 16,184
  • 3
  • 27
  • 38
  • So in the form.php I should write: echo "
    ";
    print_r($_POST);
    
    Array
    (
        [user] => Array
            (
                [0] => test
            )    
        [submit] => Submit Query
    )
    $user = $_POST['user'];
    print_r($user);
    
    // Result
    Array
    (
        [0] => test
    )
    foreach ($user as $value) {
        echo $value. "
    "; // test } All together?
    – Massimiliano Rubino Jan 18 '16 at 06:56
  • @MassimilianoRubino: no, it just an example... how to get the valuues from form, your form is fine... u only need this in php... $user = $_POST['user']; print_r($user); – devpro Jan 18 '16 at 06:58
  • Yes, thanks. With this I print in the form.php but not in the email, how to transfer in the email? – Massimiliano Rubino Jan 18 '16 at 08:00
  • @MassimilianoRubino: can u show your email code?? do u want to show all emails in email body??? – devpro Jan 18 '16 at 08:02
  • @MassimilianoRubino: use this: $user = implode(",",$_POST['user']); instead of $user = $_POST['user']; – devpro Jan 18 '16 at 08:21