bulrush Posted May 10, 2010 Share Posted May 10, 2010 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> Quote Link to comment Share on other sites More sharing options...
Adam Posted May 10, 2010 Share Posted May 10, 2010 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? Quote Link to comment Share on other sites More sharing options...
Sudantha Posted May 10, 2010 Share Posted May 10, 2010 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 . Quote Link to comment Share on other sites More sharing options...
bulrush Posted May 10, 2010 Author Share Posted May 10, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 10, 2010 Share Posted May 10, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 10, 2010 Share Posted May 10, 2010 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.