wiscWeb
wiscWeb

Reputation: 213

Posting a value from Dropdown List from PHP div page

I'm having an issue in passing a value from a dropdown list that is in a separate PHP file that is being used by jquery.

I ended up getting the values from the dropdown list that isn't posting correctly by using jquery based on the value selected in the first dropdown list. The dropdown list in question is populating correctly, but with the way I have it setup I cannot post the value to the submit PHP page.

I'm pretty sure it has to do with the way I have it setup; however, I'm very new to jquery and was looking for some guidance.

The main PHP Page (the small areas in question)

<select name="department_list" id="department_list" onchange="$('#singleUser').load('get_users.php?nid='+this.value);">

...

<div id="singleUser" class="singleUser">
</div>

The PHP page (get_users) used to fill the values (only the area in question)

echo '<p style="text-align:center">';
echo "    
    <br />
    Select a person to receive the form
<br />";

echo "<select id='userSelect' class='userSelect'>";
if ($id == '50') {
    echo "<option value='Done'>Done</option>";
    echo "</select>";
}else {
echo "<option value='none'>Select user</option>";

try {
$db = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

        $stmt = $db->prepare("SELECT dm.empid AS empid, u.name AS name FROM mytable dm
                    JOIN mytable2 u ON dm.empid = u.id 
                    WHERE dm.deptid = :id
                    ORDER BY u.name");
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        while ($r = $stmt->fetch()) {
        $empid = $r['empid'];
                $userName = $r['name'];

               echo "<option value='".$empid."'>".$userName."</option>"; 

    }
echo "</select>";
echo "</p>";
    $db = null;
    }
    catch (PDOException $ex) {
        echo "An Error occurred!";
        }
}//end else

In the submit page:

if(isset($_POST['userSelect'])){
$givenID = $_POST['userSelect'];
//the rest of my code

I do have the div code above within the form tags and have method="post". All of my other inputs post correctly, so I'm thinking it has to do with the way I have only the div tags within the main page. Again, I'm pretty new to all of this so any ideas or changes that I should make so it posts correctly would be greatly appreciated.

Upvotes: 1

Views: 444

Answers (2)

Laurynas Mališauskas
Laurynas Mališauskas

Reputation: 1919

i think the error is in the PHP file generating the user select it is missing the name attribute name="userSelect"

echo "<select id='userSelect' class='userSelect'>";

it should be

echo '<select id="userSelect" name="userSelect" class="userSelect">';

every form element with the name attribute gets posted with its value. if you do not enter the name attribute, the value can not be retrieved from the $_POST array. Also have in mind that disabled form inputs also do not get posted.

Edited the PHP quotes. Use Single qutes everyt time you do not need to insert PHP variable into the string. It is ~9 times faster than double quotes ;)

Upvotes: 1

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17370

You forgot the name of the select when you write it with php:

change this:

echo "<select id='userSelect' class='userSelect'>";

to

echo "<select id='userSelect' name='userSelect' class='userSelect'>";

Upvotes: 1

Related Questions