christov Posted November 16, 2009 Share Posted November 16, 2009 Hi all, I'm really new to PHP so I'm still green with all the code and a lot of the things that php can or can't do, so mega apologies if this is the noobiest thing that you've ever seen. Basically I'm trying to create a web form that simply redirects you to a different page depending on what you've put into the form, simple submission etc but I can't seem to get it to work. All it does at the moment (as far as i know) is check if $email (email section of the form) is filled in, if it's not, it goes to the 'oops' page, if it is filled in, it goes to the 'success' page, and if there was a server error you get the 'fail' page. Now the problem I'm having is that when I submit the form you get taken to the 'success' page no matter whether you left the email section blank or not.. maybe I can't have more than one redirect per script? here's the code: <?php // // // // // EMail form section // // // // send to this address $to = "info@criticalsound.co.uk"; //with this subject $subject = "New Word Form Data"; //send this stuff in the email // e.g. // $messagename = $_REQUEST['messagename'] ; // will send the field marked as messagename and define it as $messagename for use in the fields array $email = $_REQUEST['email'] ; // checking for empty form submission if(empty($email)) header( 'Location: http://www.criticalsound.co.uk/form_oops.php' ) ; // add headers $headers = "From: $email"; // define the $fields array that sends all the data with human formatting $fields = array(); $fields{"messagename"} = "Name"; $fields{"email"} = "E-Mail"; // define $body which takes the array and sends it in a nice format $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } // actually get round to sending the email and choose what gets sent, then print something to tell the user if it worked or not $sent = mail($to, $subject, $body, $filename, $headers) ; if($sent) header( 'Location: http://www.criticalsound.co.uk/form_success.php' ) ; else header( 'Location: http://www.criticalsound.co.uk/form_fail.php' ) ; ?> and the form itself can be seen here: http://criticalsound.co.uk/wordform.php Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted November 16, 2009 Share Posted November 16, 2009 After you do a header redirect you need to tell the script to die(), or execution will continue as normal. I'm not up to the task of digging through your code to tell if this is exactly what the problem is, but a quick glance says it could be, so I figured it worth mentioning. Quote Link to comment Share on other sites More sharing options...
christov Posted November 16, 2009 Author Share Posted November 16, 2009 I did try using the exit; command, is that the same or similar? I will try die and see if it works. Quote Link to comment Share on other sites More sharing options...
mattyvx Posted November 16, 2009 Share Posted November 16, 2009 if you're having header problems see the sticky post for that. you can also use (as an alternative); echo '<meta http-equiv="refresh" content="1;url=form_fail.php" />'; where the number in content is the number in seconds before the page refreshes. For a smoother refresh Quote Link to comment Share on other sites More sharing options...
christov Posted November 22, 2009 Author Share Posted November 22, 2009 Hey all had a bit of a fiddle with the die(); command. If I add it as so: // checking for empty form submission if(empty($email)) header( 'Location: http://www.criticalsound.co.uk/form_oops.php' ) ; die(); it seems to half work, in that it will forward the user to the failed form page if nothing is entered in the email field. However, it still sends the email to the inbox... any way I can just basically stop the rest of the script from happening if $email is empty? I tried using exit(); but that just breaks the whole script. Also if I add the die() command to the end of that short bit of code and they enter something in the email field, the user now is not forwarded to the success page and instead gets a blank screen, but the email is not sent. I tried adding die() to the ends of the other headers lower down in the code as so: // actually get round to sending the email and choose what gets sent, then forward to a new page to tell the user if it worked or not $sent = mail($to, $subject, $body, $filename, $headers) ; if($sent) header( 'Location: http://www.criticalsound.co.uk/form_success.php' ) ; else header( 'Location: http://www.criticalsound.co.uk/form_fail.php' ) ; die(); and this still gives me the same result, blank screen Any thoughts? I could really do some help with this as the website is for my dissertation at uni. The web coding isn't being marked but I have to get it working asap really. Had a look at the header sticky, but I'm not sure if it really applies here since the php is in a seperate page and is triggered at the top of the web form like this: <form name=form action="./upload_word.php" method="post" enctype="multipart/form-data"> That should mean that the php is being used before the form data, so that should be ok right? Also tried using this instead: // checking for empty form submission if(empty($email)) echo '<meta http-equiv="refresh" content="1;url=http://criticalsound.co.uk/form_oops.php" />'; die(); But I still get the same results as above depending on where I put the die() Quote Link to comment Share on other sites More sharing options...
christov Posted November 22, 2009 Author Share Posted November 22, 2009 Sorry for the double post but I thought I'd just say that I solved the problem. Basically I'm now using this: if (!$_POST['email'] ) { die('<META HTTP-EQUIV="Refresh" CONTENT="0;URL=http://criticalsound.co.uk/form_oops.php">'); } to check if a field is filled in or not, which seems to work really well, it also doesn't send the data if the script "dies" so thats a double win right there. Whole script now looks like this: <?php // // // // // EMail form section // // // // checking for stuff not filled in if (!$_POST['email'] ) { die('<META HTTP-EQUIV="Refresh" CONTENT="0;URL=http://criticalsound.co.uk/form_oops.php">'); } //send this stuff in the email // e.g. // $messagename = $_REQUEST['messagename'] ; // will send the field marked as messagename and define it as $messagename for use in the fields array $email = $_REQUEST['email'] ; // define the $fields array that sends all the data with human formatting $fields = array(); $fields{"messagename"} = "Name"; $fields{"email"} = "E-Mail"; // send to this address $to = "info@criticalsound.co.uk"; //with this subject $subject = "New Word Form Data"; // add headers $headers = "From: $email"; // define $body which takes the array and sends it in a nice format $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } // actually get round to sending the email and choose what gets sent, then forward to a new page to tell the user if it worked or not $sent = mail($to, $subject, $body, $filename, $headers) ; if($sent) echo '<meta http-equiv="refresh" content="0;url=http://criticalsound.co.uk/form_success.php" />'; else echo '<meta http-equiv="refresh" content="0;url=http://criticalsound.co.uk/form_fail.php" />'; ?> Thanks for all ur help guys 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.