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? Quote Link to comment 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. Quote Link to comment 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@here.com"; Â Â $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"); } ?> Quote Link to comment Share on other sites More sharing options...
xyn Posted July 3, 2007 Share Posted July 3, 2007 -- Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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?? Quote Link to comment 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 you@serverdomain.com 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. Quote Link to comment 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 Quote Link to comment 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 ? Quote Link to comment 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  Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 4, 2007 Share Posted July 4, 2007 maybe you dont have a mail server Quote Link to comment 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 = "webmaster@iheartvacation.com";   $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"); } ?> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 so how would i change that ??? Quote Link to comment 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 ?> Quote Link to comment Share on other sites More sharing options...
khujo56 Posted July 5, 2007 Author Share Posted July 5, 2007 that does not work, sorry Quote Link to comment 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 = "webmaster@iheartvacation.com"; Â Â $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"); } Â } ?> Quote Link to comment 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 Quote Link to comment 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(); ?> Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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.