JayPawar Posted March 18, 2014 Share Posted March 18, 2014 hello i have a dropdown box where i have populated values from the sql database. no when i select a value from the drop dopwn, i want to display all the records related to that value under the dropdown. here is my code <?php $select=FALSE; require_once ("configFile.php"); $result=mysql_query("SELECT Registration_No FROM vehicle_master "); if($result == FALSE) { die(mysql_error()); // TODO: better error handling } //populate dropdown with vehicles //echo "<label>Select vehicle number : </label>"; //echo "<select name='vehicles'>"; //while ($row = mysql_fetch_array($result)) //{ // echo "<option value='numberPlate'>" . $row['Registration_No'] . "</option>"; //} //echo "</select>"; //echo "<br><br>"; // $select=isset($_POST['vehicles']); ?> <form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> "> <?php echo "<select name='select'>"; $list=mysql_query("SELECT * from vehicle_master"); while($row_list=mysql_fetch_assoc($list)) { ?> <option value="<?php echo $row_list['Registration_No']; ?>" <?php if($row_list['Registration_No']==$select) { echo "selected"; } ?>> <?php echo $row_list['Registration_No']; ?> </option> <?php } ?> </select> <input type="submit" name="Submit" value="select" /> </form> <br> <?php if (isset($_POST['SELECT'])) { $Registration_Nos=mysql_real_escape_string($_POST['SELECT']); $result=mysql_query("SELECT * FROM vehicle_master WHERE Registration_No='".$Registration_Nos."'"); while ($row=mysql_fetch_assoc($result)) { echo "".$row['Emergency_Contact_1'].""; } }?>. can anyone tell me where am i going wrong ? control.php Quote Link to comment https://forums.phpfreaks.com/topic/287042-display-other-data-based-on-dropdown-selection/ Share on other sites More sharing options...
gristoi Posted March 18, 2014 Share Posted March 18, 2014 you may want to look at using jquery and ajax to achieve what you want Quote Link to comment https://forums.phpfreaks.com/topic/287042-display-other-data-based-on-dropdown-selection/#findComment-1472956 Share on other sites More sharing options...
Solution Strider64 Posted March 18, 2014 Solution Share Posted March 18, 2014 (edited) It's a little tricky using just straight PHP, but it can be done function displayDir($pdo, $name) { try { // Fetch the images: $query = 'Select id, thumbnail, picName, subDirectory, user_name FROM pictures WHERE subDirectory=:subDirectory ORDER BY id ASC'; $result = $pdo->prepare($query); $result->execute(array(':subDirectory' => $name)); if ($result && $result->rowCount() > 0) { // Check that rows were returned: $result->setFetchMode(PDO::FETCH_CLASS, 'StoredPictures'); // Arm the image class by fetching it: } else { // Problem! throw new Exception('No content is available to be viewed at this time'); } } catch (Exception $e) { // Catch generic exceptions } return $result; } /* Display images & thumbnails when user selects directory */ if (isset($_POST['imageDir']) && $_POST['imageDir'] == 'selected') { $_SESSION['name'] = $_POST['childName']; // Assign category to path: $name = $_SESSION['name']; $resultDir = displayDir($pdo, $name); } <form class="selectDir" id="selectDir" action="imageGallery.php" method="post"> <input type="hidden" name="imageDir" value="selected"> <label class="mySelect" for="mySelection">Choose Directory</label> <select class="mySelection" id="mySelection" name="childName"> <?php echo '<option value="' . $name . '">' . ucfirst($name) . '</option>' ?> <option value="ava">Ava</option> <option value="cammi">Cammi</option> <option value="carter">Carter</option> <option value="grant">Grant</option> <option value="jayreed">JayReed</option> <option value="nolan">Nolan</option> </select> <input class="submitCat" type="submit" name="submitImages" value="Enter"> </form> <div class="container photogallery"> <div class="row bg-header-part3"></div> <ul class="row"> <?php $x = 1; if (isset($resultDir)) { while ($pictures = $resultDir->fetch()) { echo '<li><a class="links" href="' . htmlspecialchars($pictures->getPicName()) . '" ><img data-pic="' . $x . '" class="thumbnails" src="' . htmlspecialchars($pictures->getThumbnail()) . '" alt="Thumbnails"></a></li>'; $x += 1; } } ?> </ul> </div> Though I do use jquery and ajax for the rest, but this gives the web page Graceful Degradation for people who disable JavaScript. Getting back to the script, the main important thing I want to show you is the form itself, I don't propagate the whole form in php (you could though) I just do the part where it's needed. Maybe just looking over the form and maybe glancing over the first script might help you out? Edited March 18, 2014 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/287042-display-other-data-based-on-dropdown-selection/#findComment-1472964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.