Home > other >  Adding line break if $_Post is populated
Adding line break if $_Post is populated

Time:02-22

Trying to build a Supply Request form that will send me an HTML formatted email upon submission. It will contain 100 supplies, but I need to include Text before some of the fields (e.g. Qty:) that are posting and then a <br> after to have the next field start on a new line.

The issue I'm having is that regardless of whatever I do, the un-selected supplies cause gaps in the email where that product should have been.

The email should display something like this:

Hi Name, we received your request! Below is a list of everything you requested.

Vendor: Product 1

Qty: 5

Notes:


Vendor: Product 3

Qty: 5

Notes:

Code is below.

<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Company <[email protected]>' . "\r\n";
$headers .= 'To: [email protected]' . "\r\n";
$headers .= "Cc: " . $_POST["EmailWork"] . "\r\n";
$subject = "Supply Request";
$Name = $_POST["Name"];
$EmailWork = $_POST["EmailWork"];
$Product1 = $_POST["Product1"];
$Product2 = $_POST["Product2"];
$Product3 = $_POST["Product3"];
$Notes1 = $_POST["Notes1"];
$Notes2 = $_POST["Notes2"];
$Notes3 = $_POST["Notes3"];
$Qty1 = $_POST["Qty1"];
$Qty2 = $_POST["Qty2"];
$Qty3 = $_POST["Qty3"];
$to = '[email protected]';
$message = '
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Staff Portal Submission: Add a Bookmark</title>
</head>
<body bgcolor="#F7F7F7" leftmargin="0" marginheight="0" marginwidth="0" offset="0" topmargin="0">
    <table bgcolor="#F7F7F7" border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td align="center" valign="top">
                <table border="0" cellpadding="0" cellspacing="0" width="660">
                    <tr>
                        <td align="center" height="50" valign="top">&nbsp;</td>
                    </tr>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF" valign="top"><img height="190" src="#" width="660"></td>
                    </tr>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF" valign="top">
                            <table border="0" cellpadding="0" cellspacing="20" width="660">
                                <tr>
                                    <td><span style="color: rgba(44, 68, 85, 0.8); font-family: Arial, Helvetica, sans-serif; font-size: 18px;">'. "Hi" . $_POST['Name'] . ", we received your request! Below is a list of everything you requested." . '</span></td>
                                </tr>
                                <tr>
                                  <td>'. $_POST['Product1'] . $_POST['Qty1'] . $_POST['Notes1'] . $_POST['Product2'] . $_POST['Qty2'] . $_POST['Notes2'] . $_POST['Product3'] . $_POST['Qty3'] . $_POST['Notes3'] .'</td>
                              </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>
</html>
';

if ($Name && $EmailWork)
{
    if (mail($to, $subject, $message, $headers))
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=/assets/ssi/received.asp\">";
    }
    else
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=/assets/ssi/error.asp\">";
    }
}
else
{
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=/assets/ssi/incomplete.asp\">";
}

?>

Form Example

<form action="/assets/ssi/hub/add/submit7.inc.php" method="post">
<input  id="Product1" name="Product" type="checkbox" value="<br>Exam Glove McKesson 3.5C Small Gloves <br>Qty: "></div>
<input  id="Qty[]" name="Qty[]" type="text">
<input  id="Notes[1]" name="Notes[1]" type="text">
<button  type="submit">Submit Order</button>
</form>

This is a screenshot of what the page would somewhat look like... Obviously it would have A LOT more on it:

enter image description here

CodePudding user response:

You haven's shown your form.

Remember that within form you can use a value as an array:

<input type="text" name="products[]" /> 

if you have multiple inputs with the name ending with [], all the inputs are read and posted as an array $_POST["products"]. You can then loop through the post to see all the values from input called "products[]".

CodePudding user response:

This is slightly trickier to get right than it looks, partly due to the way checkboxes are treated when submitting a HTML form - if the checkbox isn't checked, it simply isn't submitted. This means if you're submitting lists of repeating fields which are meant to be matched with each checkbox, they'll get out of sync because - for example - if you have 4 rows of fields, and check 2 boxes, you'll get 4 lots of the other fields, but only 2 checkboxes, making it hard/impossible to work out which checkbox goes with which fields in the POST data.

This can be resolved by tweaking your form a bit. It needed a few fixes anyway from your version above.

Changes as follows:

  • most importantly, using array syntax for the Products field, and numbering the indices - this means that the numbering will be preserved when submitting, even if not all the boxes are ticked. This means you can match the index of the checkbox with the indices of the accompanying fields in the various arrays in $_POST.

  • don't use IDs on repeating fields. This is invalid in HTML, because IDs are, by definition, meant to be unique.

  • don't number the Notes fields.

  • I strongly recommend not including formatting information or the text of your emails in the value of the products field...keep the formatting aspect all in one place, when you're constructing your message in the PHP code.

Example of new form structure:

<form action="/assets/ssi/hub/add/submit7.inc.php" method="post">
  Name: <input type="text" name="Name" />
  Email: <input type="email" name="EmailWork" /><br/>
  ...
   <input  name="Product[0]" type="checkbox" value="Exam Glove McKesson 3.5C Small Gloves"></div>
  <input  name="Qty[]" type="text">
  <input  name="Notes[]" type="text">
  <br/>
  <input  name="Product[1]" type="checkbox" value="Product no. 1"></div>
  <input  name="Qty[]" type="text">
  <input  name="Notes[]" type="text">
  <br/>
  <input  name="Product[2]" type="checkbox" value="Product no. 2"></div>
  <input  name="Qty[]" type="text">
  <input  name="Notes[]" type="text">
  <br/>
  <input  name="Product[3]" type="checkbox" value="Product no. 3"></div>
  <input  name="Qty[]" type="text">
  <input  name="Notes[]" type="text">
  <br/>
  ...
  <button  type="submit" name="submit">Submit Order</button>
</form>

And then the PHP code can use a simple foreach loop to go through all the selected Product checkboxes (since those will be the only checkbox values sent in the POST data), get the index (key) and value of each, and output that value plus the entries from the other POST arrays which match the key. Each iteration of the loop will create a new row in the table.

PHP code:

$Name = $_POST["Name"];
$EmailWork = $_POST["EmailWork"];

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Company <[email protected]>' . "\r\n";
$headers .= 'To: [email protected]' . "\r\n";
$headers .= "Cc: " . $EmailWork . "\r\n";
$subject = "Supply Request";
$to = '[email protected]';
$message = '
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Staff Portal Submission: Add a Bookmark</title>
</head>
<body bgcolor="#F7F7F7" leftmargin="0" marginheight="0" marginwidth="0" offset="0" topmargin="0">
    <table bgcolor="#F7F7F7" border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td align="center" valign="top">
                <table border="0" cellpadding="0" cellspacing="0" width="660">
                    <tr>
                        <td align="center" height="50" valign="top">&nbsp;</td>
                    </tr>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF" valign="top"><img height="190" src="#" width="660"></td>
                    </tr>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF" valign="top">
                            <table border="0" cellpadding="0" cellspacing="20" width="660">
                                <tr>
                                    <td><span style="color: rgba(44, 68, 85, 0.8); font-family: Arial, Helvetica, sans-serif; font-size: 18px;">'. "Hi " . $Name . ", we received your request! Below is a list of everything you requested." . '</span></td>
                                </tr>';
                                
foreach ($_POST["Product"] as $key => $val)
{
    $message .= "<tr><td>Vendor: ".$val."<br/>Qty: ".$_POST['Qty'][$key]."<br/>Notes: ".$_POST['Notes'][$key]."</td></tr>";
}
                                
                                
$message .=                            '</table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>
</html>
';

if ($Name && $EmailWork)
{
    if (mail($to, $subject, $message, $headers))
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=/assets/ssi/received.asp\">";
    }
    else
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=/assets/ssi/error.asp\">";
    }
}
else
{
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=/assets/ssi/incomplete.asp\">";
}
  • Related