Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

header() function

Status
Not open for further replies.

gavalmart

Programmer
Jan 25, 2001
2
SV
Hi, I'm having problems making this function works.
If a wrote a little php file with just this function within it, it works well, the browser is redirected to the file I have especified in its unique argument.

However, when the code is more comples the browser gets no redirected, however, all the rest of the code is executed (in my particular case, a new record is added to my database's table).

This is the code (the problematic lines are just right at the end) :

<html>
<head>
<title></title>
</head>
<body>


<?php
//Defining the function to validate if the user has not written requirede information
function ValidateEntries(){
$arrErrores = array();

//Validate credit card number
if (empty($card_number))
{
print($card_number);
$arrErrores[0] = &quot;Card Number has not been especified.&quot;;
}
if (empty($holder_name))
{
print($holder_name);
$arrErrores[1] = &quot;Card owner's name has not been especified.&quot;;
}
if (empty($credit_limit))
{
print($credit_limit);
$arrErrores[2] = &quot;Credit limit for this card has not been especified.&quot;;
}
if (empty($expiration_date))
{
print($expiration_date);
$arrErrores[3] = &quot;Expiration limit for this card has not been especified.&quot;;
}
//return $arrErrores;

//show the errors
print(&quot;<table>\n&quot;);
for($i=0;$i<4;$i++)
{
if (empty($arrErrores[$i])){
//Add a new row
print(&quot;<tr><td>$i</td>\n<td>$arrErrores</td></tr>&quot;);
}
}
//print the button to return to the previous page
print(&quot;<tr><td colspan='2' align='right'><a href=javascript:history.back();>Return to edit information</a></tr>&quot;);
print(&quot;<table>\n&quot;);
}

//Establishing connection with database
$cnx = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;lhotka10&quot;) or die(&quot;Unable to conenct to database&quot;);

//Selecting the database
mysql_select_db(&quot;expenses&quot;,$cnx);

if (!$submitted)
{

?>

<!--print the table border and header -->
<!--outer table: used just to give the colored border -->
<table width=&quot;75%&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;0&quot; bgcolor=&quot;#C0C0C0&quot;>
<tr>
<td>
<table width=&quot;100%&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;0&quot; bgcolor = &quot;#FFFFFF&quot;>
<tr ><!-- Title Row -->
<td bgcolor=&quot;#D9D51A&quot; colspan=&quot;4&quot;>
<font face=&quot;Arial&quot; size=&quot;+2&quot;>Credit Cards's Edition</font>
</td>
</tr>
</table>
</td>
</tr>
</table>

<!-- Start of FORM -->
<form method=&quot;POST&quot; action=&quot;<?php echo $PHP_SELF;?>&quot;>
<table width=&quot;75%&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;-2&quot;>
<tr><!-- Row 1 -->
<td><font face=&quot;Arial&quot; size=&quot;-1&quot;><b>Card Number</b></font></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><!-- Row 2 -->
<td colspan=&quot;2&quot;><input type=&quot;text&quot; name=&quot;card_number&quot; size=&quot;16&quot; value=&quot;&quot;></td>
</tr>
<tr><!-- Row 3 -->
<td><font face=&quot;Arial&quot; size=&quot;-1&quot;><b>Holder's Name</b></font></td>
<td>&nbsp;</td>
<td><font face=&quot;Arial&quot; size=&quot;-1&quot;><b>Tipo</b></font></td>
</tr>
<tr><!-- Row 4 -->
<td colspan=&quot;2&quot;><input type=&quot;text&quot; name=&quot;holder_name&quot; size=&quot;80&quot; value=&quot;&quot;></td>
<td>
<select name=&quot;card_type&quot; size=&quot;1&quot;>
<?php
//Establishing connection with database
//$cnx = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;lhotka10&quot;) or die(&quot;Unable to conenct to database&quot;);

//Selecting the database
// mysql_select_db(&quot;expenses&quot;,$cnx);
//Load the different types of credit cards
$sSQL = &quot;SELECT TypeID, descrip FROM cardtype&quot;;
$resData = mysql_query($sSQL,$cnx);

if (mysql_num_rows($resData)>0)
{
while ($tuples = mysql_fetch_array($resData))
{
printf(&quot;<option>%02d - %s</option>&quot;,$tuples[0],$tuples[1]);
//printf(&quot;<option>1</option>&quot;);
}
}
else
printf(&quot;there is nothing to loop through&quot;);

?>
</select>
</td>
</tr>
<tr><!-- Row 5 -->
<td><font face=&quot;Arial&quot; size=&quot;-1&quot;><b>Limit</b></font></td>
<td><font face=&quot;Arial&quot; size=&quot;-1&quot;><b>Expires On</b></font></td>
<td><font face=&quot;Arial&quot; size=&quot;-1&quot;><b>Current Balance</b></font></td>
</tr>
<tr><!-- Row 6 -->
<td><input type=&quot;text&quot; name=&quot;credit_limit&quot; size=&quot;23&quot; value=&quot;&quot;></td>
<td><input type=&quot;text&quot; name=&quot;expiration_date&quot; size=&quot;23&quot; value=&quot;&quot;></td>
<td><input type=&quot;text&quot; name=&quot;balance&quot; size=&quot;23&quot; value=&quot;&quot;></td>
</tr>
<tr><!-- Row 7 -->
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align=&quot;left&quot;><input type=&quot;submit&quot; name=&quot;submitted&quot;
value=&quot; Update &quot; ></td>
</tr>

</table>
</form>
<!-- End of FORM -->

</body>
</html>

<?php
}
else
{
//Validate required entries
$errorCounter = 0;
$arrErrores = array();

//Validate credit card number
if (empty($card_number))
{
$errorCounter++;
$arrErrores[$errorCounter] = &quot;Card Number has not been especified.&quot;;
}

if (empty($holder_name))
{
$errorCounter++;
$arrErrores[$errorCounter] = &quot;Card owner's name has not been especified.&quot;;
}

if (empty($credit_limit))
{
$errorCounter++;
$arrErrores[$errorCounter] = &quot;Credit limit for this card has not been especified.&quot;;
}

if (empty($expiration_date))
{
$errorCounter++;
$arrErrores[$errorCounter] = &quot;Expiration limit for this card has not been especified.&quot;;
}
//return $arrErrores;

//show the errors


if ($errorCounter)
{
print(&quot;<font face='Arial' size='-1'><b>Errors</b></font>\n&quot;);
print(&quot;<table>\n&quot;);
for($i=1;$i<=$errorCounter;$i++)
{
//Add a new row
print(&quot;<tr><td><font face='Arial' size='-1'>&quot;.$i.&quot;</font></td>&quot;.
&quot;<td><font face='Arial' size='-1'>&quot;.$arrErrores[$i].&quot;</font></td></tr>&quot;);
}
//print the button to return to the previous page
print(&quot;<tr><td colspan='2' align='right'><a href=javascript:history.back();>Return to edit information</a></tr>&quot;);
print(&quot;</table>\n&quot;);
}
else
{
//Get credit card type
//Store the information in the database


$sSQL = &quot;INSERT INTO creditcard(TypeId, CardNo, CreditLimit, ExpiresOn, Balance,HolderName) &quot; .
&quot;VALUES (&quot;. substr($card_type,1,2).&quot;,'$card_number',$credit_limit,'$expiration_date',$balance,'$holder_name')&quot;;

mysql_query($sSQL,$cnx);

if (mysql_affected_rows($cnx)>=1)
{
/*
print(&quot;<SCRIPT LANGUAGE='JavaScript'>\n&quot;);
print(&quot;window.location.href= print(&quot;window.location.reload()&quot;);
print(&quot;</SCRIPT>\n&quot;);
print(&quot;</body>\n&quot;);
//a href= javascript:history.back();>Return to edit information</a>
*/
header(&quot;Location: exit();
}
else
{
print($sSQL);
print(&quot;La informacion no pudo ser almacenada&quot;);
}

}

}
?>
 
OK, you've taken lots of info, processed it and echo'd / printed it to the screen and then youwant to send header requests. Too late, headers are the very first thing you send or they don't work.

The error you should see is
Header information already sent.
Just about the only thing you can do beofre headers is cookies and you must have no blank lines between your top of the page <? and the header();
---------------top of page------------
<?
header();
everything else ***************************************
Party on, dudes!
[cannon]
 
KarveR, as you can see this is mixed php/html code and is a self submit form, so the first big chunck of code will be executed if the user has not clicked on the submit button, however when he/she decide to submit it the code that gets executed is just a mysql_query and the call to the header function. But it doesn't work anyway.
 
I'll reaffirm the bit about NO BLANK LINES.
}else{
//code
code
code
code
header()

if you have a blank line and you are screwed. eg
}else{
//notes
code
<this blank line will send headers
code
header() ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top