user2233333
user2233333

Reputation: 193

Capture HTML Form Arrays in PHP Email

I have an HTML form with a few arrays as defined by their name tags having [] after them. I am trying to send all of those array values in a single PHP email. I know var_dump will show those array values on screen, but var_dump doesn't seem to work to send the values in the email. Any guidance is greatly appreciated. Thank you, below is what I have.

In the html form the fields name and email are arrays with '[]' after their name tags, up to 15 can be submitted on a single form. Below is the PHP code I have trying to capture the values.

$to = "$Eemail";
$subject = "Credential Request";
$message = var_dump($name, $email);
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

The email is sending fine, but as I've tried many options in the $message, I either get an empty email or a NULL response.

Upvotes: 3

Views: 366

Answers (4)

user2233333
user2233333

Reputation: 193

I found what I was looking for. A foreach loop with multiple arrays.

$to = "$Eemail";
$subject = "Credential Request"; 
$message = '';
$message .= '<html><body>';
        $message .= "<h2>Credential Requests</h2>";
        $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';

foreach($_POST['name'] as $key => $something) { 
$email = $_POST['email'][$key];
$position = $_POST['position'][$key];
        $message .= "<tr><td> $something</td><td> $email </td><td> $position</td></tr>";
}
        $message .= "</table>"; 
$from = "[email protected]";
$headers = "From: " . $from . "\r\n";
        $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);

Upvotes: 0

Fabio
Fabio

Reputation: 23500

var_dump is just a function to print arrays, very similar to print_r in your case you will need to assign a string to your $message variable

I suppose $name and $email are both array with same number of values, you can just use a for loop

for($i=0; $i<count($name); $i++)
{
    $to = "$Eemail";
    $subject = "Credential Request";
    $message = 'name: ' . $name[$i] . ' email: ' . $email[$i];
    $from = "[email protected]";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
}

As pointed out it seems you need to send one email with all your arrays value so simply loop and concatenate all arrays value

$to = "$Eemail";
$subject = "Credential Request";
$message = '';
for($i=0; $i<count($name); $i++)
{
     $message .= 'name: ' . $name[$i] . ' email: ' . $email[$i] . "\n";
}
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

Upvotes: 1

likeitlikeit
likeitlikeit

Reputation: 5638

var_dump only outputs something to the output buffer. You're probably looking for something like this:

$message = implode(" ", $name) . " " . implode(" ", $email);

Upvotes: 0

Patrick Kostjens
Patrick Kostjens

Reputation: 5105

var_dump doesn't return anything. Therefor assigning its result to $message will result in an empty variable, which in turn causes your mail to be empty.

Instead if you want to have the result as a string, you could use the following code:

$message = print_r($name, true) . "\n" . print_r($email, true);

Upvotes: 0

Related Questions