ElaineN46 Posted October 17, 2014 Share Posted October 17, 2014 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <style> .error {color: #FF0000;} h6 { font-family: bookman old style; font-size:20px; text-align: center; font-weight: normal; } h5 { font-family: bookman old style; font-size:15px; text-align: center; font-weight: normal; } </style> <?php $nameErr = $emailErr = $websiteErr = $categoryErr; $name = $email = $comment = $website = $reset = $category; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["website"])) { $websiteErr = "URL is required"; } else { $website = test_input($_POST["website"]); if (!preg_match("/\b(??:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; } } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["category"])) { $categoryErr = "Category is required"; } else { $category = test_input($_POST["category"]); } if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['website']) && !empty($_POST['category'])) { $myemail = "links@loadsofads.com"; $subject = "Link Submission"; $message = "Your Link Submission form has been submitted by: Website Name: $name E-mail: $email URL: $website Category: $category Description: $comment"; mail($myemail, $subject, $message); header('location:submitthanks.php'); }} function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <?php include'header.php'?> <h6>Link Submission</h6> <h5><p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name Of Site: <input type="text" name="name" value=""> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value=""> <span class="error">* <?php echo $emailErr;?></span> <br><br> URL: <input type="text" name="website" value=""> <span class="error">* <?php echo $websiteErr;?></span> <br><br> Description: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Category Of Site: <select size="1" name="category"> <option value=""> -- Please select -- </option> <option>Arts</option> <option>Business</option> <option>Computers</option> <option>Games</option> <option>Health</option> <option>Home</option> <option>Kids and Teens</option> <option>News</option> <option>Recreation</option> <option>Reference</option> <option>Science</option> <option>Shopping</option> <option>Society</option> <option>Sports</option> <option>World</option> </select><span class="error">* <?php echo $categoryErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="Reset Form"> </form> <?php include'footer.php'?> Hello All, OK so I have been at this for a few days now and everywhere I go to learn or read information it says the same thing, to redirect the code is header('location:mypage.php');exit(); but it just will not redirect it every thing I try it does not load the redirect page. it clears the form and sits there, I do not know why! Can please someone please help me and see why it is not doing it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/ Share on other sites More sharing options...
maxxd Posted October 17, 2014 Share Posted October 17, 2014 Turn on error reporting. You've got output to the screen before your redirect call, which won't work. The reason you're seeing the form before it sits there is because the form is the output to the screen that's causing your script not to work. Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494048 Share on other sites More sharing options...
ginerjm Posted October 17, 2014 Share Posted October 17, 2014 (edited) There are some posts out there that say the statement has to be formed like this: header("Location: name.php"); Note the cap and the space. As for the previous post - he's right. As for how you said you thought the header was supposed to be used, how come your only header command is NOT followed by exit? And one more thing: This is the stupidest thing that I see in new coders code: ?> <?php What is that supposed to be? Turn off php mode; turn on php mode? Why? Edited October 17, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494049 Share on other sites More sharing options...
ElaineN46 Posted October 17, 2014 Author Share Posted October 17, 2014 cause when I take it out and just put the whole page in php it gives an error and I am not a coder so I try by putting things together and testing...I have fun doing it and if it works well it works, if not I try and get help from people like you....who usually do not say that I am stupid, but all is good! Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494051 Share on other sites More sharing options...
ElaineN46 Posted October 17, 2014 Author Share Posted October 17, 2014 how do you turn on error reporting? Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494052 Share on other sites More sharing options...
ginerjm Posted October 17, 2014 Share Posted October 17, 2014 I didn't say you were stupid - I said you did a stupid thing. Have you not read my signature? Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494053 Share on other sites More sharing options...
ElaineN46 Posted October 17, 2014 Author Share Posted October 17, 2014 ok done...no errors Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494054 Share on other sites More sharing options...
ginerjm Posted October 17, 2014 Share Posted October 17, 2014 As Maxxd said earlier - you can't use 'header' once you have sent output to the screen. Since you're new - Anything that is outside of the php opening/closing tags is considered output. That means the php parser ignores it and just passes along to be sent to the client. So - your script begins with a set of html to begin a proper html document, ie, web page. Problem is you have some logic in your script that decides it doesn't want to do that. Too late - you already began the page. Solution: Design your scripts so that you examine the incoming data (from a form on the page perhaps?) and process it and decide what you want to do and do it BEFORE you start sending output, whether it is html code, javascript, css or any php variables. Basically php code at the beginning of the script, output at the end. Of course that html output at the end will probably contain php vars that you build in the top to send some dynamic data along with that html. Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494055 Share on other sites More sharing options...
ElaineN46 Posted October 17, 2014 Author Share Posted October 17, 2014 ok I have played around with it....and moved stuff around, but to no avail, im sorry I guess I just don't have the php writing skills, does anyone have any suggestions on the placement of this code so it can work? Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494060 Share on other sites More sharing options...
Frank_b Posted October 17, 2014 Share Posted October 17, 2014 (edited) i am sorry but your code is a bit to long to edit it all in my available free time. But i give you a basic example <?php // Here comes your PHP programm logic! // There will be not a single output. That means nog echo or printf(), etc // assume this variables has been loaded from the database: $isLoggedIn = true; $username = 'Frank'; $prevLogin = '2014/10/06 14:07:00'; // what will we do with users that are nog logged in? if(!$isLoggedIn) { header('Location: login.php'); exit; // stop this script after the redirect! } ?> <!-- HERE STARTS YOUR OUTPUT, THAT IS ALWAYS THE LAST PART OF YOUR CODE. --> <!-- FROM THIS MOMENT WE ONLY USE PHP SNIPPETS TO ECHO PHP VARIABLES OR TO LOOP THROUGH AN ARRAY TO DISPLAY THE ELEMENTS. --> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Admin Home</title> </head> <body> <div style="userinfo"> <p>Hello <?php echo $username; ?>, we are glad that you are back since <?php echo $prevLogin; ?> !</p> <h1>Admin Home</h1> <p>bla bla bla</p> </div> </body> </html> Edited October 17, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291899-header-redirect-issue/#findComment-1494061 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.