whats the general code to redirect to another page?
Moderator: General Moderators
whats the general code to redirect to another page?
Hi,
I ahve a strage problem here. I have a form and its action is specified to a new php page.When I submit the form the ddata is getting inserted into the database.Well all this is doen once my form is submitted to the submit.php page.This submit.php page gets my form data insterted into database.Once inserted, I ahve use header() method to get to my original page.While all this seems to work fine in my own computer, I get error message when I run from remote server. The error message says, "header information all ready sent".So the thing hangs on my "submit.php" page.If I go back by pressing back key of my browser, then refresh the page, I get the updated information from my database.The data is already put into the databse.But the page does not redirect.I am going wrong way? I mean whats the general method to get back to the origianl page after inserting tha data.
I ahve a strage problem here. I have a form and its action is specified to a new php page.When I submit the form the ddata is getting inserted into the database.Well all this is doen once my form is submitted to the submit.php page.This submit.php page gets my form data insterted into database.Once inserted, I ahve use header() method to get to my original page.While all this seems to work fine in my own computer, I get error message when I run from remote server. The error message says, "header information all ready sent".So the thing hangs on my "submit.php" page.If I go back by pressing back key of my browser, then refresh the page, I get the updated information from my database.The data is already put into the databse.But the page does not redirect.I am going wrong way? I mean whats the general method to get back to the origianl page after inserting tha data.
header() is the correct method to redirect. If you're getting 'header information already sent...' errors then there is something wrong with the script, either an error message is shown to do with the database code, or there is a blank space or other character sent on the page prior to the call to header(). Pretty common error, check the PHP manual and search Google or this forum.
[edit]
^ what he said.
[edit]
^ what he said.
Last edited by bdlang on Mon Jun 26, 2006 1:39 am, edited 1 time in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Pimptastic | Please use
thats everything I have in my putData.php file.
any help?
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I guess the same.But I got confused to see, that the script works on my own pc, not from server. Any way here is the script I am usingCode: Select all
<?php
require_once('DbConnector.php');
$MYconnector = new DbConnector();
//Gets the user submitted values
$volume = $_POST['volume'];
$article = $_POST['article'];
$publishDate = $_POST['publishdate'];
$author = $_POST['author'];
$content = $_POST['data'];
//checks whether user has submitted anything
if($volume!="" && $article!="" && $publishDate!="" && $author!="" && $content!=""){
$contentFill_sql= "INSERT INTO `articles` (`volume`,`article`,`publishdate`,`content`,`author`) VALUES('$volume','$article','$publishDate','$content','$author');";
mysql_query($contentFill_sql) or die('error : '.mysql_error());
echo('successfully put');
header( "Location: ../index.php" );
}else{
echo('please fill the form');
header( "Location: ../index.php" );
}
?>any help?
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]In php.ini, if you have output buffering turned on, you can have spaces and echo data before you send a header. On your local server, you may have output buffering turned on, while your remote server has it turned off. Either way, there is a space, linefeed, or data being sent before the header, otherwise you would not get the error you are receiving.
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
Now,I turned off "output buffering" on my machine and the script I am running now is
Now I could not figure out where is the blank space.I have already commented evrything and calling my header function. In my development environment I found nothing at the browser.It comes to this page and hangs.It never goes back to the redirect ( 
Code: Select all
<?php
require_once('DbConnector.php');
$MYconnector = new DbConnector();
//Gets the user submitted values
$volume = $_POST['volume'];
$article = $_POST['article'];
$publishDate = $_POST['publishdate'];
$author = $_POST['author'];
$content = $_POST['data'];
//checks whether user has submitted anything
if($volume!="" && $article!="" && $publishDate!="" && $author!="" && $content!=""){
$contentFill_sql= "INSERT INTO `articles` (`volume`,`article`,`publishdate`,`content`,`author`) VALUES('$volume','$article','$publishDate','$content','$author');";
mysql_query($contentFill_sql);
header( "Location: ../index.php" );
//mysql_query($contentFill_sql) or die('error : '.mysql_error());
//echo('successfully put');
}else{
echo('please fill the form');
header( "Location: ../index.php" );
}
?>-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
are the comments have to be eleiminated !!?Because now I have removed all the white spaces too, but without any effect in my script.This still shows me a blnak screen on submission.No redirection
Then the other file
Code: Select all
<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';
class DbConnector extends SystemComponent {
var $theQuery;
var $link;
//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){
//echo('evaluating DB Connector <br/>');
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
//echo($host.' : '.$db.' : '.$user.' : '.$pass.'<br/>');
// Connect to the database
$this->link = mysql_connect($host, $user, $pass)or die('Could not connect to Database Server'.mysql_error().'<br/>');
mysql_select_db($db) or die('DB not Found'.mysql_error().'<br/>');
register_shutdown_function(array(&$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query) {
//echo('Querying DB<br/>');
$this->theQuery = $query;
return mysql_query($query, $this->link);
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {
return mysql_fetch_array($result);
}
//*** Function: close, Purpose: Close the connection ***
function close() {
//echo('closing connection');
mysql_close($this->link);
}
}
?>Code: Select all
<?php
class SystemComponent {
var $settings;
function getSettings() {
//echo('inside system component getSettings<br/>');
// System variables
//$settings['siteDir'] = '/path/to/your/intranet/';
// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'saumya';
$settings['dbpassword'] = 'saumya123';
$settings['dbname'] = 'universal';
return $settings;
}
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Every page in your app needs to have ABSOLUTELY NO spaces, echos, or output before the call to header(). If anything, and I mean anything, gets sent to the browser before the call to header(), you are going to get that error message.
I would suggest reading the PHP manual on header() to familiarize yourself with the use of the function. Also, I noticed in one of your posts you have this code...
That little echo('please fill the form') will kill your header redirect everytime (unless you use output buffering). But I would seriously try reading the manual, and searching these forums. This is the fifth or sixth 'Headers already sent' post I have responded to within the last week.
PS A blank page is usually a sign of a critical error message thrown by PHP coupled with display_errors being off. If you want to see what PHP is telling you about the error, turn display_errors on.
I would suggest reading the PHP manual on header() to familiarize yourself with the use of the function. Also, I noticed in one of your posts you have this code...
Code: Select all
<?php
{
$contentFill_sql= "INSERT INTO `articles` (`volume`,`article`,`publishdate`,`content`,`author`) VALUES ('$volume','$article','$publishDate','$content','$author');";
mysql_query($contentFill_sql);
header( "Location: ../index.php" );
//mysql_query($contentFill_sql) or die('error : '.mysql_error());
//echo('successfully put');
}else{
echo('please fill the form');
header( "Location: ../index.php" );
}
?>PS A blank page is usually a sign of a critical error message thrown by PHP coupled with display_errors being off. If you want to see what PHP is telling you about the error, turn display_errors on.
Code: Select all
<?php
ini_set('display_errors', TRUE);
?>