Tartar
Tartar

Reputation: 5452

PHP Unable to POST select box values

I'am trying to get some values from users and after that I'am inserting these values to my database but I can't get some of my select boxes's values using $_POST.

Users first select intership type via selectbox and depending on value of this selectbox the bottom menu changes via jQuery (That's why these IENG349ReqCourses and IENG449ReqCourses div's are hidden).

But I can't get the values of grade selecboxes inside of these hidden div's. Rest of input fields are okay, I can get their values and save to my database.

HTML:

<form id="intershipStage1Form" method="POST" action="form1.php">
    <div id="generalInfo">
        <center>
            <table>
                <tr>
                    <td colspan="2" valign="middle" align="center">
                        <label id="stuNameLabel">Student Full Name: <?php echo $_SESSION['username']; ?></label>
                    </td>
                </tr>
                <tr>
                    <td title="Your student number contains only numbers and should be 11 digits " valign="middle"><label id="stuNumberLabel">Student Number:</label></td>
                    <td><input id="stuNumberText" class="form1Text" type="text" name="stuNumberText" /></td>
                </tr>
                <tr>
                    <td title="A sample format: 3.16 and do not use comma(,)" valign="middle"><label id="stuGPALabel">Student GPA:</label></td>
                    <td><input id="stuGPAText" class="form1Text" type="text" name="stuGPAText" /></td>
                </tr>
                <tr>
                    <td title="Choose your academic advisor" valign="middle"><label id="stuAdvisorLabel">Student Advisor:</label></td>
                    <td align="center">
                        <select id="advisorSelectionBox" name="advisorSelectionBox">
                            <option value="">--select--</option>
                            <?php
                                $userType = "A";
                                $stmt = $db->prepare("SELECT * FROM users WHERE UserType = ?");
                                if($stmt == "false"){
                                    die('Query error !'.$db->error);  
                                }
                                $stmt->bind_param('s', $userType);
                                $stmt->execute();
                                $result = $stmt -> get_result();

                                while($advisor = $result ->fetch_array(MYSQLI_BOTH)){
                                    echo '<option value="'.$advisor["UserID"].'">'.$advisor['FirstName']." ".$advisor['LastName'].'</option>';
                                }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td title="Choose your current study year" valign="middle">
                        <label>Study Year:</label>
                    </td>
                    <td align="center">
                        <select id="studyYearBox" name="studyYearBox">
                            <option value="">--select--</option>
                            <option value="SOPHOMORE">SOPHOMORE</option>
                            <option value="JUNIOR">JUNIOR</option>
                            <option value="SENIOR">SENIOR</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td title="Choose your internship type before try to sent this form to your advisor" valign="middle">
                        <label>Internship Type:</label>
                    </td>
                    <td align="center">
                        <select id="intershipTypeBox" name="intershipTypeBox">
                            <option value="">--select--</option>
                            <option value="IENG349">IENG 349</option>
                            <option value="IENG449">IENG 449</option>
                        </select>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    <div id="litleSpacer"></div>
    <div id="IENG349ReqCourses" class="reqCourses" hidden="true">
        <center>
            <table>
                <caption style="color:#f00;">Please select your grades</caption>
                <tr>
                    <td valign="middle">
                        <label>IENG 102 - Intro to IE:</label>
                    </td>
                    <td>
                        <select name="IENG102Grade">
                            <option value="">--select--</option>
                            <option value="A">A</option>
                            <option value="A-">A-</option>
                            <option value="B+">B+</option>
                            <option value="B">B</option>
                            <option value="B-">B-</option>
                        </select>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    <div id="IENG449ReqCourses" class="reqCourses" hidden="true">
        <center>
            <table>
                <caption style="color:#f00;">Please select your grades</caption>
                    <tr>
                        <td valign="middle">
                            <label>IENG 312 - System Simulation:</label>
                        </td>
                        <td>
                            <select name="IENG312Grade">
                                <option value="">--select--</option>
                                <option value="A">A</option>
                                <option value="A-">A-</option>
                                <option value="B+">B+</option>
                                <option value="B">B</option>
                                <option value="B-">B-</option>
                            </select>
                        </td>
                    </tr>
            </table>
        </center>
    </div>
    <div id="litleSpacer"></div>
    <input id="sendButton" type="submit" name="sendButton" value="SEND FOR APPROVEMENT"/>
</form>

PHP:

<?php
    $stuID = $_SESSION['user_id'];
    $stuFullName = $_SESSION['username'];
    $stuNumber = $_POST['stuNumberText'];
    $stuGPA = $_POST['stuGPAText'];
    $stuAdvisor = $_POST['advisorSelectionBox'];
    $studyYear = $_POST['studyYearBox'];
    $internshipType = $_POST['intershipTypeBox'];
    $coordinatorAppr = "SENT";
    $advisorAppr = "SENT";

    $IENG102 = $_POST['IENG102Grade'];
    $IENG312 = $_POST['IENG312Grade'];

    $insert_stmt = $db->prepare("INSERT INTO internship_form_info VALUES(NULL,?,?,?,?,?,?,?,?,?)");
    if($insert_stmt === false){
        die('Query Error: '.$db->error);
    }
    $insert_stmt ->bind_param("sssssssii",$stuFullName,$stuNumber,$stuGPA,$studyYear,$internshipType,$advisorAppr,$coordinatorAppr,$stuID,$stuAdvisor);
    $insert_stmt ->execute();

    $formID = $db->insert_id;

    if($internshipType=="IENG349"){
        $insert_stmt2 = $db->prepare("INSERT INTO ieng349_req_courses_grades VALUES(NULL,?,?,?)");
        if($insert_stmt2 === false){
            die('Query Error: '.$db->error);
        }
        $insert_stmt2 ->bind_param("iisssssss",$stuID,$formID,$IENG102);
        $insert_stmt2 ->execute();
    }
    else if($internshipType=="IENG449"){
        $insert_stmt3 = $db->prepare("INSERT INTO ieng449_req_courses_grades VALUES(NULL,?,?,?)");
        if($insert_stmt3 === false){
            die('Query Error: '.$db->error);
        }
        $insert_stmt3 ->bind_param("iisssssssssssss",$stuID,$formID,$IENG312);
        $insert_stmt3 ->execute();
    }

    $db->close();

So what can be the problem? Any help would be appriciated!

Upvotes: 0

Views: 259

Answers (2)

pr0metheus
pr0metheus

Reputation: 488

I suggest to you, to check for example in Live Http Headers, which $_POST date you send. Also in your very top of script put var_dump($_POST);

Upvotes: 0

Ali
Ali

Reputation: 151

You have to make one option as selected to get it posted to the server. If field is hidden and no option is marked selected, you will get no value on server.

Upvotes: 1

Related Questions