Jump to content

Redirect Header Issue


steveb1471

Recommended Posts

Hi

Everyone was kind enough to help with my last issue and am now nearly there.

The data actually populates correctly now in my database, however, it will not direct me to my redirect.

I get the error

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\loginsystem\Permnew.php:4) in C:\xampp\htdocs\loginsystem\Permnew.php on line 124

This is the redirect i am trying to do

anyone any ideas?

Thanks

Steve

 

<?php
 require "header.php";
?>

<main>
  <div class="wrapper-main">

        <div class="welcomelogged">
          <p>Adding A Permanent New Starter<p>                  
        </div>

    <form class="form-addperm" action="" method="post">
		<table id="Tableperm" width="1000px;" border="0">
			<tr>
			<th align="right" scope="row"><label for="select">Week Commencing</label></th>
	        <td><select name="WeekComm">
	        <option value="WC 6th April">WC 6th April</option>
	        <option value="WC 13th April">WC 13th April</option>
	        <option value="WC 20h April">WC 20h April</option>
	        <option value="WC 27h April">WC 27h April</option>
	        </select></td>
			</tr>
			<tr>
	      	<th align="right" scope="row"><label for="StartDate">Start Date</label></th>
	      	<td><input type="date" name="StartDate" placeholder="Start Date"></td>
	      	</tr>
	      	<tr>
			<th align="right" scope="row"><label for="select1">Consultant</label></th>
	        <td><select name="Consultant">
	        <option value="Steven Buntin">Steven Buntin</option>
	        <option value="Sam Ahmed">Sam Ahmed</option>
	        <option value="David Millington">David Millington</option>
	        <option value="Steven Nixon">Steven Nixon</option>
	        <option value="Grahame Walsh">Grahame Walsh</option>
	        <option value="Helal Ahmed">Helal Ahmed</option>
	        </select></td>
			</tr>
			<tr>
	      	<th align="right" scope="row"><label for="FirstName">First Name</label></th>
	      	<td><input type="text" name="FirstName" placeholder="First Name"></td>
	      	</tr>
			<tr>
	      	<th align="right" scope="row"><label for="LastName">Last Name</label></th>
	      	<td><input type="text" name="LastName" placeholder="Last Name"></td>
	      	</tr>
	      	<tr>
	      	<th align="right" scope="row"><label for="ClientName">Client Name</label></th>
	      	<td><input type="text" name="ClientName" placeholder="Client Name"></td>
	      	</tr>
			<th align="right" scope="row"><label for="Position">Position</label></th>
	      	<td><input type="text" name="Position" placeholder="Position"></td>
	      	</tr>
	      	<th align="right" scope="row"><label for="Comments">Comments</label></th>
	      	<td><input type="text" name="Comments" placeholder="Comments"></td>
	      	</tr>
	      	<tr>
	      	<th align="right" scope="row"><label for="Salary">Salary</label></th>
	      	<td><input type="varchar" name="Salary" placeholder="Salary"></td>
	      	</tr>
	      	<tr>
	      	<th align="right" scope="row"><label for="ChargePercentage">Charge Percentage</label></th>
	      	<td><input type="varchar" name="ChargePercentage" placeholder="ChargePercentage"></td>
	      	</tr>
	      	<ty>
	      	<th align="right" scope="row"><label for="GPNotes">GP Notes</label></th>
	      	<td><input type="text" name="GPNotes" placeholder="GPNotes"></td>
	      	</tr>
		</table>
		<button type="submit" name="addstarter">Add Starter</button>
    </form>	  
  </div>
 </main>

<?php

$dBServername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "loginsystemtut";

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);


$conn = mysqli_connect($dBServername, $dBUsername, $dBPassword, $dBName);


if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


if (isset($_POST['addstarter'])) {


  $WeekComm = $_POST['WeekComm'];
  $StartDate = $_POST['StartDate'];
  $Consultant = $_POST['Consultant'];
  $FirstName = $_POST['FirstName'];
  $LastName = $_POST['LastName'];
  $ClientName = $_POST['ClientName'];
  $Position = $_POST['Position'];
  $Comments = $_POST['Comments'];
  $Salary = $_POST['Salary'];
  $ChargePercentage = $_POST['ChargePercentage'];
  $GPNotes = $_POST['GPNotes'];

  $sql = ("INSERT INTO permanent (WeekComm, StartDate, Consultant, FirstName, LastName, ClientName, Position, Comments, Salary, ChargePercentage, GpNotes) values (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

  $stmt = mysqli_stmt_init($conn);

      	if (!mysqli_stmt_prepare($stmt, $sql)) {
          // If there is an error we send the user back to the signup page.
          header("Location: ../signup.php?error=sqlerror");
          exit();
        }
        else {


    	mysqli_stmt_bind_param($stmt,"sssssssssss",$WeekComm,$StartDate,$Consultant,$FirstName,$LastName,$ClientName,$Position,$Comments,$Salary,$ChargePercentage,$GPNotes);
      
      	mysqli_stmt_execute($stmt);
    	}

  header("Location: ../loginsystem/permnew1.php?success");
  exit();
    }
?>

 

Link to comment
Share on other sites

You must not send anything back to the browser before issuing the redirect.  No HTML tags, not even whitespace.   

In the code you show, you're sending most of the form before issuing the redirect.

It's conventional to do the POST processing first, then render the rest of the Page if the User is still "here". something like this: 

if ( Posted_Data_Present() ) 
{ 
   ProcessPostedData_IncludingAnyRedirects(); 
} 

DisplayForm_IncludingPostedData(); 

Regards, 
   Phill  W.

Link to comment
Share on other sites

19 hours ago, steveb1471 said:

would it be better to have the action of the form going to another page

I would say no.  The displaying of the form and it's processing are closely coupled.  There's not much benefit in splitting them out into separate files. 

Just because you process the data before displaying the form doesn't mean that the code to do that processing has to go first in the file! 
Call a function that does the work.  That function can be defined anywhere in the file.

Also, there are limits on the number of redirects you can chain together, so why introduce another one when you don't need it? 

Regards, 
   Phill  W.

 

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.