Jump to content

Form mail help to a beginner


FRASA
Go to solution Solved by requinix,

Recommended Posts

Hi

I'm a beginner in php and are facing a problem i cant seem to find the correct solution on.
I have crated a large html form where i't sends mail with vaules from the inputs in an HTML mail.
I would like to check if there is a value from the form, and if there is i need to put the value in the mail. If not, then nothing from that input will be written.
So far my thoughts have been like this:(just a sniplet of my actionpage.php
 

$item5 = $_POST['PIGM1'];
if (empty($item5)) {
   $email_body .= '';
}
if (isset($item5))  
    { 
$email_body = '<html><head><style type="text/css">td { width: 450; }</style></head><body>';
$email_body .= '<table border="0" width="900">';
$email_body .= '<tr><td><strong>Header in table</strong></td><td></td></tr>';
$email_body .= '<tr><td><strong>Ordered</strong></td><td>' . $item5 . ' pcs. of some item</td></tr>';
  $email_body .= '</table><br />';
    }

Can anyone help me in the right direction?

Link to comment
Share on other sites

It would be easier to know what the right direction is if we had a more detailed description, or perhaps even examples, of what you wanted to do.

If you want to send the email always and only include in it particular values then your code there isn't right because it constructs the entire email based on that field. You would need the contents of the email and then to insert some piece(s) depending on $item5.

Yeah, I think some examples would be nice.

Link to comment
Share on other sites

 

Hi Requinix

Thanks for answering. 
I will try to explain it more clear and descripe better.
(I am aware that mail () is not optimal and not at all with html. Is of course in the process of rewriting so it works with phpmailer.)

I have an html form where I want to send data from inputs via mail.
In order for the email not to be filled with lines stating that "0" items are being ordered, my thought is that I will test whether $item5 is empty or 0.
If it's empty or "0" the line should not be sent in mail. If, on the other hand, there is "1" or more, the line must appear in the email.
In the code below, it is only $item5 I have tested on, but the intention is that it is all lines of items to be tested on.

I have many lines. More than in the example below

 

<?php
if(!isset($_POST['submit']))
{
	echo "Error; you need to submit the form!";
}
$address =$_POST['address'];
$tlf =$_POST['tlf'];
$date =$_POST['date'];
$ordernote =$_POST['ordernote'];
$project =$_POST['project'];
$item1 = $_POST['DW'];
$item2 = $_POST['DW1'];
$item3 = $_POST['AF1'];
$item4 = $_POST['AF2'];
$item5 = $_POST['PIGM1'];
$visitor_mail = $_POST['visitormail'];
$visitor_name = $_POST['visitorname'];

//Validate
if(empty($address)||empty($tlf)) 
{
    echo "Address and tlf need to be filled";
    exit;
}


$email_from = 'some@mail.com';
$email_subject = 'My subject';
$email_body = '<html><head><style type="text/css">td { width: 450; }</style></head><body>';
$email_body .= '<h2>New form submitted</h2>';
$email_body .= '<table border="0" width="900">';
$email_body .= '<tr><td><strong>Info:</strong></td><td></td></tr>';
$email_body .= '<tr><td><strong>Address:</strong></td><td>' . $adresse . '</td></tr>';
$email_body .= '<tr><td><strong>Phone:</strong></td><td>' . $tlf . '</td></tr>';
$email_body .= '<tr><td><strong>Date:</strong></td><td>' . $date . '</td></tr>';
$email_body .= '<tr><td><strong>Project number:</strong></td><td>' . $project . '</td></tr>';
$email_body .= '<tr><td><strong>Notes:</strong></td><td>' . $ordernote . '</td></tr>';
$email_body .= '</table><br />';
$email_body .= '<table border="0" width="900">';
$email_body .= '<tr><td><strong>Items info:</strong></td><td></td></tr>';
$email_body .= '<tr><td></td><td>' . $item1 . '&nbsp; pcs. of item 1 is ordered</td></tr>';
$email_body .= '<tr><td></td><td>' . $item2 . '&nbsp; pcs. of item 2 is ordered</td></tr>';
$email_body .= '<tr><td></td><td>' . $item3 . '&nbsp; pcs. of item 3 is ordered</td></tr>';
$email_body .= '<tr><td></td><td>' . $item4 . '&nbsp; pcs. of item 4 is ordered</td></tr>';
//if (empty($vare5)) {
//   echo '$email_body .= "";';
//}
if (isset($item5))  
    { 
        $email_body .= '<tr><td></td><td>' . $item5 . '&nbsp; pcs. of item 5 is ordered</td></tr>';
    }
$email_body .= '</table><br />';
$email_body .= '</body></html>';

$to = "destination@mail.com";
$headers = "From: Project form <". $email_from .">\r\n";
//$headers .= "Cc: <". $visitor_mail .">"\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');
?> 

 

Link to comment
Share on other sites

for the set of values that you want to optionally include, you should use a html array for the form field name, with the item id as the array index, then you can test and loop over that array of values to dynamically add them to the email body. you would use the item id values to query for the information about the items to include them in the email so that it will have helpful context for whoever must deal with the order information.

On 6/11/2021 at 3:21 AM, FRASA said:

a large html form

if you have more than 2-3 form fields, you should use a data-driven design, where you have a data structure (array or database table) that defines the expected fields, which fields are required, and which fields have specific formats that must be met, e.g. an email...

you would then loop over this defining structure and use general-purpose code to validate and process the data.

your form processing code should -

  1. detect if a post method form has been submitted before using any of the form data. your current logic, because it doesn't stop code execution, actually runs everything even if a form has not been submitted.
  2. don't copy variables to other variables for nothing. this is just a waste of typing time. instead, keep the set of form data as an array and operate on elements in this array throughout the rest of the code.
  3. trim all input data before using it so that you can detect if all white-space characters were entered. this can be done using one singe statement, provided you have done item #2 in this list.
  4. validate all the trimmed data before using it, storing validation error messages in an array using the field name as the array index.
  5. if there are no validation errors, the array holding the error messages will be empty, use the summitted form data.
  6. any external, unknown, dynamic value you put into the mail body need to have htmlentities() applied to it in order to prevent any html, css, or javascript in it from being rendered when you read the email in a browser.
  7. any value you put into a mail header, such as the visitor's email address as a Reply-to: field, must be validated that it is exactly and only one properly formatted email address in order to prevent mail header injection.

 

Link to comment
Share on other sites

Sorry mac_gyver. I'm only a beginner in php. 
I do understand that my code contains a lot of "not the correct way to do it".
My only question was if it is possible to do some kind of if/else on the serverside code.
From all posts I may conclude that it's not possible.

Link to comment
Share on other sites

As gw1500se suggested, a loop may be helpful here, but if you only have the 5 fields then it's not a crime to use an if for each one.

19 minutes ago, FRASA said:

From all posts I may conclude that it's not possible.

You have an if statement for item 5. Why not use a similar if statement for items 1-4?

Link to comment
Share on other sites

The if statement on item5 is not giving me correct output.
The line occurs in the mail even if empty or "0". Therefore i have not made it yet for the other items.

As a beginner i'm a bit confused. My plan was to use the if/else on the serverside. But as I understand it's not possible.
It may only be possible by making a loop to check in the html form.
Or?

Link to comment
Share on other sites

  • Solution
15 minutes ago, FRASA said:

The line occurs in the mail even if empty or "0". Therefore i have not made it yet for the other items.

You check if the value "is set". Being empty or being zero counts as "set".

If you want to check that the value is >0 then try doing exactly that.

 

15 minutes ago, FRASA said:

But as I understand it's not possible.

It is very much possible. You're misunderstanding the recommendations to use loops and otherwise change how your form processing code works.

Link to comment
Share on other sites

I tried this weekend to use <0 in the if/else but the outcome was not as i expected.
Now I can see that my problem realy is that I did not use the correct symbol. 
I hope that my teachers in Math are not reading in this forum.
I did not get "less than" and "more than" symbols right. Therefore thoe output did not come out correct.
Less than = >
More than = <

My bad. Thanks for all the help. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.