khujo56 Posted July 3, 2007 Author Share Posted July 3, 2007 is there a way you can say it has been submitted to my e-mail address then have it re-directed to a new page? Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288882 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 This is always fun looking at how people do stuff the hard way. If you are going to be using more forms like this, I would suggest a different method of error reporting than just echoing or exiting, bad habit and does not allow for full control. I would actually suggest a validate function <?php function validate(&$error_msg) { $validate_array = array("email", "name", "message"); // add more here for verification $error_msg = ""; foreach ($validate_array as $value) { switch ($value) { case 'email': // note requires a "is_email" function that you have to create if (!isset($_POST[$value]) || empty($_POST[$value]) || !is_email($_POST[$value])) { $error_msg .= "Email is missing or in properly formatted.<br />"; } break; case 'name': if (!isset($_POST[$value]) || empty($_POST[$value])) { $error_msg .= "Name field is requried.<br />"; } break; case 'message': if (!isset($_POST[$value]) || empty($_POST[$value])) { $error_msg .= "Message field is requried.<br />"; } break; } } if (!empty($error_msg)) { return false; } return true; } function is_email($email) { return true; // for now you need to write the code here. } if (isset($_POST['submit'])) { if (!validate($error_msg)) { header('Location: http://www.placeiwanttoredirectto.com/page.php?error_msg=' . $error_msg); die(); } // else everything is valid do processing here } ?> Which in return would allow you expand easily and have it in a nice function could could be included via a functions file and should allow for you to use this in any other script with ease. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288885 Share on other sites More sharing options...
xyn Posted July 3, 2007 Share Posted July 3, 2007 Alternativly, modify this to your hearts content. Form.htm <form method="POST" action="sendmail.php"> <div align="center"> <center> <table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="400"> <tr> <td width="122"><b>Name:</b></td> <td width="270"><input type="text" name="name" size="20"></td> </tr> <tr> <td width="122"><b>E-mail:</b></td> <td width="270"><input type="text" name="email" size="20"></td> </tr> <tr> <td width="122"><b>Message:</b></td> <td width="270"></td> </tr> <tr> <td width="396" colspan="2"><textarea rows="6" name="message" cols="47"></textarea></td> </tr> <tr> <td width="392" colspan="2"><input type="submit" value="Submit"></td> </tr> </table> </center> </div> </form> sendmail.php <?php if(isset($_POST['name']) && empty($_POST['name'])) { $error[] = "Post your name"; } if(isset($_POST['email']) && empty($_POST['email'])) { $error[] = "Post your name"; } if(isset($_POST['message']) && empty($_POST['message'])) { $error[] = "Post your name"; } if(is_array($error)) { echo("errors:<br>"); foreach($error as $x => $i) { echo("- ".$i."<br>"); } } else { $to = "[email protected]"; $subject = "subject"; $message = $_POST['message']; $name = $_POST['name']; $from = $_POST['email']; mail($to, $subject, $message, "From: $name<$from>"); echo("<script>alert(\"Thanks!\nE-mail has been sent!\");</script>"); $url = "redirect_page.htm"; header("Location: $url"); } ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288887 Share on other sites More sharing options...
xyn Posted July 3, 2007 Share Posted July 3, 2007 -- Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288893 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 Problem with your function. you could make an array quicker than running your function :/ This is always fun looking at how people do stuff the hard way. If you are going to be using more forms like this, I would suggest a different method of error reporting than just echoing or exiting, bad habit and does not allow for full control. I would actually suggest a validate function <?php function validate(&$error_msg) { $validate_array = array("email", "name", "message"); // add more here for verification $error_msg = ""; foreach ($validate_array as $value) { switch ($value) { case 'email': // note requires a "is_email" function that you have to create if (!isset($_POST[$value]) || empty($_POST[$value]) || !is_email($_POST[$value])) { $error_msg .= "Email is missing or in properly formatted.<br />"; } break; case 'name': if (!isset($_POST[$value]) || empty($_POST[$value])) { $error_msg .= "Name field is requried.<br />"; } break; case 'message': if (!isset($_POST[$value]) || empty($_POST[$value])) { $error_msg .= "Message field is requried.<br />"; } break; } } if (!empty($error_msg)) { return false; } return true; } function is_email($email) { return true; // for now you need to write the code here. } if (isset($_POST['submit'])) { if (!validate($error_msg)) { header('Location: http://www.placeiwanttoredirectto.com/page.php?error_msg=' . $error_msg); die(); } // else everything is valid do processing here } ?> Which in return would allow you expand easily and have it in a nice function could could be included via a functions file and should allow for you to use this in any other script with ease. Its not a problem with the function. The function would work just fine. Just a different way of doing it. I generally only use validate functions because than I have one location to change all that information. I actually go a lot deeper in my personal code so one validate function can validate everything my site needs. Just an extra idea, that does work. The nicest thing about it, if I have certain fields like name and message that do not need any special validation I can easily do something like this: <?php function validate(&$error_msg) { $validate_array = array("email", "name", "message"); // add more here for verification $error_msg = ""; foreach ($validate_array as $value) { switch ($value) { case 'email': // note requires a "is_email" function that you have to create if (!isset($_POST[$value]) || empty($_POST[$value]) || !is_email($_POST[$value])) { $error_msg .= "Email is missing or in properly formatted.<br />"; } break; case 'message': case 'name': if (!isset($_POST[$value]) || empty($_POST[$value])) { $error_msg .= ucwords($value) . " field is requried.<br />"; } break; } } if (!empty($error_msg)) { return false; } return true; } function is_email($email) { return true; // for now you need to write the code here. } if (isset($_POST['submit'])) { if (!validate($error_msg)) { header('Location: http://www.placeiwanttoredirectto.com/page.php?error_msg=' . $error_msg); die(); } // else everything is valid do processing here } ?> case 'message': case 'name': if (!isset($_POST[$value]) || empty($_POST[$value])) { $error_msg .= ucwords($value) . " field is requried.<br />"; } break; } Which saves on typing time. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288903 Share on other sites More sharing options...
xyn Posted July 3, 2007 Share Posted July 3, 2007 I didnt mean to say there is a problem with your function, as there wasn't it's just i find arrays quicker and easier to do then messing with Swith, case and break. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288911 Share on other sites More sharing options...
khujo56 Posted July 3, 2007 Author Share Posted July 3, 2007 well either one works great..so thank you. now i still did not get the e-mail that i tested. is there a problem with hotmail accounts it should usually be fast. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288912 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 I didnt mean to say there is a problem with your function, as there wasn't it's just i find arrays quicker and easier to do then messing with Swith, case and break. Switch/cases are very useful and easy to use. Either way its personal preference. I love the functionality and flexibility a switch/case can give you over just an array. Generally, hotmail aol gmail and yahoo have spam filters that filter out emails. Usually emails coming from servers are filtered out as spam. Check the hotmail junk box. There is really no set solution other than making sure you set mail headers properly. www.php.net/mail Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288918 Share on other sites More sharing options...
scarhand Posted July 3, 2007 Share Posted July 3, 2007 After reading frost110's reponse I am going to be using validation functions like that from now on! The way I showed you to do it with echo's and exit's was the simplest and easiest way I could have showed you with minimal coding, but then again like I said I am still a newb. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288922 Share on other sites More sharing options...
khujo56 Posted July 3, 2007 Author Share Posted July 3, 2007 well i tried my actual e-mail address associated with my website and i still don't get anything. what could be the problem?? Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289115 Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 well i tried my actual e-mail address associated with my website and i still don't get anything. what could be the problem?? Sometimes servers do not like sending email address to [email protected] something to do with the relaying bit of it, unsure. I know my server will not send an email address to that address, so I have a gmail account setup which forwards all emails to my main account address and just use that for emails to be sent from my server. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289120 Share on other sites More sharing options...
khujo56 Posted July 4, 2007 Author Share Posted July 4, 2007 is there an alternative to that. it kinda defeats the purpose of a form if the info cannot be sent to the assigned e-mail address Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289260 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 is there an alternative to that. it kinda defeats the purpose of a form if the info cannot be sent to the assigned e-mail address what ? Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289272 Share on other sites More sharing options...
khujo56 Posted July 4, 2007 Author Share Posted July 4, 2007 the form works fine but the info does not get to my assigned address...i tried my hotmail account then i switched to my other e-mail address..neither receives any Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289279 Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 maybe you dont have a mail server Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289281 Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 so i finally figured out that i did not have a php package for my hosting. finally switched over to php, it works fine but it does redirect to the assigned address. instead i get this message Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/n/r/jnr1975/html/mailer.php:31) in /home/content/j/n/r/jnr1975/html/mailer.php on line 34....it should redirect to the main page. below is the php script. <?php if(isset($_POST['name']) && empty($_POST['name'])) { $error[] = "Post your name"; } if(isset($_POST['email']) && empty($_POST['email'])) { $error[] = "Post your e-mail address"; } if(isset($_POST['message']) && empty($_POST['message'])) { $error[] = "Post your comments"; } if(is_array($error)) { echo("errors:<br>"); foreach($error as $x => $i) { echo("- ".$i."<br>"); } } else { $to = "[email protected]"; $subject = "subject"; $message = $_POST['message']; $name = $_POST['name']; $from = $_POST['email']; mail($to, $subject, $message, "From: $name<$from>"); echo("<script>alert(\"Thanks!\E-mail has been sent!\");</script>"); $url = "http://www.iheartvacation.com/"; header("Location: $url"); } ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290177 Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 header("Location: $url"); will fail beacuse you have an output above the header header will work if theres no thing to be displayed on top of it or even below Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290179 Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 so how would i change that ??? Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290185 Share on other sites More sharing options...
mmarif4u Posted July 5, 2007 Share Posted July 5, 2007 try this at very top of ur script for header problem. <?php ob_start(); rest of ur code ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290187 Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 that does not work, sorry Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290230 Share on other sites More sharing options...
redarrow Posted July 5, 2007 Share Posted July 5, 2007 <?php if(isset($_POST['name']) && empty($_POST['name'])) { $error[] = "Post your name"; } if(isset($_POST['email']) && empty($_POST['email'])) { $error[] = "Post your e-mail address"; } if(isset($_POST['message']) && empty($_POST['message'])) { $error[] = "Post your comments"; } if(is_array($error)) { echo("errors: "); foreach($error as $x => $i) { echo("- ".$i." "); } } else { $to = "[email protected]"; $subject = "subject"; $message = $_POST['message']; $name = $_POST['name']; $from = $_POST['email']; mail($to, $subject, $message, "From: $name<$from>"); echo("<script>alert(\"Thanks!\E-mail has been sent!\");</script>"); if($to){ $url = "http://www.iheartvacation.com/"; header("Location: $url"); } } ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290251 Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 HI i tried that script..when it says that the info has been sent, instead of re-directing to the assigned page, i get this message instead : Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/n/r/jnr1975/html/mailer.php:33) in /home/content/j/n/r/jnr1975/html/mailer.php on line 37 Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290581 Share on other sites More sharing options...
xyn Posted July 5, 2007 Share Posted July 5, 2007 At the top of your page... add. (aka above the <head> tags) <?php ob_start(); ?> and at the bottom of your page <?php ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290584 Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 so post this on the actual html page instead of the php page? Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290587 Share on other sites More sharing options...
xyn Posted July 5, 2007 Share Posted July 5, 2007 yep. because that allows you to use header(); while using <head></head> in html.. I believe thats the problem. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.