Jump to content

Redirecting with the help of a GET variable


kreut

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.