Jump to content

[SOLVED] Please Help!


annehornung

Recommended Posts

This is probably a really dumb question, but I do not know much about php.  I am trying to get a form in a website to send to my email.  To view the form, go to www.thedutchtouch.net/contact.html

 

I did some research online, and this is the code I have so far, but it isn't working:

 

 

<?php

$to = "*myemail*";

$subject = "Custom Order Request";

$email = $_REQUEST['email'] ; 

$headers = "From: $email";

 

$fields = array();

$fields{"name"} = "Name";

$fields{"phone"} = "Phone Number";

$fields{"product"} = "Product";

$fields{"wood"} = "Wood Types";

$fields{"date"} = "Date Need By";

 

$body = foreach($fields as $key => $age){echo "$key, $age"

**This is where it says the error is**

 

if($sent)

{print "Your mail was sent successfully"; }

else

{print "We encountered an error sending your mail"; }

?>[/font]

 

 

Can anyone help me!!!???

 

Thanks!

Link to comment
Share on other sites

that is true, but your code isn't sending any email... it's not even trying.

 

I'm assuming you are pulling in the values in your $fields array from the previous form?

 

try something like this:

 


<?php

#### Set up email ##############
$to = "*myemail*";
$subject = "Custom Order Request"; 
$email = $_REQUEST['email'] ; 
$headers = "From: $email"; 



### Get results from form ############
## change these to reflect the names of the entries in
## your form.

$fields['name']  = $_REQUEST['name'];
$fields['phone'] = $_REQUEST['phone'];
$fields['product'] = $_REQUEST['product'];
$fields['wood'] = $_REQUEST['wood'];
$fields['date']  = $_REQUEST['date'];


##### Build the body of the email ####

foreach($fields as $key => $age){

  $body .= "$key : $age\n";
}


$sent = mail($to,$subject,$body,$headers);


if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

 

Before anyone jumps on me....yes there is much that can be optimized, but I didn't for the sake of clarity.

 

 

So lets look at what this is doing....  Your first part is okay and you build the headers fine.  Next you have to get the data from the form.  This is in the form of an array which you are accessing with $_REQUEST.  You could also access this array with $_POST or $_GET depending on what method you set in the form tag....anyway.  The array from the form is an associative array, meaning that the key for an entry is the name of the form object and the value is the value.  IE a text box named 'name' that is submitted with the value "Ted" will give $_REQUEST['name'] = 'Ted'.  Make sense? 

 

So we pull those values out and put them in the new $fields array.  Then to build the body, we loop through the array with foreach and create a string containing each key->value pair.  We need to save the data to a string instead of echoing it.  Echoing it just displays it on the screen.  The .= operator means take the current value and add this new stuff to it.  So every time the loop loops, the new value is just tacked on to the end.  We use the \n so that the new value is started on a new line.

 

The final part is actually sending the mail with the mail() command.

 

 

 

 

Link to comment
Share on other sites

Thanks for your input.

 

I tried what you said, and this is what I got as an error:

 

Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home2/thedutc2/public_html/mail.php on line 20

In dreamweaver, line 20 is:  $fields['date'] = $_REQUEST['date'];

 

Any ideas?

 

This is the part I don't understand:

 

foreach($fields as $key => $age){

 

  $body .= "$key : $age\n";

}

 

what is $key and $age? 

 

Link to comment
Share on other sites

okay, lets just keep it simple for now. 

 

<?php

#### Set up email ##############
$to = "*myemail*";
$subject = "Custom Order Request"; 
$email = $_REQUEST['email'] ; 
$headers = "From: $email"; 



### Get results from form ############
## change these to reflect the names of the entries in
## your form.

$name = $_REQUEST['name'];
$phone= $_REQUEST['phone'];
$product = $_REQUEST['product'];
$wood = $_REQUEST['wood'];
$date  = $_REQUEST['date'];


##### Build the body of the email ####

$body = "Name: $name\n
             Phone: $phone\n
             Product: $product\n
             Wood: $wood\n
             Date:  $date";


$sent = mail($to,$subject,$body,$headers);


if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

 

make sure you change

 

$name = $_REQUEST['name'];
$phone= $_REQUEST['phone'];
$product = $_REQUEST['product'];
$wood = $_REQUEST['wood'];
$date  = $_REQUEST['date'];

 

to match the names of the fields in your form.  For that example, my form would have 5 fields, named name, phone, product, wood and date.  If yours are named differently, then change the name in the $_REQUEST[' '] part.

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.