GavinC
GavinC

Reputation: 53

Cannot retrieve value from <select> option to send to php

Can someone please help me in the following matter, I cannot seem to find out where my problem is.

I have a 'select' option which has a default value of '---' and then the rest of the options are populated with criteria determined by the selected Member above it. I have read through all the PHP and Select problems or the forum and tried all the solutions provided, but none of the solutions has been able to help me retrieve the value from the 'select' to send to another query.

Here is my code:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
Please select a Dependant if needed:                    
<select name="DependantList">
<!-- Used to insert a blank entry for 1st option -->
    <option value="1" selected="selected">---</option>
    <!-- Query will then insert all dependant numbers for member selected -->
    <?php
        $sql= ("SELECT MEM_DEPNO FROM WEB_DEPENDANT
        WHERE MEM_NO = '$selMember'
        AND MEM_DEPNO != 0
        ORDER BY MEM_DEPNO");

        $result=$dh->query($sql) or die ("Error in: $sql1");
        $array=$result->fetchALL(PDO::FETCH_ASSOC);
        $count=count($array);

        $x = 0;
        $a = 2;
        while ($x < count($array))
        {
            echo "<option value=$a>".$array[$x][MEM_DEPNO]."</option>";
            $x = $x + 1;
            $a = $a + 1;                                
        }
    ?>
</select>
</form>

Now here I try and retrieve the data from the 'select' and display its value, but it displays nothing:

<?php
    $selectDependant = isset($_POST['DependantList']);
    echo "Selected:" . $selectDependant
?>

I want to retrieve the selected 'Dependant' value and then send it to the following SQL to populate my next grid:

<table width="99%" border="1" cellpadding="3" bordercolor="#000000">
<tr>
    <th width = '97' height = "45" bordercolor="#000000" class="ParagraphHeader">Dependant<br/> number</th>
    <th width = '65' height = "45" bordercolor="#000000" class="ParagraphHeader">Gender</th>
    <th width = '62' height = "45" bordercolor="#000000" class="ParagraphHeader">Initials</th>
    <th width = '120' height = "45" bordercolor="#000000" class="ParagraphHeader">First name</th>
    <th width = '165' height = "45" bordercolor="#000000" class="ParagraphHeader">Surname</th>
    <th width = '101' height = "45" bordercolor="#000000" class="ParagraphHeader">ID number</th>
    <th width = '92' height = "45" bordercolor="#000000" class="ParagraphHeader">Date registered</th>
    <th width = '110' height = "45" bordercolor="#000000" class="ParagraphHeader">Date resigned</th>
</tr>
<?php
    $sql= ("SELECT * FROM WEB_DEPENDANT 
    WHERE MEM_NO = '$selMember'
    AND MEM_DEPNO = '$selDependant'");

    $result=$dh->query($sql) or die ("Error in: $sql1");
    $array=$result->fetchALL(PDO::FETCH_ASSOC);
    $count=count($array);
    echo "Member:" . $selMember;
    echo "Dependant:" . $selDependant;

    if($count==1)
    {                        
        echo "<tr>
            <td width = '83' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_DEPNO]."</td>
            <td width = '63' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][SEX_CODE]."</td>
            <td width = '65' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_INITIALS]."</td>
            <td width = '134' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_FIRSTNAME]."</td>
            <td width = '182' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_SURNAME]."</td>
            <td width = '95' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_ID]."</td>
            <td width = '114' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_DATEREGISTERED]."</td>
            <td width = '118' height = '25' class='ParagraphDetail' bordercolor='#000000'>".$array[0][MEM_DATERESIGNED]."</td>
        </tr>"; 
        }
        else {
            echo "No record of Member found!";
        }
    ?>  
</table>

When I echo the 'Member', it displays correctly from the input box I have at the top of the page as it posts correctly, but the 'select' for the Dependant, does not post through to my variable and thus I cannot use it, but for some weird reason, my grid does populate with the Member's details no matter what option I select from the 'select'.

I am already displaying the Members details above this code, so this grid only needs to display the Dependant selected from the 'select'.

Upvotes: 0

Views: 801

Answers (2)

Saedawke
Saedawke

Reputation: 471

Put quotes around the option value:

echo "<option value='{$a}'>".$array[$x][MEM_DEPNO]."</option>";

Upvotes: 0

Gerald Schneider
Gerald Schneider

Reputation: 17797

Two errors:

$selectDependant = isset($_POST['DependantList']);

isset() only checks if the variable is set and returns true or false. If you want the content of $_POST['DependantList'] in $selectDependant change it to:

$selectDependant = $_POST['DependantList'];

Second:

AND MEM_DEPNO = '$selDependant'");

You never define $selDependant. Most probably it should be:

AND MEM_DEPNO = '$selectDependant'");

Upvotes: 1

Related Questions