Redirect back to form

OK, I know this is in here somewhere but can’t seem to find it.

What I have is a form to enter data into my mySQL database. When the form is complete (and error free), I want to user to be directed back to the same form. However, I get the " Cannot modify header information - headers already sent by " warning and the script stops.

How can I change that? I can put the code here but it is quite big and not sure how much or what part is relevant for this discussion.

Thanks
F

It sounds like you are submitting the form to another page to do the processing and then trying to use a header redirect to go back to the form. The redirect would have to happen before anything is sent to the browser. Nothing can be output before using header.

You could also submit the form page to itself and do all the processing on the form page. Then there would be no need to redirect back to it.

I am doing all the form processing on the form page.

So, how can I get back to the beginning of the form? Confused - which is normal for me.

F

What do you mean by “get back to the beginning of the form”? Do you just want to clear the form values?

Yes, clear the form and ready for new data. Sorry, but got another thing going so was trying to hurry.

At the beginning of the page, I have the line " if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) " with the else to display the form with blank values. Just mentioning this in case it makes a difference.

F

Can we see some code? :slight_smile:

How much?? It is pretty long so am giving only the first part - not sure again how much is necessary. This is the beginning of the form with the stuff to validate it at the bottom of the form (much later since there is 10 input fields).



<?php
	session_start();
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST') 
  { 
    $task = isset($_POST['task']) ? $_POST['task'] : false; 
    
     if ($task == "Save") {
      $formValues = $_POST; 
      $formErrors = array();  
      if (!VerifyForm($formValues, $formErrors)) 
      DisplayForm($formValues, $formErrors); 
      else     {
        echo 'Processing form' ;
        ProcessForm($formValues);
        header("Location: ".$_SERVER['PHP_SELF']);     }
      }
      else if ($task == 'Return to main page')   
        header('Location: index.php');
  }  
  else 
    DisplayForm(null, null);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>SACCC Form</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <style type="text/css" media="screen">
  	@import "css/main.css";
	  @import "css/top-positioned-labels.css";
  
   .error_msg {
      margin: 0;
      font-size: .8em;
      font-weight: 700;
      color: red;
   }
   <script type="text/javascript" src="jsquery/jquery.js"></script> 
  </style>
</head>
<body>
<?php

function DisplayForm($values, $errors) { ?>
<div id="page">
		<div id="header">
			<h1>
				SACCC Event Input
			</h1>
         <p style="margin-top: -35px; color:#FFF;">All items marked with an asterisk (*) are required.</p>
		</div>
		<?php 
		 $no_string = "&nbsp;";
    if (count($errors) > 0) 
      echo '<span style="color: red; font-size: 1.2em; font-weight: 600; margin-top: -1.8em; margin-bottom: -2em; "><center>There were some errors in your submitted form, please correct them and try again. </center></span>'; 
    else
       echo '<span style="color: red; font-size: 1.2em; font-weight: 600; margin-top: -1.8em; margin-bottom: -2em; ">'.$no_string.' </span>';
                      ?>
		<div id="content">
		  <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> 
<!--      <form action="mail.php" method="post">    -->
			<fieldset style="margin-left: 20px;">
					<legend>
						<span>Event</span>
					</legend>


Try changing the following. Assuming you are using the $formValues variable to populate the form values this should fix the issue. If you are populating the form values using the $_POST values directly this wont work.

ProcessForm($formValues); 
/*Add this line to clear you form values after they are processed*/
unset($formValues);
/*Remove the following line*/
//header("Location: ".$_SERVER['PHP_SELF']);

Do you have that enter in the file before <?php ?
If so, then that’s your problem right there. Remove the enter and the everything should work.

there’s also the echo 2 lines above header("Location: ".$_SERVER[‘PHP_SELF’]);

Indeed there is, nice catch :slight_smile:

First, thanks for the help.

Second, ScallioXTX, I don’t understand “Do you have that enter in the file before <?php ?” so explain the “enter” meaning.

Last, jdiben, I removed the echo statement referenced in the other reply.

When pressing the “Save” button, it just comes back to a “blank” screen.

Thanks again
F

By “enter” I meant carriage return, i.e., a blank line before the <?php. The file doesn’t start on line 1 but on line 2.

Sorry, but when I put it here I probably put that in.

My real file does have the <?php on line 1.

So, it still does not redisplay the form after submitting, validation and writing to the database.

Still looking at how to get this to work like that.

F