0

You guys are awesome! Thank you so much for answering this so quickly. I was missing a couple of .'s and ;'s but this is even more detailed than I expected!! The column trick and border was an excellent touch! Thank you all!

I have a php form that I want to send to an email, but it looks terrible with just

Name: Entered Name
Email: Entered Email
etc

Can anyone help me style this into a table, I keep getting errors trying to figure it out myself, and I've searched for examples but none use the type of code my form is using and i'm probably missing something. I believe this is the relevant section I need to do:

    // If the e-mail is not working, change the debug option to 2 | $debug = 2;
    $debug = 0;

    $subject = $_POST['subject'];

    $fields = array(
        0 => array(
            'text' => 'First Name',
            'val' => $_POST['fname']
        ),
        1 => array(
            'text' => 'Last Name',
            'val' => $_POST['lname']
        ),
        2 => array(
            'text' => 'Address',
            'val' => $_POST['address']
        ),
        3 => array(
            'text' => 'City',
            'val' => $_POST['city']
        ),
        4 => array(
            'text' => 'State',
            'val' => $_POST['state']
        ),
        5 => array(
            'text' => 'Zip Code',
            'val' => $_POST['zip']
        ),
        6 => array(
            'text' => 'Date of Birth',
            'val' => $_POST['dob']
        ),
        7 => array(
            'text' => 'Phone Number',
            'val' => $_POST['phone']
        ),
        8 => array(
            'text' => 'Email Address',
            'val' => $_POST['email']
        ),
        9 => array(
            'text' => 'Position Desired',
            'val' => $_POST['position']
        ),
        10 => array(
            'text' => 'Available Start Date',
            'val' => $_POST['start']
        ),
        11 => array(
            'text' => 'Referred By',
            'val' => $_POST['referred']
        ),
        12 => array(
            'text' => 'Additional Info',
            'val' => $_POST['message']
        )
    );

    $message = '';
    $message .= '<h1>We Have a New Jobs Application!</h1>';

    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }

    $mail = new PHPMailer(true);
3
  • 1
    "I keep getting errors" - What errors are you getting? And show us your attempt. Commented Aug 4, 2017 at 21:19
  • Can you add the rest of the code, where you are defining and sending the email? Commented Aug 4, 2017 at 21:19
  • In your case, you don't need to add the keys when adding the nested arrays. You can do just: $fields = [ ['text' => '...', 'val' => '...'], ['text' => '...', 'val' => '...'], and so on... ] Commented Aug 4, 2017 at 21:21

4 Answers 4

1

You can add any style in foreach loop easily !

foreach($fields as $field) {
    $message .= "<span style='font-weight:bold;color:#000'>" . $field['text'].":</span><span style='color:#ccc;'>" . htmlspecialchars($field['val'], ENT_QUOTES) . "</span><br>\n";
}

to make table:

$message .= "<table>";
$message .= "<thead>";
$message .= "<tr><th>column1</><th>column2</th></tr>";
$message .="</thead>";
$message .= "<tbody>";
foreach($fields as $field) {
    $message .= "<tr><td>". $field['text']. "</td><td>" . htmlspecialchars($field['val'], ENT_QUOTES) . "<td></tr>";
}
$message .= "</tbody>";
$message .= "</table";
Sign up to request clarification or add additional context in comments.

Comments

0
$message.="<table>";
foreach($fields as $field) {
    $message.="<tr>";
    $message .= "<td>".$field['text']."</td><td>" . htmlspecialchars($field['val'], ENT_QUOTES) . "</td>";
    $message.="</tr>";
}
$message.="</table>";

But maybe checkout some template libraries like smarty to encapsulate the view from the data.

Comments

0

You need to tell PHPMailer that your content is HTML after setting the body of the email, you can do this by calling the isHTML() method

$mail->Subject = $Subject;
$mail->Body    = $Body;
$mail->IsHTML(true);

In the body you can just generate a table from your foreach

$message = "<table border='1'>";
foreach($fields as $field) {
    $message .= "<tr><td>".$field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "</td></tr>";
}
$message .= "</table>";

Note This is an example, your table may have to look different, this is only a pointer in the right direction

Comments

0

It should be somethink like that:

 $message .= '<table>';

$message .= '<tr> <th>text</th>  <th>value</th> </tr>';

 foreach($fields as $field) {
 $message .= '<tr>';
 $message .= '<td>'.$field['text'].'</td><td>'.htmlspecialchars($field['val'], ENT_QUOTES) . "</td>";
$message .= '</tr>';
    }
$message .= '</table>';

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.