amkaos Posted February 21, 2013 Share Posted February 21, 2013 (edited) Hi: brand new to php. need help w/ form. i want to display omitted but required information on same page as the form after validation. also, to keep the user data filled in.. i got this from php master tut.. it does not work. i changed actual sites and personal info to generic any help is greatly appreciated... thanx <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = 'myemail.com'; $email_subject = "Questons Form"; $emailErr = $commentsErr = ""; $email = $comments = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["email"])) { $emailErr = "Missing"; } else { $email = $_POST["email"]; } $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!--include your own success html here --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd"> <html> <head> <meta http-equiv="Refresh" content="10; URL=website_form.html"> <title>form show</title> <meta http-equiv="Content-Language" content="en-us" /> <meta http-equiv="imagetoolbar" content="no" /> <meta name="MSSmartTagsPreventParsing" content="true" /> <meta name="description" content="Society presents annual in 2012, at American Center. " /> <meta name="keywords" content="Society ,Center" /><style type="text/css" media="all">@import "css/master.css";</style> <body> <p><div id="textbox"><br><br> <center><h1>Thank you for contacting us. <br /> We will be in touch with you very soon.</h1> <br><br> Please wait 10 seconds and you will be transferred to the next page or <a href=show.org/index.html><font color="#0000ff">click here</font></a> to go now!</center> <p><center>Please visit our website <a href="show.org/"><i>www.show.org</i></a> to learn more <br /> about the Society </center> <p> </p> </div> </div> </div> </body> </html> <?php { } ?> </body> </html> Edited February 21, 2013 by amkaos Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/ Share on other sites More sharing options...
AyKay47 Posted February 21, 2013 Share Posted February 21, 2013 I don't see a form.. Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/#findComment-1413959 Share on other sites More sharing options...
exeTrix Posted February 21, 2013 Share Posted February 21, 2013 Ok, that code as a number of duplicate checks which are redundant. What I'm going to suggest should NOT be used in a production environment, however, due to you being new to PHP there's no point in me throwing information at you that you're going to struggle retain/understand. We all learnt to walk before we learnt to run... unless you're a freak of nature form.php - really simple, just a form that sends data via the POST method to a script called process.php <html> <head></head> <body> <form action="process.php" method="post" > <label for="email">Email</label><input type="text" name="email" id="email" /><br /> <label for="comments">Comments</label><textarea name="comments" id="comments"></textarea><br /> <input type="submit" /> </form> </body> </html> So when a user hits submit they'll land at process.php lets define the logic behind that: <?php //when submitting a form via the POST method (defined as one of the form attributes) we will use the $_POST superglobal to get the values entered by the user on the next page //first lets check to make sure we even have the values to begin //empty will first check to make sure that the variable exists, then it will make sure it has a none empty value //from memory empty values are: 0, "0", null, false //notice the || this means if email is empty or comments are empty execute what's between { } if( empty( $_POST['email']) || empty( $_POST['comments'] ) ){ //anyway if either of the above are empty then send them back to the form header("Location: http://yourdomain.com/form.php"); //exit is here to make sure that the rest of the page doesn't execute exit; } //now we know we have valid values (I use the term valid extremely loosly) we can continue $email_subject = 'Questions form'; $email_to = 'you@yourdomain.com'; $email_from = $_POST['email']; $email_message = $_POST['comments']; $headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); //ok so we've assigned our POST data to some variables and generated some headers $response = @mail( $email_to, $email_subject, $email_message, $email_headers ); //now you might be thinking what's the @symbol all about? It basically supresses nasty fugly error produced when mail fails for whatever reason //also notice how we've done $response = @mai... this is because mail will return a boolean value (true/false) //to let the scriptknow if errors were encountered or not. This is good for testing the response if( $response === true ){ echo "Shazam email sent holmes" }else{ echo "Aww snap, failed to send the email. Please try again." } I've left out the html stuff on the second page, but that's about the long and short of an insecure crude email sending facility. Further reading: Cross Site Request Forgeries Hope that helps, give me a shout if you have any further questions. Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/#findComment-1413963 Share on other sites More sharing options...
amkaos Posted February 21, 2013 Author Share Posted February 21, 2013 Hi: thanx for the replies.. this is where i took reference from:: http://phpmaster.com/form-validation-with-php/ can i assuse that your code gives me the missing-req'd-info errors on same page? i do have working contact page and when req'd info is not complete, user is taken to page saying whats missing.. the report on the same form page is much nicer. thanx Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/#findComment-1413984 Share on other sites More sharing options...
exeTrix Posted February 21, 2013 Share Posted February 21, 2013 I'm sorry but that's rather irritating. I have taken the time to do the above for you and you quite clearly having read or bothered to understand what's going on. Stop copy and pasting and attempt to understand, if you don't want to learn then get a freelancer. Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/#findComment-1414002 Share on other sites More sharing options...
amkaos Posted February 21, 2013 Author Share Posted February 21, 2013 Hi: thanx for the replies.. this is where i took reference from:: http://phpmaster.com...ation-with-php/ can i assuse that your code gives me the missing-req'd-info errors on same page? i do have working contact page and when req'd info is not complete, user is taken to page saying whats missing.. the report on the same form page is much nicer. thanx thanx again for helping.. i just wanted to understand what you are showing me.. i am new and not sure what im looking at.. i am probly a little bit dizzy .. i ve been trying hard on my own for a while before i come here.. this is my real contact form from our non profit org..and how it works for now.. http://www.kcorchidshow.org/OSGKC_Questons.html Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/#findComment-1414016 Share on other sites More sharing options...
amkaos Posted February 25, 2013 Author Share Posted February 25, 2013 I'm sorry but that's rather irritating. I have taken the time to do the above for you and you quite clearly having read or bothered to understand what's going on. Stop copy and pasting and attempt to understand, if you don't want to learn then get a freelancer. Dear exe trix:: thanx for your reply last week.. i was so tired and blurry that day, i didnt see what was in front of me.. this project is for our not-for-profit club and i did not come back to it until today. i now see what you gave me. i think i understand and will try to adapt to my use. i am concerned to use this as worry-free tho. i do not make habit of joining forums for help and offending anyone who posts for my benefit.. sorry again. Quote Link to comment https://forums.phpfreaks.com/topic/274786-help-request-form/#findComment-1414893 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.