kreut Posted February 11, 2011 Share Posted February 11, 2011 Hello, I'm having some problems with a redirect situation. I start on a page called Create Assignment. From this page I pass (using GET) the variable assignment_id to a page called Add_questions (the user can add questions to this specific assignment). When a user wants to add a question, they hit a link which brings them to my Database Update script. All works great so far! After the update, however, I want to redirect them BACK to the Add_questions page while keeping the same Assignment number. I've tried using: header('Location: list_assignment_question.php?assignment_id'); but I get this error: Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/algebra_book/main_scripts/db_insert_assignment_question.php:6) in /Applications/MAMP/htdocs/algebra_book/main_scripts/db_insert_assignment_question.php on line 12 Any thoughts? I'd be more than happy to provide more code as needed. Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/ Share on other sites More sharing options...
KevinM1 Posted February 11, 2011 Share Posted February 11, 2011 Look at our stickied thread at the top of this subforum: http://www.phpfreaks.com/forums/php-coding-help/header-errors-read-here-before-posting-them/ Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172759 Share on other sites More sharing options...
kreut Posted February 11, 2011 Author Share Posted February 11, 2011 Thank you for the quick reply. I took a look at the stick note. And, from what I understand (please tell me if this is correct..), I can't put the header command down within my code, as I do here: <?php $errors = array(); require_once('library.php'); try { require_once('db_definitions.php'); echo $_GET['question_id']; echo isset($_GET['question_id']); if (isset($_GET['question_id'])) { $data = array('question_id' => $_GET['question_id'], 'assignment_id' => $_GET['assignment_id']); $inserted = $dbWrite->insert('assignments_questions',$data); $returnto = $_SERVER['HTTP_REFERER']; header('Location: $returnto'); exit; } } catch (Exception $e) { echo $e->getMessage(); } However, I only want the redirect if there is a successful insert. I guess I'm not quite sure how to both: 1) Not put the header within the code 2) Be able to have a conditional redirect. Any help to this php newbie would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172764 Share on other sites More sharing options...
KevinM1 Posted February 11, 2011 Share Posted February 11, 2011 You can't have anything sent to the screen before the redirect. That's what the 'Headers already sent' message means - an echo is, ultimately, an HTTP response. And, if you think about it, echoing output to the screen doesn't make much sense in this case because you're going to another page. If you want to display a message, delay, then redirect the user, look into JavaScript which will allow you to use the browser for redirection rather than an HTTP header command. Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172778 Share on other sites More sharing options...
kreut Posted February 11, 2011 Author Share Posted February 11, 2011 I think that this is starting to make sense! I actually just put the echo's in to test things out. So...if I understand correctly, if I get rid of the echo, which is an ouput to the browser, then it should work??? Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172789 Share on other sites More sharing options...
kreut Posted February 11, 2011 Author Share Posted February 11, 2011 Never mind! Error gone.... Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172793 Share on other sites More sharing options...
bglinkerman Posted February 11, 2011 Share Posted February 11, 2011 Just an FYI, I have solved this issue by placing "ob_start();" for the very first line, then you have your redirect statements anywhere because the page is executing on the server side completely (ob stands for output buffering) before ever sending data to the client. So if your page finds that half way down the page a condition exists where it needs to redirect it will do so without an error. I use this just yesterday on a page and it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172796 Share on other sites More sharing options...
KevinM1 Posted February 11, 2011 Share Posted February 11, 2011 From a design standpoint, the output buffer is still a bit sloppy. The ideal design is to organize your scripts like so: ALL PHP Proccessing | | | V Output When you process first, you gain many advantages - you separate logic from presentation, you avoid header errors, you can store generated results in variables, simplifying your presentation code a lot. Even though PHP gives you the option to jump in and out of HTML on the fly, it's really not the ideal way to structure your code. I can't tell you how many people I've seen have loops of PHP where they're writing entire forms in straight HTML, with inline JavaScript to boot, wonder why something is broken. Separation of concerns is the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/227370-redirecting-with-the-help-of-a-get-variable/#findComment-1172803 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.