Sending data of an associative array to an email address. Please help me out

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Sending data of an associative array to an email address. Please help me out

    I am trying to create a php file that sends some information to the recipient's email address . My code works but , it only sends the $data and not the $key as well .

    I want this php script to make the following output :

    Firstname : Theodore (for example)
    Lastname : Zerbas (for examle)

    Is there somebody to help me out ? thank you for your time.

    Code:
     
     $firstname = $_POST['fname']; $lastname  = $_POST['lname'];  $to = "example@example.com";  $message = array("name"=>$firstname,"lastname:"=>$lastname);   foreach($message as $key=>$data) {            $message[]=implode("<br/>",$key.':'.$data);        }  if(mail($to,$subject,implode("<br/>",$message),implode("\r\n",$headers))) { 	echo "Your message has been sent successfully"; 	} else  {echo " Something went wrong";}

    #2
    I'm a little confused on why all the arrays and imploding etc when you are hard writing the result into the array.
    Would it not be easier not to use an array and just hard write the result into the email?
    PHP Code:
    $firstname $_POST['fname']; 
    $lastname  $_POST['lname'];  
    $to "example@example.com";   
    $message 'name: '.$firstname.'<br/>lastname: '.$lastname.'<br/>';
    if(
    mail($to$subject$messageimplode("\r\n",$headers))) 
    {     
        echo 
    "Your message has been sent successfully";     

    else  
    {
        echo 
    " Something went wrong";


    Or if for some reason an array is required due to checkboxes or something similar
    then:
    PHP Code:
    $firstname $_POST['fname']; 
    $lastname  $_POST['lname'];   
    $to "example@example.com";  
    $message = array("name"=>$firstname,"lastname:"=>$lastname);  
    $newMessage '';
    foreach(
    $message as $key=>$data
    {            
        
    $newMessage .= $key.':'.$data.'<br/>';
    }  

    if(
    mail($to$subject$newMessageimplode("\r\n",$headers))) 
    {     
        echo 
    "Your message has been sent successfully";     

    else  
    {
        echo 
    " Something went wrong";

    Comment


      #3
      Thank you for your reply . I've tried the following code and it worked as well .
      Code:
      $message = array("firstname" => "name: ".$firstname,"lastname" => "lastname: ".$lastname);
      Last edited by zervas; 12.05.17, 08:24.

      Comment

      Working...
      X