wednesdayaddams Posted June 15, 2009 Share Posted June 15, 2009 Ok so I have created a form in DW. The action is targeted to this PHP script (see below) However, it doesn't display the message or name?! Just sends a blank email... Any help really appreciated for a frustrated newbie, Thankyou <?php $HTTP_POST_VARS. */ $subject = $HTTP_POST_VARS['subject']; $message = $HTTP_POST_VARS['message']; $email = 'myemail@thatwebsite.com'; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/ Share on other sites More sharing options...
trq Posted June 15, 2009 Share Posted June 15, 2009 Besides the fact that $HTTP_POST_VARS has long been deprticated in favour of a simple $_POST array, the very first line.... $HTTP_POST_VARS. */ will cause a parse error in your code. Why is it there? Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856086 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 Its amended code from a tutorial website. That was already there. Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856089 Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 you are missing a /* which would comment out the line. i.e. /* $HTTP_POST_VARS. */ and like thorpe said, change $HTTP_POST_VARS to $_POST is the code working now? Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856091 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 So I changed the code to below... But still recieving blanks messages (with no content from name or subject input boxes) I really do appreciate your help and hasty responses guys <?php /*$_POST */ $subject = $_POST['subject']; $message = $_POST['message']; $email = 'myemail@mywebsite.com'; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> ps- Please excuse my stupidity!!! Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856097 Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 try put in these 2 lines after /* $_POST */ print_r($_POST); exit(); this will let you see what is being posted and see if your message/subject is being posted.... can you post the response you get from this? and your FORM HTML. Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856105 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 Ok, now when I click submit I get this... Array ( [name] => myname [comment] => comment => venus200013@yahoo.com [submit] => Submit ) The code for the table is as follows (its probably wrong!)- I made it inserting fields in DW (which was the easy bit!)... <form action="send.php" method="post" enctype="application/x-www-form-urlencoded" name="contact form" id="contact form"> <p align="left" class="form">Your name </p> <p align="left"> <input name="subject" type="text" class="formtext" id="subject" size="40" maxlength="50" /> </p> <p align="left" class="form">Your comments or query</p> <p align="left"> <textarea name="message" cols="40" rows="10" class="formtext" id="message"></textarea> </p> <p align="left" class="form">Your email address </p> <p align="left" class="form"> <input name="email" type="text" class="formtext" id="email" size="40" maxlength="50" /> </p> <p align="left" class="form"> <input name="Submit" type="submit" class="form" id="Submit" value="Submit" /> </p> <p class="form"> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856116 Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 Array ( [name] => myname [comment] => comment => venus200013@yahoo.com [submit] => Submit ) you wrote myname in the name field nad comment in the comment field, yes? did you type anything in the subject field? *** just noticed after i typed all the stuff below this... *** an input with the name "comment" is being posted... and the comment field in your form is named "message".... is the HTML FORM you wrote the right one? is send.php in the same directory as the form HTML file? and the contents of send.php is that of the PHP code you've been posting, yeah? also, you have no "name" field in the html form... it says name but its being sent as "subject"... i fixed up the php/html for you... <form action="send.php" method="post" enctype="application/x-www-form-urlencoded" name="contactForm" id="contactForm"> <p align="left" class="form">Your name</p> <p align="left"> <input name="userName" type="text" class="formtext" id="userName" size="40" maxlength="50" /> </p> <p align="left" class="form">Your email address </p> <p align="left" class="form"> <input name="email" type="text" class="formtext" id="email" size="40" maxlength="50" /> </p> <p align="left" class="form">Subject</p> <p align="left"> <input name="subject" type="text" class="formtext" id="subject" size="40" maxlength="50" /> </p> <p align="left" class="form">Your comments or query</p> <p align="left"> <textarea name="message" cols="40" rows="10" class="formtext" id="message"></textarea> </p> <p align="left" class="form"> <input name="Submit" type="submit" class="form" id="Submit" value="Submit" /> </p> <p class="form"> </p> </form> <?php /*$_POST */ $subject = $_POST['subject']; $userMessage = $_POST['message']; $userName = $_POST['userName']; $userEmail = $_POST['email']; $email = 'myemail@mywebsite.com'; $message = "Email sent from $userName \n Reply: $userEmail \n Subject: $subject \n Message: $userMessage"; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856124 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 Ok, I named the comment box 'message' as I didnt think this would make a difference- as long as the php pointed to 'message'? The HTML form is the right one, the only form I have made. Yes, the php file is in the same directory as the html form file. And yes the contents of send.php is the code I have been posting. I don't actually need the subject box, I only need the users name, email and comments and when they click 'submit' it comes to my email. I think the name field was named subject as I was trying to follow the tutorial as closely as possible and thats what they used. Still not working! Now I am not recieving any email at all (and yes I changed the 'myemail@mywebsite' address in the php code to mine) Thankyou for your help, Im sure you are going to become as frustrated as I am soon!!! Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856133 Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 ... might be a silly question, but did you remember to change the $email = 'myemail@mywebsite.com'; line to your email in the code i posted? if you didn't, change it and try it again.. otherwise try this code and tell me what gets displayed... I've made the subject always "New message from site"... you can change this to whatever you want. <?php /*$_POST */ $subject = 'New message from site'; $userMessage = $_POST['message']; $userName = $_POST['userName']; $userEmail = $_POST['email']; $email = 'myemail@mywebsite.com'; $message = "Email sent from $userName \n Reply: $userEmail \n Message: $userMessage"; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } echo "Subject: $subject<br/> User Name: $userName<br/> Posted Message: $userMessage<br/> Email Message: $message<br/><br/> To email: $email"; exit(); elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> HTML <form action="send.php" method="post" enctype="application/x-www-form-urlencoded" name="contactForm" id="contactForm"> <p align="left" class="form">Your name</p> <p align="left"> <input name="userName" type="text" class="formtext" id="userName" size="40" maxlength="50" /> </p> <p align="left" class="form">Your email address </p> <p align="left" class="form"> <input name="email" type="text" class="formtext" id="email" size="40" maxlength="50" /> </p> <p align="left" class="form">Your comments or query</p> <p align="left"> <textarea name="message" cols="40" rows="10" class="formtext" id="message"></textarea> </p> <p align="left" class="form"> <input name="Submit" type="submit" class="form" id="Submit" value="Submit" /> </p> <p class="form"> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856138 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 Yes I changed the email to my email address, that was the first thing I checked. Tried that code above, same thing, uploaded the html and php files, tested it and when you click 'submit' it just takes you to a blank page and Im not recieving any email! Thankyou so much for helping me, Im not expecting you to solve this, its getting silly now! Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856148 Share on other sites More sharing options...
MadTechie Posted June 15, 2009 Share Posted June 15, 2009 the if logic is wrong try this <?php error_reporting(E_ALL); /*$_POST */ $subject = 'New message from site'; $userMessage = $_POST['message']; $userName = $_POST['userName']; $userEmail = $_POST['email']; $email = 'myemail@mywebsite.com'; $message = "Email sent from $userName \n Reply: $userEmail \n Message: $userMessage"; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; }elseif(mail($email,$subject,$message)) { echo "Subject: $subject<br/> User Name: $userName<br/> Posted Message: $userMessage<br/> Email Message: $message<br/><br/> To email: $email"; echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856150 Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 okay, i just uploaded it on my site and it worked? i got rid of my debugging lines (i put them in the wrong spot anyway, didn't notice you had an elseif there): echo "Subject: $subject<br/> User Name: $userName<br/> Posted Message: $userMessage<br/> Email Message: $message<br/><br/> To email: $email"; exit(); this is the code as is on my server, minus the yourEmail@yourdomain.com thingo... <?php /*$_POST */ $subject = 'New message from site'; $userMessage = $_POST['message']; $userName = $_POST['userName']; $userEmail = $_POST['email']; $email = 'yourEmail@yourdomain.com'; $message = "Email sent from $userName \n Reply: $userEmail \n Message: $userMessage"; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856152 Share on other sites More sharing options...
joel24 Posted June 15, 2009 Share Posted June 15, 2009 if that doesn't work, put in the following lines ini_set ("display_errors", "1"); error_reporting(E_ALL); at the very top of the code.. the line after <?php and try and run the program and tell me exactly what happens? does it say Thank you for sending email etc? Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856155 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 HURRAH! Used 'MadTechie's code and it works. Thankyou so much! Im sure you've got better things to do, its really kind of you to help me Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856157 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 Thanks Joel Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856159 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 If I wanted to add CSS to jazz up the php form after it is submitted, can i just add this to the top of the page...? Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856160 Share on other sites More sharing options...
wednesdayaddams Posted June 15, 2009 Author Share Posted June 15, 2009 Ok no worries, I've worked it out, thanks again guys x Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856161 Share on other sites More sharing options...
MadTechie Posted June 15, 2009 Share Posted June 15, 2009 can you click topic solved bottom left please Quote Link to comment https://forums.phpfreaks.com/topic/162217-sending-a-simple-form/#findComment-856168 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.