Rajasekaran Raji
Rajasekaran Raji

Reputation: 74

Form values get in arrays and send it to email not working in PHP

I am try to send form values using foreach to send email but not works.

My Error is: I have Receive email. But last value Only.

My Form Code

<form action="index.php" method="POST"> 
Name : <input type="text" name="name">
Email : <input type="text" name="email">
Message : <textarea name="message"></textarea>
<input type="submit">
</form>

My PHP Code

if(isset($_POST['name'])){

$data = $_POST;

$arrays = serialize($data);

$decod = unserialize($arrays);

foreach($decode as $key => $value) {
   $message = '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
}

    $headers = "From: [email protected]" . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
    $emailbody = $message; 

    mail('[email protected]','Contacts From Website',$emailbody,$headers);

}
?>

Upvotes: 3

Views: 67

Answers (1)

Ram Sharma
Ram Sharma

Reputation: 8819

change

foreach($decode as $key => $value) {
   $message = '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
}

to

foreach($decode as $key => $value) {
   $message.= '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
}

Your values are overwritten by the next value as per loop you may need to append data in $message instead of add new value every time.

Upvotes: 3

Related Questions