Jump to content

"header cannot be modified" error


bulrush

Recommended Posts

Yes, I read the sticky, but I still don't understand why I'm getting this error or how to fix it. When this form is submitted I need to run some code to save the "grid" to $_SESSION, then call the next php screen/script. Here is my whole code for editpartsearch.php, which then calls editpart.php.

 

<?php session_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Part Search Screen</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Proto3: Product/Part Edit Search v.10b</h2>
<p>This screen is used to search for existing groups, and their associated products, to edit.

<?php
// User chooses group to edit parts/features/spec tips/to order in. 
require_once('navmenu.php');
require_once('connectvars.php');
require_once('constants.php');

// Connect to the database 
$dbc = mysqli_connect($host, $user, $password, $database);

$errcnt=0;
if (isset($_POST['cmdEdit'])) //Try to save items first.
    {
    //First get GID based on cbxGroupname.
    $s=$_POST['cbxGroupname'];  
    $arr=explode('#',$s);
    $gname=$arr[0]; //Group name
    $basemodel=$arr[1];
    $query="SELECT grid, groupname FROM hgroup WHERE (groupname='".$gname."') AND ".
    "(basemodel='".$basemodel."');"; 

    if (!$result=mysqli_query($dbc,$query))
        {
        $msg=mysql_error();
        echo '<p class="error">'.$msg.'<br/>'.$query.'</p>';
        die();
        }
    $i=mysqli_num_rows($result);
    if ($i>1)
        {
        $msg='<p class="error">ERROR: There was more than one group selected. Cannot save data.<br/>'.$query.'</p>';
        die($msg);
        }

    $row = mysqli_fetch_array($result);
    $grid=trim($row['grid']); //Group id. Stored on other tables also.
    $_SESSION['grid']=$grid;
    
    $home='http://'.$SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/editpart.php';
    header('Location: '.$home);
} //if isset($_POST['submit']
?>

<!-------------------------------------------------------->
<form action="<?php echo $_SERVER['PHP_SELF'].'?'.SID; ?>" method="post">

<hr/>
<label for="cbxGroupname">Group name</label>
<select name="cbxGroupname">
<?php
$query="SELECT grid, groupname, basemodel FROM hgroup ORDER BY groupname;";
if (!$result=mysqli_query($dbc,$query))
    {
    $msg=mysql_error();
    echo '</select><p class="error">'.$msg.'<br/>'.$query.'</p>';
    die();
    }
$i=mysqli_num_rows($result);
if ($i<1)
    {
    $msg='</select><p class="error">ERROR: No groups selected: '.$query.'</p>';
    die($msg);
    }

//Create combo box showing all group names. 
$done=0;
while ($row = mysqli_fetch_array($result)) 
    {
    $v=$row['groupname'].'#'.$row['basemodel'];
    $s='<option value="'.$v.'"';
    if ($done==0)
        {
        //$s.=' selected'; //Select only first one.
        //$done=1;
        }
    $s.='>'.$v."\n";
    echo $s;
    } 
?>
</select>

<p>
<p>Commands: <input type="submit" name="cmdEdit" value="Edit">
</form>

<?php
mysqli_close($dbc);
?>
</body>
</html>

Link to comment
Share on other sites

You cannot send a header once you've outputted content to the page:

 

    $home='http://'.$SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/editpart.php';
    header('Location: '.$home);

 

 

Why not process the form before you output any HTML?

Link to comment
Share on other sites

are u getting the error or a warning like this ?  “Cannot send headers; headers already sent ….” , i faced thing problem a lot but by reading and experience i overcome this by several ways

 

 

 

1. Dont not print or echo before this line

2. HTML tags before header() will make problems

3. finally the blank spaces

don’t keep any blank spaces before or after the <?php ?> tags ,

 

I think in huge web projects its better use only a one header in a function and re use it bu passing the $url to the function this will minimize the code debugging time  . :D

Link to comment
Share on other sites

So how do I jump to another page (load a php file) after I have sent text to the browser? That's what I need to do.

 

I need to store the "grid" variable in the $_SESSION variable before jumping to another page. How do I do that?

 

Thanks.

 

Link to comment
Share on other sites

So how do I jump to another page (load a php file) after I have sent text to the browser? That's what I need to do.

 

I need to store the "grid" variable in the $_SESSION variable before jumping to another page. How do I do that?

 

No that is NOT what you need to do. You need to look at the problem logically and proceed accordingly. You cannot submit a header() request after you have sent data to the browser. So, just do as has already been explained. Process the data first THEN either send to another page or output content from the current script.

Link to comment
Share on other sites

Here is your code modified to have the logic all at the top of the page and only output data to the browser if it should. There may be some typos as I have not tested this.

 

<?php
  
session_start(); 
  
// User chooses group to edit parts/features/spec tips/to order in. 
require_once('navmenu.php');
require_once('connectvars.php');
require_once('constants.php');

// Connect to the database 
$dbc = mysqli_connect($host, $user, $password, $database);

$errcnt=0;
if (isset($_POST['cmdEdit'])) //Try to save items first.
{
    $error_msg = '';
    
    //First get GID based on cbxGroupname.
    $s=$_POST['cbxGroupname'];  
    $arr=explode('#',$s);
    $gname=$arr[0]; //Group name
    $basemodel=$arr[1];
    $query="SELECT grid, groupname
            FROM hgroup
            WHERE groupname='{$gname}'
              AND basemodel='{$basemodel}';"; 
    $result=mysqli_query($dbc, $query);

    if (!$result)
    {
        $error_msg = mysql_error().'<br/>'.$query;
    }
    else if (mysqli_num_rows($result)>1)
    {
        $error_msg = "ERROR: There was more than one group selected. Cannot save data.<br/>{$query}";
    }
    else
    {
        $row = mysqli_fetch_array($result);
        $grid=trim($row['grid']); //Group id. Stored on other tables also.
        $_SESSION['grid']=$grid;
        
        $home='http://'.$SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/editpart.php';
        header('Location: '.$home);
        exit();
    }

} //if isset($_POST['submit']

$error_msg2 = '';
$query = "SELECT grid, groupname, basemodel
          FROM hgroup
          ORDER BY groupname;";
$result = mysqli_query($dbc,$query);

if (!$result)
{
    $error_msg2 = mysql_error().'<br/>'.$query;
}
elseif (mysqli_num_rows($result)<1)
{
    $error_msg2 = "ERROR: No groups selected: {$query}";
}
else
{
    //Create combo box options showing all group names. 
    $options = '';
    $selected = ' selected="selected"';
    while ($row = mysqli_fetch_array($result)) 
    {
        $value = $row['groupname'].'#'.$row['basemodel'];
        $options .= "<option value=\"{$value}\"{$selected}>{$value}</option>\n";
        $selected = '';
    } 
}

mysqli_close($dbc);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Part Search Screen</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<p class="error"><?php echo $error_msg; ?></p>
<h2>Proto3: Product/Part Edit Search v.10b</h2>
<p>This screen is used to search for existing groups, and their associated products, to edit.

<!-------------------------------------------------------->
<form action="<?php echo $_SERVER['PHP_SELF'].'?'.SID; ?>" method="post">

<hr/>
<label for="cbxGroupname">Group name</label>
<select name="cbxGroupname">
<?php echo $options; ?>
</select>
<p class="error"><?php echo $error_msg2; ?></p>

<p>Commands: <input type="submit" name="cmdEdit" value="Edit"></p>
</form>

</body>
</html>

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.