Jump to content

[SOLVED] php5 header redirect problem


el-sid

Recommended Posts

hi all, ive been working on this php form/submit script. it takes credentials from a form  and passes then in a session variable for validation. if i put the correct input..it writes the data in the database but does not redirect and instead shows a blank page. if the values are incorrect, it does not redirect to the form with errors. here's the code for the form

<?php
// This script shows the user a customer <form>.
// It can be used both for INSERTing a new customer and
// for UPDATE-ing an existing customer. If the customer 
// is logged in, then it is an UPDATE; otherwise, an 
// INSERT.


include 'include.inc';

set_error_handler("errorHandler");

// Show an error in a red font
function fieldError($fieldName, $errors)
{
    if (isset($errors[$fieldName]))
    echo "<font color=\"red\">" .
    $errors[$fieldName] .
           "</font><br>";
}

// Connect to a session
session_start();

// Initialise $formVars from the $_SESSION["formVars"] (if set)
if (session_is_registered("formVars"))
{
    $row = $_SESSION["formVars"];
    $formVars["surname"] = $row["surname"];
    $formVars["other_names"] = $row["other_names"];
    $formVars["address"] = $row["address"];
    $formVars["phone_no"] = $row["phone_no"];
    $formVars["email"] = $row["email"];
    $formVars["dob"] = $row["dob"];
    $formVars["gender"] = $row["gender"];
    $formVars["date_of_emp"] = $row["date_of_emp"];
    $formVars["level"] = $row["level"];
    $formVars["username"] = $row["username"];
    $formVars["loginPassword"] = $row["loginPassword"];

}

// Is the user logged in and were there no errors from a previous
// validation?  If so, look up the customer for editing
if (session_is_registered("loginUsername") && empty($_SESSION["errors"]))
{
    if (!($connection = @ mysql_pconnect($hostName,
                $username,
                $password)))
    showerror();

    if (!mysql_select_db($databaseName, $connection))
    showerror();

    $salesman_code = getSalesmanID($_SESSION["loginUsername"], $connection);

    $query = "SELECT * FROM salesman
               WHERE salesman_code = '$salesman_code'";

    if (!($result = @ mysql_query($query, $connection)))
    showerror();

    $row = mysql_fetch_array($result);

    // Reset $formVars, since we're loading from
    // the customer table
    $formVars = array();

    // Load all the form variables with customer data
    $formVars["surname"] = $row["surname"];
    $formVars["other_names"] = $row["other_names"];
    $formVars["address"] = $row["address"];
    $formVars["phone_no"] = $row["phone_no"];
    $formVars["email"] = $row["email"];
    $formVars["dob"] = $row["dob"];
    $formVars["gender"] = $row["gender"];
    $formVars["date_of_emp"] = $row["date_of_emp"];
    $formVars["level"] = $row["level"];
    $formVars["username"] = $row["username"];
    $formVars["loginPassword"] = $row["loginPassword"];
}

?>
<!DOCTYPE HTML PUBLIC 
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
    <head><title>Salesman Details</title></head>
    <body bgcolor="white">
        <?php
        // Show the user login status
        showLogin();
        ?>
        <form method="post" action="salesman.validate.php">
            <h3>Salesman Details</h3>
            <?php
            // Display any messages to the user
            showMessage();

            // Show meaningful instructions for UPDATE or INSERT
            if (session_is_registered("loginUsername"))
            echo "<h4>Please amend your details below as required.</h4>";
            else
            echo "<h4>Please fill in the details below to join.</h4>";
            ?>
            <table>
                <col span="1" align="right">


                <tr><td>Surname:</td>
                    <td><?php echo fieldError("surname", $_SESSION["errors"]); ?>
                        <input type="text" name="surname"
                           value="<?php echo $formVars["surname"]; ?>" size=50></td>
                </tr>

                <tr><td>Other Names:</td>
                    <td><?php echo fieldError("other_names", $_SESSION["errors"]); ?>
                        <input type="text" name="other_names"
                           value="<?php echo $formVars["other_names"]; ?>" size=50></td>
                </tr>

                <tr><td>Address:</td>
                    <td><?php echo fieldError("address", $_SESSION["errors"]); ?>
                        <input type="text" name="address"
                           value="<?php echo $formVars["address"]; ?>" size=50></td>
                </tr>

                <tr><td>Phone Number:</td>
                    <td><?php echo fieldError("phone_no", $_SESSION["errors"]); ?>
                        <input type="text" name="phone_no"
                           value="<?php echo $formVars["phone_no"]; ?>" size=30></td>
                </tr>

                <tr><td>Email:</td>
                    <td><?php echo fieldError("email", $_SESSION["errors"]); ?>
                        <input type="text" name="email"
                           value="<?php echo $formVars["email"]; ?>" size=30></td>
                </tr>

                <tr><td>Date of Birth:<br>(YYYY-MM-DD) format</td>
                    <td><?php echo fieldError("dob", $_SESSION["errors"]); ?>
                        <input type="text" name="dob"
                           value="<?php echo $formVars["dob"]; ?>" size=10></td>
                </tr>

                <tr><td>Gender:</td>
                    <td><?php echo fieldError("gender", $_SESSION["errors"]); ?>
                        <input type="text" name="gender"
                           value="<?php echo $formVars["gender"]; ?>" size=20></td>
                </tr>

                <tr><td>Date of Employment <br>(YYYY-MM-DD) format:</td>
                    <td><?php echo fieldError("date_of_emp", $_SESSION["errors"]); ?>
                        <input type="text" name="date_of_emp"
                           value="<?php echo $formVars["date_of_emp"]; ?>" size=20></td>
                </tr>

                <tr><td>Department Level:</td>
                    <td><?php echo fieldError("level", $_SESSION["errors"]); ?>
                        <input type="text" name="level"
                           value="<?php echo $formVars["level"]; ?>" size=20></td>
                </tr>
                <?php
                // Only show the username and password widgets to new users
                if (!session_is_registered("loginUsername"))
                {
                    ?>
                <tr><td>Username:</td>
                    <td><?php echo fieldError("username", $_SESSION["errors"]); ?>
                        <input type="text" name="username"
                           value="<?php echo $formVars["username"]; ?>" size=30></td>
                </tr>

                <tr><td>Password:</td>
                    <td><?php echo fieldError("loginPassword", $_SESSION["errors"]); ?>
                        <input type="password" name="loginPassword"
                           value="<?php echo $formVars["loginPassword"]; ?>" size=15></td>
                </tr>
                <?php
            }
            ?>
                <tr>
                    <td><input type="submit" value="Submit"></td>
                </tr>
            </table>
        </form>
        <br><a href="http://validator.w3.org/check/referer"><img
                src="http://www.w3.org/Icons/valid-html401" height="31" width="88"
            align="right" border="0" alt="Valid HTML 4.01!"></a>
    </body>
</html>


 

here is the validate code in a seperate php file

<?php
// This script validates customer data entered into
// example.customer.2.php.
// If validation succeeds, it INSERTs or UPDATEs
// a customer and redirect to a receipt page; if it 
// fails, it creates error messages and these are later 
// displayed by example.customer.2.php.

include 'include.inc';

set_error_handler("errorHandler");

// Initialize a session
session_start();

// Register an error array - just in case!
if (!session_is_registered("errors"))
session_register("errors");

// Clear any errors that might have been 
// found previously
$errors = array();

// Set up a $formVars array with the POST variables
// and register with the session.
if (!session_is_registered("formVars"))
session_register("formVars");

foreach($_POST as $varname => $value)
$formVars[$varname] = trim(clean($value, 50));

$_SESSION["formVars"] = $formVars;

// Validate the surname
if (empty($formVars["surname"])) 
// surname cannot be a null string
$errors["surname"] = 
          "You must enter your surname.";

elseif (strlen($formVars["surname"]) > 50)
$errors["surname"] = 
      "The surname cannot be longer than 50 " .
         "characters";


// Validate the Other names
if (empty($formVars["other_names"]))
// the user's other name cannot be a null string
$errors["other_names"] = 
          "You must enter your other names.";

elseif (strlen($formVars["other_names"]) > 50)
$errors["other_names"] = 
          "The other names cannot be longer than 50 " .
          "characters";

// Validate the Address
if (empty($formVars["address"]))
$errors["address"] = 
          "You must enter your address";

elseif (strlen($formVars["address"]) > 50)
$errors["address"] = 
          "The address cannot be longer than 50 " .
          "characters";

//Validate the phone number
if (empty($formVars["phone_no"]))
$errors["phone_no"] = 
        "You must enter your phone number";

elseif (strlen($formVars["phone_no"]) > 30)
$errors["phone_no"] = 
          "The address cannot be longer than 30 " .
          "characters";

//Validate the email
if (empty($formVars["email"]))
$errors["email"] = 
        "You must enter your email address";


// Validate the date of birth
if (empty($formVars["dob"]))
$errors["dob"] = 
          "You must provide a date of birth.";

elseif (!ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
        $formVars["dob"], $parts))
// Check the format
$errors["dob"] = 
        "The date of birth should be in the" .
        "format DD/MM/YYYY";


// Validate the gender
if (empty($formVars["gender"]))
$errors["gender"] = 
        "You must specify your gender";

// Validate the date of employment
if (empty($formVars["date_of_emp"]))
$errors["date_of_emp"] = 
        "You must enter your employment date";

elseif (!ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
        $formVars["date_of_emp"], $parts))
// Check the format
$errors["date_of_emp"] = 
        "The date of employment should be in the" .
        "format DD/MM/YYYY";

if (empty($formVars["level"]))
$errors["level"] = 
        "The level field cannot be empty";

// Only validate username if this is an INSERT
if (!session_is_registered("loginUsername"))
{
    if (empty($formVars["username"]))
    $errors["username"] =
            "You must enter the username";

    elseif (strlen($formVars["username"]) > 10)
    $errors["username"] =
            "The username can be no longer than 10 " .
            "characters";

    else
    {
        // Check if the email address is already in use in
        //  the winestore
        if (!($connection = @ mysql_pconnect($hostName,
                    $username,
                    $password)))
        showerror();

        if (!mysql_select_db($databaseName, $connection))
        showerror();

        $query = "SELECT * FROM login
                  WHERE username = '" .
        $formVars["username"] . "'";

        if (!($result = @ mysql_query ($query,
                    $connection)))
        showerror();

        // Is it taken?
        if (mysql_num_rows($result) == 1)
        $errors["username"] =
              "A salesman already exists with this " .
              "login name.";
              
             
    }
}


// Only validate password if this is an INSERT
// Validate password - between 6 and 8 characters
if (!session_is_registered("loginUsername") &&
    (strlen($formVars["loginPassword"]) < 6 ||
        strlen($formVars["loginPassword"] > ))
$errors["loginPassword"] = 
         "The password must be between 6 and 8 " .
         "characters in length";


foreach($error as $errorname=>$name){
              echo $name[$errorname];
              }

// Now the script has finished the validation, 
// check if there were any errors
if (count($errors) > 0)
{
    // Store the errors in the session variable
    $_SESSION["errors"] = $errors;

    // There are errors.  Relocate back to the client form
    header("Location: salesman.register.php");
    exit;
}

// If we made it here, then the data is valid

if (!isset($connection))
{
    if (!($connection = @ mysql_pconnect($hostName,
                $username,
                $password)))
    showerror();

    if (!mysql_select_db($databaseName, $connection))
    showerror();
}


// Is this an update?
if (session_is_registered("loginUsername"))
{
    $salesman_code = getSalesmanID($_SESSION["loginUsername"], $connection);

    $query = "UPDATE salesman SET ".
     "surname = \"" . $formVars["surname"] . "\", " .
     "other_names = \"" . $formVars["other_names"] . "\", " .
     "address = \"" . $formVars["address"] . "\", " .
     "phone_no = \"" . $formVars["phone_no"] . "\", " .
     "email = \"" . $formVars["email"] . "\", " .
     "dob = \"" . $formVars["dob"] . "\", " .
     "gender = \"" . $formVars["gender"] . "\", " .
     "date_of_emp = \"" . $formVars["date_of_emp"]. "\" " .
     " WHERE salesman_code =" . $salesman_code;
}
else
$query = "INSERT INTO salesman VALUES (NULL, " .
              "\"" . $formVars["surname"] . "\", " .
              "\"" . $formVars["other_names"] . "\", " .
              "\"" . $formVars["address"] . "\", " .
              "\"" . $formVars["phone_no"] . "\", " .
              "\"" . $formVars["email"] . "\", " .
              "\"" . $formVars["dob"] . "\", " .
              "\"" . $formVars["gender"] . "\", " .
              "\"" . $formVars["date_of_emp"] . "\" "
. ")";

// Run the query on the customer table
if (!(@ mysql_query ($query, $connection)))
showerror();


// If this was an INSERT, we need to INSERT
// also into the users table
if (!session_is_registered("loginUsername"))
{
    // Get the customer id that was created
    $salesman_code = @ mysql_insert_id($connection);

    // Use the first two characters of the
    // username as a salt for the password
    $salt = substr($formVars["username"], 0, 2);

    // Create the encrypted password
    $stored_password =
    crypt($formVars["loginPassword"],$salt);

    // Insert a new user into the user table
    $query = "INSERT INTO login
               SET salesman_code = $salesman_code,
                   password = '$stored_password',
                   username = '" . $formVars["username"] . "'".","."
                   level = '" . $formVars["level"] . "'";

    if (!($result = @ mysql_query ($query, $connection)))
    showerror();

    // Log the user into their new account
    session_register("loginUsername");

    $_SESSION["loginUsername"] = $formVars["username"];
}

// Clear the formVars so a future <form> is blank
session_unregister("formVars");
session_unregister("errors");


header("Location: salesman.mainform.php?salesman_code=$salesman_code");
?>


my problem lies in these two sections in the validate php file

if (count($errors) > 0)
{
    // Store the errors in the session variable
    $_SESSION["errors"] = $errors;

    // There are errors.  Relocate back to the client form
    header("Location: salesman.register.php");
    exit;
}

session_unregister("formVars");
session_unregister("errors");


header("Location: salesman.mainform.php?salesman_code=$salesman_code");

please help. i dont seem to be getting anywhere

Link to comment
Share on other sites

hi all, ive been working on this php form/submit script. it takes credentials from a form  and passes then in a session variable for validation. if i put the correct input..it writes the data in the database but does not redirect and instead shows a blank page. if the values are incorrect, it does not redirect to the form with errors. here's the code for the form

<?php
// This script shows the user a customer <form>.
// It can be used both for INSERTing a new customer and
// for UPDATE-ing an existing customer. If the customer 
// is logged in, then it is an UPDATE; otherwise, an 
// INSERT.


include 'include.inc';

set_error_handler("errorHandler");

// Show an error in a red font
function fieldError($fieldName, $errors)
{
    if (isset($errors[$fieldName]))
    echo "<font color=\"red\">" .
    $errors[$fieldName] .
           "</font><br>";
}

// Connect to a session
session_start();

// Initialise $formVars from the $_SESSION["formVars"] (if set)
if (session_is_registered("formVars"))
{
    $row = $_SESSION["formVars"];
    $formVars["surname"] = $row["surname"];
    $formVars["other_names"] = $row["other_names"];
    $formVars["address"] = $row["address"];
    $formVars["phone_no"] = $row["phone_no"];
    $formVars["email"] = $row["email"];
    $formVars["dob"] = $row["dob"];
    $formVars["gender"] = $row["gender"];
    $formVars["date_of_emp"] = $row["date_of_emp"];
    $formVars["level"] = $row["level"];
    $formVars["username"] = $row["username"];
    $formVars["loginPassword"] = $row["loginPassword"];

}

// Is the user logged in and were there no errors from a previous
// validation?  If so, look up the customer for editing
if (session_is_registered("loginUsername") && empty($_SESSION["errors"]))
{
    if (!($connection = @ mysql_pconnect($hostName,
                $username,
                $password)))
    showerror();

    if (!mysql_select_db($databaseName, $connection))
    showerror();

    $salesman_code = getSalesmanID($_SESSION["loginUsername"], $connection);

    $query = "SELECT * FROM salesman
               WHERE salesman_code = '$salesman_code'";

    if (!($result = @ mysql_query($query, $connection)))
    showerror();

    $row = mysql_fetch_array($result);

    // Reset $formVars, since we're loading from
    // the customer table
    $formVars = array();

    // Load all the form variables with customer data
    $formVars["surname"] = $row["surname"];
    $formVars["other_names"] = $row["other_names"];
    $formVars["address"] = $row["address"];
    $formVars["phone_no"] = $row["phone_no"];
    $formVars["email"] = $row["email"];
    $formVars["dob"] = $row["dob"];
    $formVars["gender"] = $row["gender"];
    $formVars["date_of_emp"] = $row["date_of_emp"];
    $formVars["level"] = $row["level"];
    $formVars["username"] = $row["username"];
    $formVars["loginPassword"] = $row["loginPassword"];
}

?>
<!DOCTYPE HTML PUBLIC 
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
    <head><title>Salesman Details</title></head>
    <body bgcolor="white">
        <?php
        // Show the user login status
        showLogin();
        ?>
        <form method="post" action="salesman.validate.php">
            <h3>Salesman Details</h3>
            <?php
            // Display any messages to the user
            showMessage();

            // Show meaningful instructions for UPDATE or INSERT
            if (session_is_registered("loginUsername"))
            echo "<h4>Please amend your details below as required.</h4>";
            else
            echo "<h4>Please fill in the details below to join.</h4>";
            ?>
            <table>
                <col span="1" align="right">


                <tr><td>Surname:</td>
                    <td><?php echo fieldError("surname", $_SESSION["errors"]); ?>
                        <input type="text" name="surname"
                           value="<?php echo $formVars["surname"]; ?>" size=50></td>
                </tr>

                <tr><td>Other Names:</td>
                    <td><?php echo fieldError("other_names", $_SESSION["errors"]); ?>
                        <input type="text" name="other_names"
                           value="<?php echo $formVars["other_names"]; ?>" size=50></td>
                </tr>

                <tr><td>Address:</td>
                    <td><?php echo fieldError("address", $_SESSION["errors"]); ?>
                        <input type="text" name="address"
                           value="<?php echo $formVars["address"]; ?>" size=50></td>
                </tr>

                <tr><td>Phone Number:</td>
                    <td><?php echo fieldError("phone_no", $_SESSION["errors"]); ?>
                        <input type="text" name="phone_no"
                           value="<?php echo $formVars["phone_no"]; ?>" size=30></td>
                </tr>

                <tr><td>Email:</td>
                    <td><?php echo fieldError("email", $_SESSION["errors"]); ?>
                        <input type="text" name="email"
                           value="<?php echo $formVars["email"]; ?>" size=30></td>
                </tr>

                <tr><td>Date of Birth:<br>(YYYY-MM-DD) format</td>
                    <td><?php echo fieldError("dob", $_SESSION["errors"]); ?>
                        <input type="text" name="dob"
                           value="<?php echo $formVars["dob"]; ?>" size=10></td>
                </tr>

                <tr><td>Gender:</td>
                    <td><?php echo fieldError("gender", $_SESSION["errors"]); ?>
                        <input type="text" name="gender"
                           value="<?php echo $formVars["gender"]; ?>" size=20></td>
                </tr>

                <tr><td>Date of Employment <br>(YYYY-MM-DD) format:</td>
                    <td><?php echo fieldError("date_of_emp", $_SESSION["errors"]); ?>
                        <input type="text" name="date_of_emp"
                           value="<?php echo $formVars["date_of_emp"]; ?>" size=20></td>
                </tr>

                <tr><td>Department Level:</td>
                    <td><?php echo fieldError("level", $_SESSION["errors"]); ?>
                        <input type="text" name="level"
                           value="<?php echo $formVars["level"]; ?>" size=20></td>
                </tr>
                <?php
                // Only show the username and password widgets to new users
                if (!session_is_registered("loginUsername"))
                {
                    ?>
                <tr><td>Username:</td>
                    <td><?php echo fieldError("username", $_SESSION["errors"]); ?>
                        <input type="text" name="username"
                           value="<?php echo $formVars["username"]; ?>" size=30></td>
                </tr>

                <tr><td>Password:</td>
                    <td><?php echo fieldError("loginPassword", $_SESSION["errors"]); ?>
                        <input type="password" name="loginPassword"
                           value="<?php echo $formVars["loginPassword"]; ?>" size=15></td>
                </tr>
                <?php
            }
            ?>
                <tr>
                    <td><input type="submit" value="Submit"></td>
                </tr>
            </table>
        </form>
        <br><a href="http://validator.w3.org/check/referer"><img
                src="http://www.w3.org/Icons/valid-html401" height="31" width="88"
            align="right" border="0" alt="Valid HTML 4.01!"></a>
    </body>
</html>


 

here is the validate code in a seperate php file

<?php
// This script validates customer data entered into
// example.customer.2.php.
// If validation succeeds, it INSERTs or UPDATEs
// a customer and redirect to a receipt page; if it 
// fails, it creates error messages and these are later 
// displayed by example.customer.2.php.

include 'include.inc';

set_error_handler("errorHandler");

// Initialize a session
session_start();

// Register an error array - just in case!
if (!session_is_registered("errors"))
session_register("errors");

// Clear any errors that might have been 
// found previously
$errors = array();

// Set up a $formVars array with the POST variables
// and register with the session.
if (!session_is_registered("formVars"))
session_register("formVars");

foreach($_POST as $varname => $value)
$formVars[$varname] = trim(clean($value, 50));

$_SESSION["formVars"] = $formVars;

// Validate the surname
if (empty($formVars["surname"])) 
// surname cannot be a null string
$errors["surname"] = 
          "You must enter your surname.";

elseif (strlen($formVars["surname"]) > 50)
$errors["surname"] = 
      "The surname cannot be longer than 50 " .
         "characters";


// Validate the Other names
if (empty($formVars["other_names"]))
// the user's other name cannot be a null string
$errors["other_names"] = 
          "You must enter your other names.";

elseif (strlen($formVars["other_names"]) > 50)
$errors["other_names"] = 
          "The other names cannot be longer than 50 " .
          "characters";

// Validate the Address
if (empty($formVars["address"]))
$errors["address"] = 
          "You must enter your address";

elseif (strlen($formVars["address"]) > 50)
$errors["address"] = 
          "The address cannot be longer than 50 " .
          "characters";

//Validate the phone number
if (empty($formVars["phone_no"]))
$errors["phone_no"] = 
        "You must enter your phone number";

elseif (strlen($formVars["phone_no"]) > 30)
$errors["phone_no"] = 
          "The address cannot be longer than 30 " .
          "characters";

//Validate the email
if (empty($formVars["email"]))
$errors["email"] = 
        "You must enter your email address";


// Validate the date of birth
if (empty($formVars["dob"]))
$errors["dob"] = 
          "You must provide a date of birth.";

elseif (!ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
        $formVars["dob"], $parts))
// Check the format
$errors["dob"] = 
        "The date of birth should be in the" .
        "format DD/MM/YYYY";


// Validate the gender
if (empty($formVars["gender"]))
$errors["gender"] = 
        "You must specify your gender";

// Validate the date of employment
if (empty($formVars["date_of_emp"]))
$errors["date_of_emp"] = 
        "You must enter your employment date";

elseif (!ereg("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
        $formVars["date_of_emp"], $parts))
// Check the format
$errors["date_of_emp"] = 
        "The date of employment should be in the" .
        "format DD/MM/YYYY";

if (empty($formVars["level"]))
$errors["level"] = 
        "The level field cannot be empty";

// Only validate username if this is an INSERT
if (!session_is_registered("loginUsername"))
{
    if (empty($formVars["username"]))
    $errors["username"] =
            "You must enter the username";

    elseif (strlen($formVars["username"]) > 10)
    $errors["username"] =
            "The username can be no longer than 10 " .
            "characters";

    else
    {
        // Check if the email address is already in use in
        //  the winestore
        if (!($connection = @ mysql_pconnect($hostName,
                    $username,
                    $password)))
        showerror();

        if (!mysql_select_db($databaseName, $connection))
        showerror();

        $query = "SELECT * FROM login
                  WHERE username = '" .
        $formVars["username"] . "'";

        if (!($result = @ mysql_query ($query,
                    $connection)))
        showerror();

        // Is it taken?
        if (mysql_num_rows($result) == 1)
        $errors["username"] =
              "A salesman already exists with this " .
              "login name.";
              
             
    }
}


// Only validate password if this is an INSERT
// Validate password - between 6 and 8 characters
if (!session_is_registered("loginUsername") &&
    (strlen($formVars["loginPassword"]) < 6 ||
        strlen($formVars["loginPassword"] > ))
$errors["loginPassword"] = 
         "The password must be between 6 and 8 " .
         "characters in length";


foreach($error as $errorname=>$name){
              echo $name[$errorname];
              }

// Now the script has finished the validation, 
// check if there were any errors
if (count($errors) > 0)
{
    // Store the errors in the session variable
    $_SESSION["errors"] = $errors;

    // There are errors.  Relocate back to the client form
    header("Location: salesman.register.php");
    exit;
}

// If we made it here, then the data is valid

if (!isset($connection))
{
    if (!($connection = @ mysql_pconnect($hostName,
                $username,
                $password)))
    showerror();

    if (!mysql_select_db($databaseName, $connection))
    showerror();
}


// Is this an update?
if (session_is_registered("loginUsername"))
{
    $salesman_code = getSalesmanID($_SESSION["loginUsername"], $connection);

    $query = "UPDATE salesman SET ".
     "surname = \"" . $formVars["surname"] . "\", " .
     "other_names = \"" . $formVars["other_names"] . "\", " .
     "address = \"" . $formVars["address"] . "\", " .
     "phone_no = \"" . $formVars["phone_no"] . "\", " .
     "email = \"" . $formVars["email"] . "\", " .
     "dob = \"" . $formVars["dob"] . "\", " .
     "gender = \"" . $formVars["gender"] . "\", " .
     "date_of_emp = \"" . $formVars["date_of_emp"]. "\" " .
     " WHERE salesman_code =" . $salesman_code;
}
else
$query = "INSERT INTO salesman VALUES (NULL, " .
              "\"" . $formVars["surname"] . "\", " .
              "\"" . $formVars["other_names"] . "\", " .
              "\"" . $formVars["address"] . "\", " .
              "\"" . $formVars["phone_no"] . "\", " .
              "\"" . $formVars["email"] . "\", " .
              "\"" . $formVars["dob"] . "\", " .
              "\"" . $formVars["gender"] . "\", " .
              "\"" . $formVars["date_of_emp"] . "\" "
. ")";

// Run the query on the customer table
if (!(@ mysql_query ($query, $connection)))
showerror();


// If this was an INSERT, we need to INSERT
// also into the users table
if (!session_is_registered("loginUsername"))
{
    // Get the customer id that was created
    $salesman_code = @ mysql_insert_id($connection);

    // Use the first two characters of the
    // username as a salt for the password
    $salt = substr($formVars["username"], 0, 2);

    // Create the encrypted password
    $stored_password =
    crypt($formVars["loginPassword"],$salt);

    // Insert a new user into the user table
    $query = "INSERT INTO login
               SET salesman_code = $salesman_code,
                   password = '$stored_password',
                   username = '" . $formVars["username"] . "'".","."
                   level = '" . $formVars["level"] . "'";

    if (!($result = @ mysql_query ($query, $connection)))
    showerror();

    // Log the user into their new account
    session_register("loginUsername");

    $_SESSION["loginUsername"] = $formVars["username"];
}

// Clear the formVars so a future <form> is blank
session_unregister("formVars");
session_unregister("errors");


header("Location: salesman.mainform.php?salesman_code=$salesman_code");
?>


my problem lies in these two sections in the validate php file

if (count($errors) > 0)
{
    // Store the errors in the session variable
    $_SESSION["errors"] = $errors;

    // There are errors.  Relocate back to the client form
    header("Location: salesman.register.php");
    exit;
}

session_unregister("formVars");
session_unregister("errors");


header("Location: salesman.mainform.php?salesman_code=$salesman_code");

please help. i dont seem to be getting anywhere

well right now i m getting off to bed so dont have much time to explain why header is not working, but quick solution would be use

echo "<script>document.location.href='salesman.mainform.php?salesman_code=$salesman_code'</script>";

exit();

Link to comment
Share on other sites

session_is_registered() and session_register() were depreciated and turned off by default 7 1/2 years ago. You need to set and reference $_SESSION variables only.

 

To find out why your code is not redirecting (assuming you have already debugged the logic and know that the header() statements are being executed), add the following two lines of code immediately after your first opening <?php tag on any affected page -

ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

thanks for the reply. the document.location worked. however it still does not redirect the errors to the form. i think its because of the session_register functions. but thats the only way i know how to check and test for session variables. can anyone show me an alternative please. thanks

Link to comment
Share on other sites

ok so i managed to test resolve the $_SESSION issue by using

 

if(isset($_SESSION["formVars"])){
          do stuff
else
      do something else

 

so i replaced it in my code as follows

if (count($errors) > 0)
{
    // Store the errors in the session variable
    $_SESSION["errors"] = $errors;

    echo "<script>document.location.href='salesman.mainform.php?salesman_code=$salesman_code'</script>";
    exit;
}

 

it redirects but still does not display the errors. i just dont get why the errors dont display. please help

 

Link to comment
Share on other sites

got it finally. after a few frastruating googling, i got a solution to remove the left padding using header like so

header("Location: salesman.mainform.php?salesman_code=$salesman_code");

as in make sure there are no whitespaces before the header redirector.

thanks everyone

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.