Click here to Skip to main content
15,903,752 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Unable to display or filter database record using dropdownlist.

im having trouble with this line of code,
PHP
$value = $_POST['cbodept'];


but if use textbox or something like
PHP
$value = $_POST['txtsearcbox']; 
$value = "sampleText";
the displays and filter record perfectly.

What I have tried:

My Dropdowns values.[This is working!]

<select class="custom-select" name="cbodept" id="cbodept-id" style="width: 50%;" >
    <option value="" selected> - SELECT DEPARTMENT - </option>
    <?php // Fetch|Load records to dropdownlist
        include "../php-file/db-config.php";
        $sql = "SELECT  DISTINCT deptcode_fk FROM tblcourse ORDER BY deptcode_fk ASC";
        $stmt = $conn -> prepare($sql);
        $stmt -> execute();
        $result = $stmt -> get_result();
        while($row = $result -> fetch_assoc()){
            #<option value="">- display -</option>
            echo " <option value=' ". $row['deptcode_fk'] ." '> " .$row['deptcode_fk'] ." </option>" ;
        }
        // Close
        $stmt -> close();
        $conn -> close(); 
    ?>
</select> 



after form submit. [Not Working here]

PHP
<pre><?php  //DISPLAY RECORD
    if(isset($_POST['btnsearch'])){
        $value = $_POST['cbodept']; #this line not working
        include "../php-file/db-config.php";
        $sql = "SELECT * FROM tblcourse WHERE deptcode_fk=? ORDER BY deptcode_fk ASC";
        $stmt = $conn -> prepare($sql);
        $stmt -> bind_param("s",$value);
        // Execute and fetch
        $stmt -> execute();
        $result = $stmt -> get_result();
        // Echo
        while($row = $result -> fetch_assoc()){
            echo "<tr >
                <td > ".$row['crscode']." </td>
                <td > ".$row['deptcode_fk']." </td>
                <td > ".$row['crsname']." </td>
                <td > ".$row['duration']." </td>
                <td > ".$row['crsstat']." </td>
                <td class='action-buttons'> <a href='#updDept.php?id=".$row['crscode']."' value='Edit'> Edit </a> </td>
                <td class='action-buttons'> <a onClick=\"javascript: return confirm('Please confirm deletion of:  ".$row['crscode']." ?');\" href='#delDept.php?id=".$row['crscode']."' value='Del'> Del</a> </td>
                <td hidden class='action-buttons'> <a href='#printDept.php?id=".$row['crscode']."' target ='_self' value='N/A'> N/A</a> </td>                                   
            </tr>";
        }
        // Close
        $stmt -> close();
        $conn -> close(); 
    }
?>
Posted

1 solution

I think the problem lies here

echo "<option value='".trim($row['deptcode_fk'])."'>".$row['deptcode_fk']."</option>";


there is a space before and after the value is concatenated to value property.

If the problem persists, try printing the selected value on html and see whats the problem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900