2

Hi everybody i need help! I've starting to learn php some week ago. i have a form that POST some text field for a contact, i need to split the array in some variables to put in a email. this is my code

$field  = $_POST['input'];
if ( isset( $field ) === TRUE){

foreach ($field as $key => $value) {

    echo '<pre>' , print_r( $value ) , '</pre>'; //to list the array

    }

    $to = $mail;
    $sbj = "thanks to register to $site";
    ..//some headers
    mail($to,sbj,$headers)

}

and this is the form

<form action="upload.php" method="POST">
 <input type="text" name="input[]">
 <input type="text" name="input[]">
 <input type="text" name="input[]">
 <input type="submit" value="invia">
</form>

any suggestion for retrive the variables on the array to include on the mail?

0

3 Answers 3

2

@John has given the right procedure . You can use another procedure by using array_push() function eg:

$field  = $_POST['input'];
$info =  array();

foreach ($field as $key => $value) {
 array_push($info,$value);
}

// echo implode(",",$info);
$to = $mail;
$sbj = "thanks to register to $site";
$body = implode(",",$info);
..//some headers
mail($to,sbj,$body,$headers)
Sign up to request clarification or add additional context in comments.

1 Comment

it's the same if i use input type="file" for input?
1

You can use the implode() function , which takes an array and a separator and joins the elements of the array together into a string, with each element separated by the separator string you pass it eg:

foreach($field as $key=>$value) {
    $input[] = $value;
}

$body = implode("\n",$input); //implode here

$to = $mail;
$sbj = "thanks to register to $site";
..//some headers
mail($to,sbj,$body,$headers) 

This would create your list of input each on a separate line in the email.

Comments

0

the easiest, but most ugly way to to this is just using:

mail($to,sbj,print_r($field, true),$headers);

print_r($field, true) will output the value to a variable and not print it out immediately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.