Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form field error message Session Variable issues

    Hello,

    I have a form. If any of the fields are blank upon submission, I want it to kick out an error message and print that on the top of the form after redirect. It is not working and I am pulling out my hair. Here is the code for the form itself....validation code is at top:

    PHP Code:
    <?php
    //Password protection element
    //Start session
    session_start();
        
    //Check whether the session variable SESS_CUSTOMER_ID is present or not
    if(!isset($_SESSION['SESS_CUSTOMER_ID']) || (trim($_SESSION['SESS_CUSTOMER_ID']) == '')) {
        
    header("location: /index/PHP-Login/access-denied.php");
        exit();
        }
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add Employee</title>
    </head>
    <body>
    <?php
    $first_name
    =$_POST['first_name'];
    $last_name=$_POST['last_name'];
    $dob=$_POST['dob'];
    $email=$_POST['email'];
    $primary_ranking=$_POST['primary_ranking'];
    $secondary_ranking=$_POST['secondary_ranking'];
    $third_ranking=$_POST['third_ranking'];
    $preferred_contact_method=$_POST['preferred_contact_method'];
    $permission_level=$_POST['permission_level'];
    $phone=$_POST['phone'];
    ?>
        
    <?php
    // If blank fields are found, print error message

        
    if( isset($_SESSION['ERRMSG_ARR1']) && is_array($_SESSION['ERRMSG_ARR1']) && count($_SESSION['ERRMSG_ARR1']) >) {
            echo 
    '<ul class="err">';
            foreach(
    $_SESSION['ERRMSG_ARR1'] as $msg1) {
                echo 
    '<li>',$msg1,'</li>'
            }
            echo 
    '</ul>';
            unset(
    $_SESSION['ERRMSG_ARR1']);
        }
    ?>
    <p>
    <h3>
        <?php
    echo "$SESS_COMPANY_NAME";
    ?>
        </h3>
            <h2><a>Add Employee</a></h2>
            <form method="post" action="new_employee_form_process.php">
                    
                <p><i>Add employee information below, then click submit.</p></i><p>
                
                <img id="bottom" src="bottom.png" alt=""><p>
                
                <label class="description" for="element_1">First Name </label>
                <input id="element_1" name="first_name" class="element text medium" type="text" maxlength="255" value=""/> 
                
                    <label class="description" for="element_2">Last Name </label>
                <input id="element_2" name="last_name" class="element text medium" type="text" maxlength="255" value=""/> 
                <p>
                
                    <label class="description" for="element_3">DOB <i>(MM/DD/YEAR)</i> </label>
                <input id="element_3" name="dob" class="element text medium" type="text" maxlength="255" value=""/> 
                <p>
                
                    <label class="description" for="element_4">Email Address </label>
                <input id="element_4" name="email" class="element text medium" type="text" maxlength="255" value=""/>          
                <p>
                
                    <label class="description" for="element_10">Phone Number </label>
                <input id="element_10" name="phone" class="element text medium" type="text" maxlength="10" value=""/> 
                <p>
                
                    <label class="description" for="element_5">Primary Ranking </label>
                <select class="element select medium" id="element_5" name="primary_ranking"> 
                <option value="1" >First option</option>
                 </select>
                <p>
                
                <label class="description" for="element_6">Secondary Ranking </label>
                <select class="element select medium" id="element_6" name="secondary_ranking"> 
                <option value="1" >First option</option>
                </select>
                <p>
                
                <label class="description" for="element_7">Third Ranking </label>
                <select class="element select medium" id="element_7" name="third_ranking"> 
                <option value="1" >First option</option>
                </select>
                <p>
                
                <label class="description" for="element_8">Preferred Contact Method </label>
                </select>            
                 <select class="element select medium" id="element_8" name="preferred_contact_method"> 
                    <option value="1">Text Message</option>
                 <option value="2">Phone Call</option>
                 </select>
                <p>
        
                <label class="description" for="element_9">Permission Level </label>
                <select class="element select medium" id="element_9" name="permission_level"> 
                <option value ="1">End User</option>
                    <option value ="2">Administrator</option>
                </select>
                <p>
        
                <input type="hidden" name="form_id" value="318257" />
                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
                </form>    
                <img id="bottom" src="bottom.png" alt="">
    </body>
    </html>
    And here is the code for the form process execution script. Any ideas? Please...thanks. Relatively newb so go easy Thanks

    PHP Code:
    <?php

    $first_name
    =$_POST['first_name'];
    $last_name=$_POST['last_name'];
    $dob=$_POST['dob'];
    $email=$_POST['email'];
    $primary_ranking=$_POST['primary_ranking'];
    $secondary_ranking=$_POST['secondary_ranking'];
    $third_ranking=$_POST['third_ranking'];
    $preferred_contact_method=$_POST['preferred_contact_method'];
    $permission_level=$_POST['permission_level'];
    $phone=$_POST['phone'];

    //Input Validations
        
    if($first_name == '') {
            
    $errmsg_arr[] = 'First Name is missing';
            
    $errflag true;
        }
        if(
    $last_name == '') {
            
    $errmsg_arr[] = 'Last Name is missing';
            
    $errflag true;
        }
        if(
    $dob == '') {
            
    $errmsg_arr[] = 'Username missing';
            
    $errflag true;
        }
        if(
    $email == '') {
            
    $errmsg_arr[] = 'Email is missing';
            
    $errflag true;
        }
        if(
    $primary_ranking == '') {
            
    $errmsg_arr[] = 'Must have atleast primary ranking';
            
    $errflag true;
        }
        if(
    $preferred_contact_method == '') {
            
    $errmsg_arr[] = 'Must select primary contact method';
            
    $errflag true;    
        }
            if(
    $phone == '') {
            
    $errmsg_arr[] = 'Must provide phone number';
            
    $errflag true;
        }   

    //If there are input validations, redirect back to the registration form
        
    if($errflag) {
            
    $_SESSION['ERRMSG_ARR1'] = $errmsg_arr1;
            
    session_write_close();
               
    header("location: /index/forms/new_employee_form/new_employee_form.php");
            exit();
        }

        
    // connect to database

    $conmysql_connect("firecasey.startlogicmysql.com""callbackstaffing""login1") or die ('Error , check your server connection.');
    mysql_select_db("callbackstaffing");

    //Inserts data from form into table

    mysql_query("INSERT INTO end_users (first_name, last_name, dob, email, phone, primary_ranking, secondary_ranking, third_ranking, preferred_contact_method, permission_level) 
    VALUES('$first_name', '$last_name', '$dob', '$email', '$phone', '$primary_ranking', '$secondary_ranking', '$third_ranking', '$preferred_contact_method', '$permission_level')"

    or die(
    mysql_error());  

    $text1='Thank you, '.$first_name." ".$last_name." ".'has been added successfully.';

    mysql_close($con);
    ?>
    <html>
    <head>
    </head>
    <body>
    <p>
        <h2>
    <?php
    echo $text1;
    ?>
        </h2>
    <p>
        Please click <a href="/index/forms/new_employee_form/new_employee_form.php">here </a>to return to add another user.
    </body>
    </html>

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,386
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    This MUST be at the top of EVERY script that uses SESSION:

    <?php
    session_start();

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you!

    Last question.... does it have to be only use once on a page?

    Meaning if I use is like this:


    <?php
    session_start();

    code here using session variable...

    ?>

    HTML Code here


    <?php
    session_start();

    more code here using same or different session variable..

    ?>


    do I only need the one at top, or both - like i have it there?

  • #4
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,386
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    Yes, only once, or you'll get an error like this: "session already started"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •