1

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.

2
  • I only posted small chunks of the code. I stated in the last paragraph that it is located within the form tags.
    – wiscWeb
    Aug 13, 2013 at 16:10
  • I noticed too late, and removed the comment. ma bad Aug 13, 2013 at 16:10

2 Answers 2

1

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'>";
0
1

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 ;)

2
  • I noticed it too. Ha, oh well. It also would've gotten the point across.
    – wiscWeb
    Aug 13, 2013 at 16:19
  • 1
    Sorry for that :) edited the answer regarding the PHP quotes :) Aug 13, 2013 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.