Hi, 🙂

I'm trying to figure out a good way to reload a page after a condition is met in a script.

		case 'status':

		$class_id = (!empty($HTTP_POST_VARS['id'])) ? intval($HTTP_POST_VARS['id']) : intval($HTTP_GET_VARS['id']);
		$status_switch = (!empty($HTTP_POST_VARS['status'])) ? intval($HTTP_POST_VARS['status']) : intval($HTTP_GET_VARS['status']);

		$sql = "UPDATE adr_classes
			SET status = $status_switch 
			WHERE cid = '$class_id'";
		if( !($result = $db->sql_query($sql)) ){
			message_die(GENERAL_ERROR, "Couldn't update classes info", "", __LINE__, __FILE__, $sql);}

		header('refresh: 0; url=/admin/admin_classes.php'); # redirects to our homepage

The use of header() fails because the page is already outputting html before the condition is met in the script. Usual output error.

Is there an easy way I'm missing that can refresh the page?

Thank you in advance. 🙂

    rewrite the structure so there is no output before header, and use header location with the full url

      dagon;10915018 wrote:

      rewrite the structure so there is no output before header, and use header location with the full url

      So there's no other way than using header() to do this?

      I struggle to believe that.

        use this to redirect to another page

        <meta http-equiv="refresh" content="2;url=http://www.phpbuilder.com">

        or this to reload the current page

        <meta http-equiv="refresh" content="5">

        2 & 5 is the time in seconds

          as its client side its not reliable, but you can risk it if you like living dangerously!

            dagon;10915071 wrote:

            as its client side its not reliable, but you can risk it if you like living dangerously!

            could you explain? is there a risk using meta refresh?

              It's client side. There is no obligation on the part of whatever is reading the response to do anything with it. And of course if it does work, the browser is going to be rendering a (blank?) page for the user and then after 2 or 5 or whatever seconds taking it away again.

              On the other hand there is no obligation on the part of whatever is reading the response to do anything with a Location: header. So a page that provides a link to where they should have gone would perhaps be a good thing.

              But it still comes down to good code design.
              "The use of header() fails because the page is already outputting html before the condition is met in the script."
              In other words "I'm doing a bunch of work before I check to see if I need to do it or not."

                So the problem is that you are doing this:

                1. output
                2. output
                3. if statment resulting in possible header() redirect

                A possible quick fix (in lieu of good design) is:

                1. set a string to empty: $output = "";
                2. keep adding output to the $output string instead of actually printing it, such as...
                3. $output += "<html>";
                4. $output += "<head>"; // etc...
                5. if statment resulting in possible header() redirect
                6. else echo $output; // Do this if no redirect was necessary

                  etully; you're essentially mimicking output buffering which, to me, is an excuse to not rework your code into the structure that it should be in.

                  Weedpacket nailed it; if a condition exists in which all of the code for the current display is inapplicable (e.g. you're going to redirect the user elsewhere), then why would you begin doing the work to display the page in the first place without checking this condition?

                  It's like asking an employee to move everything on one shelf to the one across the room, and when they're 3/4 of the way done, tell them "Nevermind - that task doesn't do us any good at all, try working over here instead.." :p

                    All true.

                    ExpendableDecoy: Please ignore my low tech, bad habit reinforcing, quick patch. Seriously, developing bad habits is bad.

                      Write a Reply...