Form repeats values

Posted by Tunji Gbadamosi on Stack Overflow See other posts from Stack Overflow or by Tunji Gbadamosi
Published on 2010-03-23T18:39:36Z Indexed on 2010/03/23 18:43 UTC
Read the original article Hit count: 531

Filed under:
|
|

I have a dynamically generated form to accept guest details and store the results into the a session array. However, when I retrieve the details, I keep finding that the last two guest details are always the same even though different inputs were given.

Here's my form for getting the values (guests.php):

<?php
 session_start();
 require_once 'FormDB.php';
 include 'connect.php';
 include 'guests_.php';

 ?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Enter guest details</title>
    <script language="JavaScript" type="text/javascript" src="validation_functions.js"></script>
</head>
<body>
    <?php

    if($_SESSION['logged_in']){
        //print '<script type="text/javascript">';
            //print 'alert("You have successfully logged in '. $_SESSION['volunteer']['first_name'].'")';
        //print '</script>';

        $first_name="first_name";
        $surname="surname";
        $sex="sex";
        $age = "age";

        echo $error;
        //echo '<form name="choose" action="tables.php" method="post" onsubmit="return validate_guests(this);">';
        echo '<form name="choose" action="guests.php" method="post" onsubmit="return validate_guests(this);">';

        echo '<input type="hidden" name="hidden_value" value="'.$_SESSION['no_guests'].'" />';

        if($_SESSION['no_guests'] >= 1){

            echo '<table border="1">';

            echo '<th>First Name</th>';
            echo '<th>Surname</th>';
            echo '<th>Day of Birth</th>';
            echo '<th>Month of Birth</th>';
            echo '<th>Year of Birth</th>';
            echo '<th>Sex</th>';

            //echo '<div id="volunteer">';
                echo '<tr>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['first_name'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['surname'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['dob_day'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['dob_month'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['dob_year'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['sex'];
                    echo '</td>';
                echo '</tr>';
            //echo '</div>';


            for($i=0;$i<$_SESSION['no_guests'];$i++){
                //$guest = "guest_".$i;
                //echo '<div class="'.$guest.'">';
                        echo '<tr>';
                            echo '<td>';
                                echo '<input type="text" name="guest['.$i.']['.$first_name.']" id="fn'.$i.'">';
                            echo '</td>';
                            echo '<td>';
                                echo '<input type="text" name="guest['.$i.']['.$surname.']" id="surname'.$i.'">';
                            echo '</td>';

                            echo '<td>';
                                echo '<select name="guest['.$i.'][dob_day]" id="dob_day'.$i.'">';
                                    for($j=1;$j<32;$j++){
                                        echo"<option value='$j'>$j</option>";
                                    }
                                echo '</select>';
                            echo '</td>';

                            echo '<td>';
                                echo '<select name="guest['.$i.'][dob_month] id="dob_month'.$i.'">';
                                    for($j=0;$j<sizeof($month);$j++){
                                        $value = ($j + 1);
                                        echo"<option value='$value'>$month[$j]</option>";
                                    }
                                echo '</select>';
                            echo '</td>';

                            echo '<td>';
                                echo '<select name="guest['.$i.'][dob_year] id="dob_year'.$i.'">';
                                    for($j=1900;$j<$year_limit;$j++){
                                        echo"<option value='$j'>$j</option>";
                                    }
                                echo '</select>';
                            echo '</td>';

                            echo '<td>';
                                echo '<select name="guest['.$i.']['.$sex.']" id="sex'.$i.'">';
                                    echo '<option>Female</option>';
                                    echo '<option>Male</option>';
                                echo '</select>';
                            echo '</td>';
                        echo '</tr>';
                //echo '</div>';
            }
            echo '</table>';
        }
        else{
            echo '<table border="1">';

            echo '<th>First Name</th>';
            echo '<th>Surname</th>';
            echo '<th>Day of Birth</th>';
            echo '<th>Month of Birth</th>';
            echo '<th>Year of Birth</th>';
            echo '<th>Sex</th>';
            echo '<th>Table</th>';
            echo '<th>Seat</th>';
            echo '<th>Menu</th>';

            //echo '<div id="volunteer">';
                echo '<tr>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['first_name'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['surname'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['dob_day'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['dob_month'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['dob_year'];
                    echo '</td>';
                    echo '<td>';
                        echo $_SESSION['volunteer']['sex'];
                    echo '</td>';
                echo '</tr>';
            //echo '</div>';
        }
        echo '</table>';
        echo '<input type="submit" value="Submit">';
    echo '</form>';
    }
    else{
        print '<script type="text/javascript">';
            print 'alert("You have not successfully logged in '.
        $_SESSION['volunteer']['first_name'].'")';
        print '</script>';
    }
    ?>
</body>

Here's my code for processing the details (guests_.php):

<?php
$error = "";
if($_POST){
//$guests = array();
$guests = isset($_POST['guest']) ? $_POST['guest'] : null;
if($guests){
    foreach($guests as &$guest){
        $guest['first_name'] = ucwords(strip_tags($guest['first_name']));
        $guest['surname'] = ucwords(strip_tags($guest['surname']));
        $guest['dob_day'] = ucwords(strip_tags($guest['dob_day']));
        $guest['dob_month'] = ucwords(strip_tags($guest['dob_month']));
        $guest['dob_year'] = ucwords(strip_tags($guest['dob_year']));
        $guest['sex'] = ucwords(strip_tags($guest['sex']));
    }
}

foreach($guests as $guest){
    $date = $form->create_date($guest['dob_day'], $guest['dob_month'], $guest['dob_year']);
    $exist = $form->user_exists($guest['first_name'], $guest['surname'], $date, $guest['sex']);
    if($exist != ""){
        $error .= $exist;
    }
}

if($error == ""){
    //$_SESSION['existent_guests'] = FALSE;
    $_SESSION['guests'] = $guests;
    $form->set_guests($_SESSION['guests']);
    //$form->set_volunteer($_SESSION['volunteer']);
    header("location: tables.php");
    exit();

 }

}
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about forms