Hey everyone. First I'd like to say this is a great community and seeing as I've finally started attempting to learn PHP I decided I'd finally sign up for all of the current and future help I will need haha.

Okay well I'm currently setting up a website for my dad and he needs a form that has dynamic fields (user can add more sets of fields). You can look here for a visual...

http://hbndev.com/ipad/index.php?option=com_content&view=article&id=2&Itemid=3

As you can see the user can add more sets of fields to add more info. Now I'm gathering the info just fine to display on the php file it goes to just fine. However I can't get the info to be emailed. I've tried a lot of different ways of setting it up and no matter what the info just won't be sent. The closest I've got is getting the last field set displayed in the email....ie

First Field Set: Job 1 - Option 1 - Material 1
Second Field Set: Job 2 - Option 2 - Material 2

The email will only contain "Job 2, Option 2, Material 2"

I need the email to display all of the info and preferably have it inline...ie

First Field Set: Job 1 - Option 1 - Material 1
Second Field Set: Job 2 - Option 2 - Material 2

Email:

Job 1 - Option 1 - Material 1
Job 2 - Option 2 - Material 2

Here are links to the code I have now...

PHP:
http://paste2.org/p/1243636

Form:
http://paste2.org/p/1243638

Javascript:
http://paste2.org/p/1243640

Any and all help is much appreciated.

If you'd like you can contact me via aim, msn or email...

iregicide, iregicide@aol.com

Recommended Answers

All 13 Replies

You have one form in your HTML and then some input elements outside of the form. They are never sent.
For me, the easiest way to transform POST data in a mail text is a post array walk:

foreach( $_POST as $key => $value ) 
  $message .= "$key: $value\n";
mail($to, $subject, $message );

That worked :)

However I now have 2 questions lol.

1 in the email how can I get the info to display inline?

Right now it comes out as:

Job 1
Job 2

Option 1
Option 2

Quantity 1
Quantity 2

I need it:

Job 1 - Option 1 - Quantity 1
Job 2 - Option 2 - Quantity 2

or:

Job 1
Option 1
Quantity 1

Job 2
Option 2
Quantity 2

Here is the php I have now...

<?php

date_default_timezone_set('PDT');

// Contact subject
$date = date('F, j');

$vendor = $_POST["vendor"];

$subject = 'Purchase Order - ' . $date;

$mail_from= $_POST["vendor"] + " Purchase Order";

$to ='iregicide@aol.com';

foreach( $_POST['job'] as $jobvalue ) 
  $jobs .= "$jobvalue\n";
  
foreach( $_POST['material'] as $materialvalue ) 
  $materials .= "$materialvalue\n";
  
foreach( $_POST['quantity'] as $quantityvalue ) 
  $quantities .= "$quantityvalue\n";
  
$message = "Vendor: $vendor \n $jobs \n $materials \n $quantities";
  
$send_contact=mail($to, $subject, $message );

if($send_contact){
echo "We've received your contact information";
}
else {
echo "There has been an ERROR and the form hasn't been sent. Please try again.";
}

?>

I have the input fields all put into the form now (I moved </form>)

Also in the email there is always an extra "option 1" at the end of where the options are for seemingly no reason lol. Any hlep with that would be appreciated.

Thanks for all of you're help

To output the job as you want, there are a few ways. YOu could remove your three 3 loops and have a single loop:

while(list($key,$value) = each($_POST['job'])) {
				
	$job = $_POST['job'][$key];
	$material = $_POST['material'][$key];
    $quantity = $_POST['material'][$key];
	
    $job_output .= $job . ' - ' . $material . ' - ' . $quantity . "\n";

}

This will output your jobs like:

Job 1 - Option 1 - Quantity 1
Job 2 - Option 2 - Quantity 2

Thank you so much! That worked perfect. Any clue as to why Option 1 keeps showing up though?

It displays perfect now except with...

Job 1 - Option 1 - Quantity 1
Job 2 - Option 2 - Quantity 2
- Option 1 -

Thank you so much! That worked perfect. Any clue as to why Option 1 keeps showing up though?

It displays perfect now except with...

Job 1 - Option 1 - Quantity 1
Job 2 - Option 2 - Quantity 2
- Option 1 -

Can you post your updated code?

Here is the updated code...

<?php

date_default_timezone_set('PDT');

// Contact subject
$date = date('F, j');

$vendor = $_POST["vendor"];

$subject = 'Purchase Order - ' . $date;

$mail_from= $_POST["vendor"] + " Purchase Order";

$to ='iregicide@aol.com';

#	foreach( $_POST['job'] as $jobvalue ) 
#	  $jobs .= "$jobvalue\n";
  
#	foreach( $_POST['material'] as $materialvalue ) 
#	  $materials .= "$materialvalue\n";
  
#	foreach( $_POST['quantity'] as $quantityvalue ) 
#	  $quantities .= "$quantityvalue\n";
  
while(list($key,$value) = each($_POST['job'])) {
				
	$job = $_POST['job'][$key];
	$material = $_POST['material'][$key];
    $quantity = $_POST['quantity'][$key];
	
    $job_output .= 'Job: ' . $job . ' - Material: ' . $material . ' - Quantity: ' . $quantity . "\n\n";

}
  
$message = "Vendor: $vendor \n\n $job_output";
  
$send_contact=mail($to, $subject, $message );

if($send_contact){
echo "We've received your contact information";
}
else {
echo "There has been an ERROR and the form hasn't been sent. Please try again.";
}

?>

Also how would I bold some of the text? I have tried adding <strong></strong> and <b></b> yet both failed haha

To bold the text you'll need to send the email in html format, you'll need to add some extra headers and pass them to the mail function, so you have your code the same but add the following:

$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= "From: noreply@youremail.com" . "\n";

$send_contact = mail($to, $subject, $message, $headers );

That extra option line outpoutting is weird. What is outputted to the screen when you add the following after your loop?

echo $job_output;
die();

This is what I get...

Job: Buck - Material: Option 1 - Quantity: 239 Job: Dad - Material: Option 2 - Quantity: 1 Job: - Material: Option 1 - Quantity:

could we possibly talk on msn or aim?

I figured out why the last option, etc is showing up....it's because of the javascript. I have hidden input fields that get brought up with the add button. So the php is seeing those hidden fields and inputting them into the email.

How can I make the email remove the last loop?

Can you post the html for your form and say what fields you want excluding. I'll post up some updated code then :)

Here is all of the code I have...

PHP:

<?php

date_default_timezone_set('PDT');

// Contact subject
$date = date('F, j');

$vendor = $_POST["vendor"];

$subject = 'Purchase Order - ' . $date;

$mail_from= $_POST["vendor"] + " Purchase Order";

$to ='iregicide@aol.com';

$headers = 'MIME-Version: 1.0' . "\n";
$headers .= "From: Online Purchase Order" . "\n";
  
while(list($key,$value) = each($_POST['job'])) {
				
	$job = $_POST['job'][$key];
	$lathmaterial = $_POST['lathmaterial'][$key];
	$plasteringmaterial = $_POST['plasteringmaterial'][$key];
    $quantity = $_POST['quantity'][$key];
	
    $job_output .= 'Job: ' . $job . '   -   Lath Material: ' . $lathmaterial . '   -   Plastering Material: ' . $plasteringmaterial . 
    '   -   Quantity: ' . $quantity .
    "\n\n"
    . '---------------------------------------------------------------------------------------------' . "\n\n";

}
  
$message = "Vendor: $vendor \n\n\n $job_output \n\n";
  
$send_contact=mail($to, $subject, $message, $headers );

if($send_contact){
header("location:http://hbndev.com/ipad/index.php?option=com_content&view=article&id=6");
exit;
} else {
header("location:http://hbndev.com/ipad/index.php?option=com_content&view=article&id=7");
exit;}

?>

HTML Form:

<form method="post" action="http://www.hbndev.com/ipad/purchaseorder.php">

<div id="newlink">

Vendor:
<select name="vendor">
	<option>Option 1</option>
	<option>Option 2</option>
	<option>Option 3</option>
</select> 

<br><br>

<div class="form">
Job:
<input name="job[]" value="" class="job" type="text">
		
Lath Material:
<select name="lathmaterial[]">
	<option>Furring Nails 1 3/4" - #7</option>
        <option>Roofing Nails 1 1/4" GA. - #12</option>
        <option>Paslode / Staples 1 1/2" Box - #19</option>
        <option>Tacker Staples (HAM) - 1/4 RPA 19 - #21</option>
        <option>J-8 3/4" with Holes - #23</option>
        <option>Metal Lath 3.4 3/8 Spray RIB - #43</option>
        <option>Metal Lath 3.4 GA CONT. FURR - #44</option>
        <option>#36 Weep Screed 10' - #62</option>
        <option>#5 Drip No Holes - #65</option>
        <option>Arch Aid - #65</option>
        <option>#66 3/4" S.FLNG - #82</option>
        <option>CORNERAID - #86</option>
</select> 

Plastering Material:
<select name="plasteringmaterial[]">
	<option>Option 1</option>
	<option>Option 2</option>
	<option>Option 3</option>
</select> 

Quantity:
<input name="quantity[]" value="" type="text">
</div>
</div>

<p id="addnew">
	<a href="javascript:new_link()" mce_href="javascript:new_link()">Add New Material</a>
</p>

<input name="submit1" class="button" value="Email" type="submit">
<input name="reset1" class="button" value="Reset" type="reset">


<!-- Template -->
<div id="newlinktpl" style="display: none;" mce_style="display:none">
<div class="form">
Job:
<input name="job[]" value="" class="job" type="text">
		
Material:
<select name="material[]">
	<option>Option 1</option>
	<option>Option 2</option>
	<option>Option 3</option>
</select> 

Quantity:
<input name="quantity[]" value="" type="text">
</div>
</div>

</form>

Javascript:

<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

<script>
/*
This script is identical to the above JavaScript function.
*/
var ct = 1;
function new_link()
{
	ct++;
	var div1 = document.createElement('div');
	div1.id = ct;
	// link to delete extended form elements
	var delLink = '';
	div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
	document.getElementById('newlink').appendChild(div1);
}
// function to delete the newly added set of elements
function delIt(eleId)
{
	d = document;
	var ele = d.getElementById(eleId);
	var parentEle = d.getElementById('newlink');
	parentEle.removeChild(ele);
}
</script>

Don't worry about the first html form being different than the template one lower in the code...I'm just working on the options so yea....thanks for all of your help :)

Anything? Bump...

I have the follow problem/issue. Would like to have it a bit nicer:

The vars $gender, $shoe_size, $clothing_size and $growth are an array(?).

And I like to have, instead each line with $gender[0] something dynamic...

My problem I have, how I do it exactly, that I get each line as I have at the moment.

Here is my code:

<?php
    error_reporting(E_ALL);

    require('../includes/class.phpmailer.php');
    require('../includes/class.smtp.php');
    require('../includes/class.pop3.php');
    require('../includes/configdb.php');

    //var_dump($_REQUEST);
    $lang               = $_REQUEST["lang"]; //]=> string(2) "en" 
    $type               = $_REQUEST["type"]; //=> string(7) "courses" 
    $firstname          = $_REQUEST["firstname"]; //=> string(5) "Simon" 
    $secondname         = $_REQUEST["secondname"]; //=> string(8) "Vetterli" 
    $hotel              = $_REQUEST["hotel"]; //=> string(14) "Hotel on Samui" 
    $email              = $_REQUEST["email"]; //=> string(21) "svetterli68@gmail.com" 
    $facebook           = $_REQUEST["facebook"]; //=> string(16) "Facebook account" 
    $gender             = $_REQUEST["gender"]; //=> array(2) { [0]=> string(4) "male" [1]=> string(4) "male" } 
    $shoe_size          = $_REQUEST["shoe_size"]; //=> array(2) { [0]=> string(5) "34-35" [1]=> string(5) "40-41" } 
    $clothing_size      = $_REQUEST["clothing_size"]; //=> array(2) { [0]=> string(2) "XS" [1]=> string(0) "" } 
    $growth             = $_REQUEST["growth"]; //=> array(2) { [0]=> string(6) "Height" [1]=> string(0) "" } 
    $course             = $_REQUEST["course"]; //=> string(1) "5" 
    $learning_discount  = $_REQUEST["learning_discount"]; //=> string(3) "yes" 
    $course_option      = $_REQUEST["course_option"]; //=> string(8) "11:16000" 
    $dates              = $_REQUEST["dates"]; //=> string(10) "2014-12-20" 
    $room_type          = $_REQUEST["room_type"]; //=> string(0) "" 
    $room_count         = $_REQUEST["room_count"]; //=> string(1) "1" } 
    $room_nights        = $_REQUEST["room_nights"]; //=> string(1) "1" 
    $comment            = $_REQUEST["comment"]; //=> string(0) ""
    $summary            = $_REQUEST["summary"]; //=> string(605) "

    $from_name  = $firstname.' '.$secondname;
    $femail     = $email;   
    $to = "info@easydivers-thailand.com";
    $date = date('Y-m-d');
    $subject = 'Booking '.$type.'-'.$date.' from '.$from_name;
    // $toCC        = "svetterli68@gmail.com";
    // Mail verschicken

    $header  = "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html; charset=utf-8\r\n";
    $header .= "From: $from_name <$femail>\r\n";
    $header .= "Reply-To: $femail\r\n";
    $header .= "X-Mailer: PHP ". phpversion();

    $pinformation = "
    You got the follow enquiry:<br />
    <br />
    Booking Date: $date<br />
    Name: $from_name<br />
    Hotel/Resort: $hotel<br />
    Email: $email<br />
    FB: $facebook<br />
    Language: $lang<br />
    <br />";
    $students = "
    Student #1: Gender: $gender[0]    / Shoe-Size: $shoe_size[0] / Clothing Size: $clothing_size[0] / Heigth: $growth[0]<br />
    Student #2: Gender: $gender[1]    / Shoe-Size: $shoe_size[1] / Clothing Size: $clothing_size[1] / Heigth: $growth[1]<br />
    Student #3: Gender: $gender[2]    / Shoe-Size: $shoe_size[2] / Clothing Size: $clothing_size[2] / Heigth: $growth[2]<br />
    Student #4: Gender: $gender[3]    / Shoe-Size: $shoe_size[3] / Clothing Size: $clothing_size[3] / Heigth: $growth[3]<br />
    Student #5: Gender: $gender[4]    / Shoe-Size: $shoe_size[4] / Clothing Size: $clothing_size[4] / Heigth: $growth[4]<br />
    Student #6: Gender: $gender[5]    / Shoe-Size: $shoe_size[5] / Clothing Size: $clothing_size[5] / Heigth: $growth[5]<br />
    Student #7: Gender: $gender[6]    / Shoe-Size: $shoe_size[6] / Clothing Size: $clothing_size[6] / Heigth: $growth[6]<br />
    Student #8: Gender: $gender[7]    / Shoe-Size: $shoe_size[7] / Clothing Size: $clothing_size[7] / Heigth: $growth[7]<br />
    Student #9: Gender: $gender[8]    / Shoe-Size: $shoe_size[8] / Clothing Size: $clothing_size[8] / Heigth: $growth[8]<br />
    Student #10: Gender: $gender[9]   / Shoe-Size: $shoe_size[9] / Clothing Size: $clothing_size[9] / Heigth: $growth[9]<br />
    <br />";
    $ainformation = "
    Dates to start the course: $dates<br />
    Learning Discount: $learning_discount<br />
    Comment: $comment<br />
    <br />";
    $acomondation = "
    Room Type: $room_type<br />
    Nr Rooms: $room_count<br />
    Nr Nights: $room_nights<br />
    <br />";

    $message = $pinformation.$students.$ainformation.$acomondation.$summary;

    $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
    // Run the preg_match() function on regex against the email address
    if (preg_match($regex, $femail))

        {
          //Instanz von PHPMailer bilden
          $mail = new PHPMailer();

          //$mail->IsSMTP(); //Versand über SMTP festlegen
          //$mail->Host = "mail.turtlesdive.com"; //SMTP-Server setzen

                          //$mail->SMTPAuth = true;     //Authentifizierung aktivieren
                          //$mail->Username = "web202p16";  // SMTP Benutzername
                          //$mail->Password = "simon99"; // SMTP Passwort 

                          //Absenderadresse der Email setzen
                          $mail->From = $femail;

                          //Name des Abenders setzen
                          $mail->FromName = $from_name;

                          //Empfängeradresse setzen
                          $mail->AddAddress($to);
                          //$mail->AddCC($toCC);
                          $mail->AddBCC("svetterli68@gmail.com");

                          //Betreff der Email setzen
                          $mail->Subject = $subject;

                          $mail->IsHTML(true); //Versand im HTML-Format festlegen

                          //Text der EMail setzen
                          $mail->Body = $message;

                          //EMail senden und überprüfen ob sie versandt wurde

                          if(!$mail->Send()){
                            echo "There was an error sending your reservation." . $mail->ErrorInfo;
                            exit;
                        }

                        header('Location:/'.$lang.'/thankyou');
                        exit;

        }
    ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.