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] = "Card Number has not been especified.";
}
if (empty($holder_name))
{
print($holder_name);
$arrErrores[1] = "Card owner's name has not been especified.";
}
if (empty($credit_limit))
{
print($credit_limit);
$arrErrores[2] = "Credit limit for this card has not been especified.";
}
if (empty($expiration_date))
{
print($expiration_date);
$arrErrores[3] = "Expiration limit for this card has not been especified.";
}
//return $arrErrores;
//show the errors
print("<table>\n"
for($i=0;$i<4;$i++)
{
if (empty($arrErrores[$i])){
//Add a new row
print("<tr><td>$i</td>\n<td>$arrErrores</td></tr>"
}
}
//print the button to return to the previous page
print("<tr><td colspan='2' align='right'><a href=javascript:history.back();>Return to edit information</a></tr>"
print("<table>\n"
}
//Establishing connection with database
$cnx = mysql_connect("localhost","root","lhotka10" or die("Unable to conenct to database"
//Selecting the database
mysql_select_db("expenses",$cnx);
if (!$submitted)
{
?>
<!--print the table border and header -->
<!--outer table: used just to give the colored border -->
<table width="75%" cellspacing="2" cellpadding="2" border="0" bgcolor="#C0C0C0">
<tr>
<td>
<table width="100%" cellspacing="2" cellpadding="2" border="0" bgcolor = "#FFFFFF">
<tr ><!-- Title Row -->
<td bgcolor="#D9D51A" colspan="4">
<font face="Arial" size="+2">Credit Cards's Edition</font>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- Start of FORM -->
<form method="POST" action="<?php echo $PHP_SELF;?>">
<table width="75%" cellspacing="2" cellpadding="2" border="-2">
<tr><!-- Row 1 -->
<td><font face="Arial" size="-1"><b>Card Number</b></font></td>
<td> </td>
<td> </td>
</tr>
<tr><!-- Row 2 -->
<td colspan="2"><input type="text" name="card_number" size="16" value=""></td>
</tr>
<tr><!-- Row 3 -->
<td><font face="Arial" size="-1"><b>Holder's Name</b></font></td>
<td> </td>
<td><font face="Arial" size="-1"><b>Tipo</b></font></td>
</tr>
<tr><!-- Row 4 -->
<td colspan="2"><input type="text" name="holder_name" size="80" value=""></td>
<td>
<select name="card_type" size="1">
<?php
//Establishing connection with database
//$cnx = mysql_connect("localhost","root","lhotka10" or die("Unable to conenct to database"
//Selecting the database
// mysql_select_db("expenses",$cnx);
//Load the different types of credit cards
$sSQL = "SELECT TypeID, descrip FROM cardtype";
$resData = mysql_query($sSQL,$cnx);
if (mysql_num_rows($resData)>0)
{
while ($tuples = mysql_fetch_array($resData))
{
printf("<option>%02d - %s</option>",$tuples[0],$tuples[1]);
//printf("<option>1</option>"
}
}
else
printf("there is nothing to loop through"
?>
</select>
</td>
</tr>
<tr><!-- Row 5 -->
<td><font face="Arial" size="-1"><b>Limit</b></font></td>
<td><font face="Arial" size="-1"><b>Expires On</b></font></td>
<td><font face="Arial" size="-1"><b>Current Balance</b></font></td>
</tr>
<tr><!-- Row 6 -->
<td><input type="text" name="credit_limit" size="23" value=""></td>
<td><input type="text" name="expiration_date" size="23" value=""></td>
<td><input type="text" name="balance" size="23" value=""></td>
</tr>
<tr><!-- Row 7 -->
<td> </td>
<td> </td>
<td align="left"><input type="submit" name="submitted"
value=" Update " ></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] = "Card Number has not been especified.";
}
if (empty($holder_name))
{
$errorCounter++;
$arrErrores[$errorCounter] = "Card owner's name has not been especified.";
}
if (empty($credit_limit))
{
$errorCounter++;
$arrErrores[$errorCounter] = "Credit limit for this card has not been especified.";
}
if (empty($expiration_date))
{
$errorCounter++;
$arrErrores[$errorCounter] = "Expiration limit for this card has not been especified.";
}
//return $arrErrores;
//show the errors
if ($errorCounter)
{
print("<font face='Arial' size='-1'><b>Errors</b></font>\n"
print("<table>\n"
for($i=1;$i<=$errorCounter;$i++)
{
//Add a new row
print("<tr><td><font face='Arial' size='-1'>".$i."</font></td>".
"<td><font face='Arial' size='-1'>".$arrErrores[$i]."</font></td></tr>"
}
//print the button to return to the previous page
print("<tr><td colspan='2' align='right'><a href=javascript:history.back();>Return to edit information</a></tr>"
print("</table>\n"
}
else
{
//Get credit card type
//Store the information in the database
$sSQL = "INSERT INTO creditcard(TypeId, CardNo, CreditLimit, ExpiresOn, Balance,HolderName) " .
"VALUES (". substr($card_type,1,2).",'$card_number',$credit_limit,'$expiration_date',$balance,'$holder_name')";
mysql_query($sSQL,$cnx);
if (mysql_affected_rows($cnx)>=1)
{
/*
print("<SCRIPT LANGUAGE='JavaScript'>\n"
print("window.location.href= print("window.location.reload()"
print("</SCRIPT>\n"
print("</body>\n"
//a href= javascript:history.back();>Return to edit information</a>
*/
header("Location: exit();
}
else
{
print($sSQL);
print("La informacion no pudo ser almacenada"
}
}
}
?>
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] = "Card Number has not been especified.";
}
if (empty($holder_name))
{
print($holder_name);
$arrErrores[1] = "Card owner's name has not been especified.";
}
if (empty($credit_limit))
{
print($credit_limit);
$arrErrores[2] = "Credit limit for this card has not been especified.";
}
if (empty($expiration_date))
{
print($expiration_date);
$arrErrores[3] = "Expiration limit for this card has not been especified.";
}
//return $arrErrores;
//show the errors
print("<table>\n"
for($i=0;$i<4;$i++)
{
if (empty($arrErrores[$i])){
//Add a new row
print("<tr><td>$i</td>\n<td>$arrErrores</td></tr>"
}
}
//print the button to return to the previous page
print("<tr><td colspan='2' align='right'><a href=javascript:history.back();>Return to edit information</a></tr>"
print("<table>\n"
}
//Establishing connection with database
$cnx = mysql_connect("localhost","root","lhotka10" or die("Unable to conenct to database"
//Selecting the database
mysql_select_db("expenses",$cnx);
if (!$submitted)
{
?>
<!--print the table border and header -->
<!--outer table: used just to give the colored border -->
<table width="75%" cellspacing="2" cellpadding="2" border="0" bgcolor="#C0C0C0">
<tr>
<td>
<table width="100%" cellspacing="2" cellpadding="2" border="0" bgcolor = "#FFFFFF">
<tr ><!-- Title Row -->
<td bgcolor="#D9D51A" colspan="4">
<font face="Arial" size="+2">Credit Cards's Edition</font>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- Start of FORM -->
<form method="POST" action="<?php echo $PHP_SELF;?>">
<table width="75%" cellspacing="2" cellpadding="2" border="-2">
<tr><!-- Row 1 -->
<td><font face="Arial" size="-1"><b>Card Number</b></font></td>
<td> </td>
<td> </td>
</tr>
<tr><!-- Row 2 -->
<td colspan="2"><input type="text" name="card_number" size="16" value=""></td>
</tr>
<tr><!-- Row 3 -->
<td><font face="Arial" size="-1"><b>Holder's Name</b></font></td>
<td> </td>
<td><font face="Arial" size="-1"><b>Tipo</b></font></td>
</tr>
<tr><!-- Row 4 -->
<td colspan="2"><input type="text" name="holder_name" size="80" value=""></td>
<td>
<select name="card_type" size="1">
<?php
//Establishing connection with database
//$cnx = mysql_connect("localhost","root","lhotka10" or die("Unable to conenct to database"
//Selecting the database
// mysql_select_db("expenses",$cnx);
//Load the different types of credit cards
$sSQL = "SELECT TypeID, descrip FROM cardtype";
$resData = mysql_query($sSQL,$cnx);
if (mysql_num_rows($resData)>0)
{
while ($tuples = mysql_fetch_array($resData))
{
printf("<option>%02d - %s</option>",$tuples[0],$tuples[1]);
//printf("<option>1</option>"
}
}
else
printf("there is nothing to loop through"
?>
</select>
</td>
</tr>
<tr><!-- Row 5 -->
<td><font face="Arial" size="-1"><b>Limit</b></font></td>
<td><font face="Arial" size="-1"><b>Expires On</b></font></td>
<td><font face="Arial" size="-1"><b>Current Balance</b></font></td>
</tr>
<tr><!-- Row 6 -->
<td><input type="text" name="credit_limit" size="23" value=""></td>
<td><input type="text" name="expiration_date" size="23" value=""></td>
<td><input type="text" name="balance" size="23" value=""></td>
</tr>
<tr><!-- Row 7 -->
<td> </td>
<td> </td>
<td align="left"><input type="submit" name="submitted"
value=" Update " ></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] = "Card Number has not been especified.";
}
if (empty($holder_name))
{
$errorCounter++;
$arrErrores[$errorCounter] = "Card owner's name has not been especified.";
}
if (empty($credit_limit))
{
$errorCounter++;
$arrErrores[$errorCounter] = "Credit limit for this card has not been especified.";
}
if (empty($expiration_date))
{
$errorCounter++;
$arrErrores[$errorCounter] = "Expiration limit for this card has not been especified.";
}
//return $arrErrores;
//show the errors
if ($errorCounter)
{
print("<font face='Arial' size='-1'><b>Errors</b></font>\n"
print("<table>\n"
for($i=1;$i<=$errorCounter;$i++)
{
//Add a new row
print("<tr><td><font face='Arial' size='-1'>".$i."</font></td>".
"<td><font face='Arial' size='-1'>".$arrErrores[$i]."</font></td></tr>"
}
//print the button to return to the previous page
print("<tr><td colspan='2' align='right'><a href=javascript:history.back();>Return to edit information</a></tr>"
print("</table>\n"
}
else
{
//Get credit card type
//Store the information in the database
$sSQL = "INSERT INTO creditcard(TypeId, CardNo, CreditLimit, ExpiresOn, Balance,HolderName) " .
"VALUES (". substr($card_type,1,2).",'$card_number',$credit_limit,'$expiration_date',$balance,'$holder_name')";
mysql_query($sSQL,$cnx);
if (mysql_affected_rows($cnx)>=1)
{
/*
print("<SCRIPT LANGUAGE='JavaScript'>\n"
print("window.location.href= print("window.location.reload()"
print("</SCRIPT>\n"
print("</body>\n"
//a href= javascript:history.back();>Return to edit information</a>
*/
header("Location: exit();
}
else
{
print($sSQL);
print("La informacion no pudo ser almacenada"
}
}
}
?>