annehornung Posted July 8, 2008 Share Posted July 8, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/ Share on other sites More sharing options...
Wolphie Posted July 8, 2008 Share Posted July 8, 2008 Array keys are defined like so: <?php $my_array = array(); $my_array['name'] = 'Wolphie'; $my_array['age'] = '19'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584658 Share on other sites More sharing options...
DarkWater Posted July 8, 2008 Share Posted July 8, 2008 And you can't assign a variable to a foreach. What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584687 Share on other sites More sharing options...
mbeals Posted July 8, 2008 Share Posted July 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584697 Share on other sites More sharing options...
annehornung Posted July 8, 2008 Author Share Posted July 8, 2008 All I am trying to do is get the subimtted info in my form to my email. When it comes to php though, I have no clue what I am doing. Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584699 Share on other sites More sharing options...
annehornung Posted July 8, 2008 Author Share Posted July 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584706 Share on other sites More sharing options...
mbeals Posted July 8, 2008 Share Posted July 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584726 Share on other sites More sharing options...
annehornung Posted July 8, 2008 Author Share Posted July 8, 2008 Perfect! It works! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/113774-solved-please-help/#findComment-584749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.