Page not redirecting; fails silently

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BFarley65
Forum Newbie
Posts: 4
Joined: Sat Oct 02, 2010 3:00 pm

Page not redirecting; fails silently

Post by BFarley65 »

I have a form that submits to itself, validates fields, sets session variables (if validation is passed) and then is supposed to go to the next page.

However, it runs through all the code, without ever redirecting.

I could understand if I got an error message regarding header already being written, but that's not the case.

When the code below is executed, the line "should have redirected" prints to the screen.

By experience I'm a .NET programmer, and I would not be surprised if there's just something small I'm overlooking.

Ideas? I'd appreciate any suggestions.

Code: Select all

<?php 
	// Define session 
	include('phpinclude/session.php'); 
	
	// Set initial validation flag value 
	$allvalid = 1;

	// Set empty validation messages
	$stockmessage = "";
	$widthmessage = "";
	$lengthmessage = "";
	
	// Set empty field values
	$StockNumber = "";
	$WidthMinFeet = "";
	$WidthMinInches = "";
	$WidthMaxFeet = "";
	$WidthMaxInches = "";
	$LengthMinFeet = "";
	$LengthMinInches = "";
	$LengthMaxFeet = "";
	$LengthMaxInches = "";
	$IdDesign = 0;
	$IdBackgroundColor = 0;
	$IdBorderColor = 0;

	// Has search been initiated? 
	if(isset($_POST['submit']))
		{
		// Variables from search form 
		$StockNumber = $_POST['txtStockNumber'];
		$WidthMinFeet = $_POST['txtWidthMinFeet'];
		$WidthMinInches = $_POST['txtWidthMinInches'];
		$WidthMaxFeet = $_POST['txtWidthMaxFeet'];
		$WidthMaxInches = $_POST['txtWidthMaxInches'];
		$LengthMinFeet = $_POST['txtLengthMinFeet'];
		$LengthMinInches = $_POST['txtLengthMinInches'];
		$LengthMaxFeet = $_POST['txtLengthMaxFeet'];
		$LengthMaxInches = $_POST['txtLengthMaxInches'];
		$IdDesign = $_POST['ddlDesign'];
		$IdBackgroundColor = $_POST['ddlBackgroundColor'];
		$IdBorderColor = $_POST['ddlBorderColor'];

		
		// Check whether stock number is blank, numeric, or six characters
		if(!empty($StockNumber))
			{
			if(!is_numeric($StockNumber))
				{
				$stockmessage = "Only numeric values can appear in the stock number field";
				$allvalid = 0;				
				}
			else
				{
				if(!strlen($StockNumber)==6)
					{
					$stockmessage = "The stock number must be six characters in length";
					$allvalid = 0;						
					}
				}
			}
		
		// Check whether Width Min Feet is blank or numeric
		if(!empty($WidthMinFeet))
			{
			if(!is_numeric($WidthMinFeet))
				{
				$widthmessage = "Only numeric values can appear in width measurement fields";
				$allvalid = 0;				
				}
			}
			
		// Check whether Width Min Inches is blank or numeric
		if(!empty($WidthMinInches))
			{
			if(!is_numeric($WidthMinInches))
				{
				$widthmessage = "Only numeric values can appear in width measurement fields";
				$allvalid = 0;				
				}
			else
				{
				if($WidthMinInches> 11)
					{
					$widthmessage = "Invalid value in width measurement inches";
					$allvalid = 0;					
					}
				}
			}
		
		// Check whether Width Max Feet is blank or numeric
		if(!empty($WidthMaxFeet))
			{
			if(!is_numeric($WidthMaxFeet))
				{
				$widthmessage = "Only numeric values can appear in width measurement fields";
				$allvalid = 0;				
				}
			}
			
		// Check whether Width Max Inches is blank or numeric
		if(!empty($WidthMaxInches))
			{
			if(!is_numeric($WidthMaxInches))
				{
				$widthmessage = "Only numeric values can appear in width measurement fields";
				$allvalid = 0;				
				}
			else
				{
				if($WidthMaxInches> 11)
					{
					$widthmessage = "Invalid value in width measurement inches";
					$allvalid = 0;					
					}
				}
			}
			
		// Check whether Length Min Feet is blank or numeric
		if(!empty($LengthMinFeet))
			{
			if(!is_numeric($LengthMinFeet))
				{
				$lengthmessage = "Only numeric values can appear in length measurement fields";
				$allvalid = 0;				
				}
			}
			
		// Check whether Length Min Inches is blank or numeric
		if(!empty($LengthMinInches))
			{
			if(!is_numeric($LengthMinInches))
				{
				$lengthmessage = "Only numeric values can appear in length measurement fields";
				$allvalid = 0;				
				}
			else
				{
				if($LengthMaxInches > 11)
					{
					$lengthmessage = "Invalid value in length measurement inches";
					$allvalid = 0;					
					}
				}
			}
		
		// Check whether Length Max Feet is blank or numeric
		if(!empty($LengthMaxFeet))
			{
			if(!is_numeric($LengthMaxFeet))
				{
				$lengthmessage = "Only numeric values can appear in length measurement fields";
				$allvalid = 0;				
				}
			}
			
		// Check whether Length Max Inches is blank or numeric
		if(!empty($LengthMaxInches))
			{
			if(!is_numeric($LengthMaxInches))
				{
				$lengthmessage = "Only numeric values can appear in length measurement fields";
				$allvalid = 0;				
				}
			else
				{
				if($LengthMaxInches > 11)
					{
					$lengthmessage = "Invalid value in length measurement inches";
					$allvalid = 0;					
					}
				}
			}
		
		// Did validation pass?
		if($allvalid == 1)
			{
			// Set session values
			$_SESSION['StockNumber'] = $StockNumber;
			$_SESSION['WidthMinFeet'] = $WidthMinFeet;
			$_SESSION['WidthMinInches'] = $WidthMinInches;
			$_SESSION['WidthMaxFeet'] = $WidthMaxFeet;
			$_SESSION['WidthMaxInches'] = $WidthMaxInches;
			$_SESSION['LengthMinFeet'] = $LengthMinFeet;
			$_SESSION['LengthMinInches'] = $LengthMinInches;
			$_SESSION['LengthMaxFeet'] = $LengthMaxFeet;
			$_SESSION['LengthMaxInches'] = $LengthMaxInches;
			$_SESSION['IdDesign'] = $IdDesign;
			$_SESSION['IdBackgroundColor'] = $IdBackgroundColor;
			$_SESSION['IdBorderColor'] = $IdBorderColor;
			
			// Go to results page
			header('Location: results.php');	
			
			echo "should have redirected";
			}
		}
	else
		{
		// Make database connection 
		include('phpinclude/conn.php'); 
		
		// Get designs 
		$qdesign = "SELECT IdDesign, Design from maloumiandb.designs ORDER BY Design";
		$rdesign = mysql_query($qdesign, $conn);
		
		if (!$rdesign) 
			{
		    die('Could not retrieve designs: ' . mysql_error());
			}
		
		// Get background colors 
		$qbgcolor = "SELECT IdBackgroundColor, BackgroundColor from maloumiandb.backgroundcolors ORDER BY BackgroundColor";
		$rbgcolor = mysql_query($qbgcolor, $conn);
		
		if (!$rbgcolor) 
			{
		    die('Could not retrieve background colors: ' . mysql_error());
			}
		
		// Get border colors 
		$qbocolor = "SELECT IdBorderColor, BorderColor from maloumiandb.bordercolors ORDER BY BorderColor";
		$rbocolor = mysql_query($qbocolor, $conn);
		
		if (!$rbocolor ) 
			{
		    die('Could not retrieve border colors: ' . mysql_error());
			}
		}
 ?>
<!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">

<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<title>Search</title>
</head>

<body>

	<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
		<table cellpadding="5">
			<tr valign="middle">
				<th colspan="2" align="left">
					Width
				</th>
			</tr>
			<tr valign="middle">
				<td align="right">
					Minimum
				</td>
				<td>
					<label id="lblWidthMinFeet" for="txtWidthMinFeet">Feet</label>
					<input name="txtWidthMinFeet" type="text" size="2" tabindex="1" value='<?php echo $WidthMinFeet; ?>' />
					<label id="lblWidthMinInches" for="txtWidthMinInches">Inches</label>
					<input name="txtWidthMinInches" type="text" size="2" tabindex="2" value="<?php echo $WidthMinInches; ?>" />
				</td>
			</tr>
			<tr valign="middle">
				<td align="right">
					Maximum
				</td>
				<td>
					<label id="lblWidthMaxFeet" for="txtWidthMaxFeet">Feet</label>
					<input name="txtWidthMaxFeet" type="text" size="2" tabindex="3" value="<?php echo $WidthMaxFeet; ?>" />
					<label id="lblWidthMaxInches" for="txtWidthMaxInches">Inches</label>
					<input name="txtWidthMaxInches" type="text" size="2" tabindex="4" value="<?php echo $WidthMaxInches; ?>" />
				</td>
			</tr>
			<tr>
				<td colspan="2" style="color:red">
					<?php 
						echo $widthmessage; 
					?>
				</td>
			</tr>
			<tr valign="middle">
				<th colspan="2" align="left">Length</th>
			</tr>
			
			<tr valign="middle">
				<td align="right">
					Minimum
				</td>
				<td>
					<label id="lblLengthMinFeet" for="txtLengthMinFeet">Feet</label>
					<input name="txtLengthMinFeet" type="text" size="2" tabindex="5" value="<?php echo $LengthMinFeet; ?>" />
					<label id="lblLengthMinInches" for="txtLengthMinInches">Inches</label>
					<input name="txtLengthMinInches" type="text" size="2" tabindex="6" value="<?php echo $LengthMinInches; ?>" />
				</td>
			</tr>
			<tr valign="middle">
				<td align="right">
					Maximum
				</td>
				<td>
					<label id="lblLengthMaxFeet" for="txtLengthMaxFeet">Feet</label>
					<input name="txtLengthMaxFeet" type="text" size="2" tabindex="7" value="<?php echo $LengthMaxFeet; ?>" />
					<label id="lblLengthMaxInches" for="txtLengthMaxInches">Inches</label>
					<input name="txtLengthMaxInches" type="text" size="2" tabindex="8" value="<?php echo $LengthMaxInches; ?>" />
				</td>
			</tr>
			<tr>
				<td colspan="2" style="color:red">
					<?php 
						echo $lengthmessage; 
					?>
				</td>
			</tr>
			<tr valign="middle">
				<th align="left">
					Design
				</th>
				<td>
					<select name="ddlDesign" tabindex="9">
					<option value="0">All</option>
					<?php 
						while($row = mysql_fetch_array($rdesign))
						  {
						  echo "<option value=";
						  echo $row['IdDesign'];
						  echo ">";
						  echo $row['Design'];
						  echo "</option>";
						  }					
					?>
					</select>	
				</td>
			</tr>
			<tr valign="middle">
				<th align="left">
					Background Color
				</th>
				<td>
					<select name="ddlBackgroundColor" tabindex="10">
					<option value="0">All</option>
					<?php 
						while($row = mysql_fetch_array($rbgcolor))
						  {
						  echo "<option value=";
						  echo $row['IdBackgroundColor'];
						  echo ">";
						  echo $row['BackgroundColor'];
						  echo "</option>";
						  }					
					?>
					</select>
				</td>
			</tr>
			<tr valign="middle">
				<th align="left">
					Border Color
				</th>
				<td>
					<select name="ddlBorderColor" tabindex="11">
					<option value="0">All</option>
					<?php 
						while($row = mysql_fetch_array($rbocolor))
						  {
						  echo "<option value=";
						  echo $row['IdBorderColor'];
						  echo ">";
						  echo $row['BorderColor'];
						  echo "</option>";
						  }					
					?>
					</select>	
				</td>
			</tr>
			<tr valign="middle">
				<th align="left">
					Stock Number
				</th>
				<td>
					<input name="txtStockNumber" type="text" size="6" tabindex="12" value="<?php echo $StockNumber; ?>" />	
				</td>
			</tr>
			<tr>
				<td colspan="2" style="color:red">
					<?php 
						echo $stockmessage; 
					?>
				</td>
			</tr>
			<tr valign="middle">
				<th align="left">
					&nbsp;
				</th>
				<td>
					<input name="submit" type="submit" value="Search"  />
				</td>
			</tr>
		</table>
	</form>
</body>

</html>
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Page not redirecting; fails silently

Post by DigitalMind »

Check for leading white spaces and show your phpinclude/session.php
BFarley65
Forum Newbie
Posts: 4
Joined: Sat Oct 02, 2010 3:00 pm

Re: Page not redirecting; fails silently

Post by BFarley65 »

DigitalMind wrote:Check for leading white spaces and show your phpinclude/session.php
The session.php include contains a single line:

Code: Select all

session_start(); 
I changed the code to get rid of the include, but still get the same result.

No white spaces are spotted.

Shouldn't I see an explicit error being presented?
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Page not redirecting; fails silently

Post by DigitalMind »

Yup
BFarley65
Forum Newbie
Posts: 4
Joined: Sat Oct 02, 2010 3:00 pm

Re: Page not redirecting; fails silently

Post by BFarley65 »

When the form is posted, it ignores the header line and prints the next.

Code: Select all

                        header('Location: results.php');        
                        
                        echo "should have redirected";
Is there somewhere I can check for errors being logged, since nothing is appearing on the screen?

I have an almost identical page in another project and I never had this issue -- I would see an error regarding header right on the screen.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Page not redirecting; fails silently

Post by DigitalMind »

does it print anything before your "the next"?
BFarley65
Forum Newbie
Posts: 4
Joined: Sat Oct 02, 2010 3:00 pm

Re: Page not redirecting; fails silently

Post by BFarley65 »

Solved.

Dunno how.

Now it suddenly works on the web server.

However ... it still does not work within my coding environment, Microsoft Expression Web.

I have an Expression for it right about now.

Jeez.

Sorry to use your time on this, and I appreciate your time on a Saturday.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Page not redirecting; fails silently

Post by DigitalMind »

no problem
Post Reply