Jump to content

Display other data based on dropdown selection


JayPawar
Go to solution Solved by Strider64,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

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?  :confused:

Edited by Strider64
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.