Skip to content
Advertisement

How to display a selected option in `drop-down menu` in a PHP update form?

I have to display a selected option in the drop down list preview, but currently when extracting the value from the database it only shows one option and not the other from the database.

Is there a way to show the actual drop down list option when extracting the known value from the database (PgAdmin)(All code is either HTML,CSS or PHP)

Basically, I want it show the value in the database as the selected option in the Drop-Down menu, as you probably see in the below code I have tried but I am not too sure how to progress further from this point. Also feel free to correct me on errors within my code. I am aware it is a bit to digest but hopefully this will be answered soon 🙂

<?php
   $conn = pg_connect("host=localhost port=5432 dbname=cafe user=postgres password=password");
   if(count($_POST)>0) {
      pg_query($conn,"UPDATE cakesweetorder set id='$_POST[id]', 
         firstname='$_POST[firstname]',
         surname='$_POST[surname]',streetno='$_POST[streetno]', 
         streetname='$_POST[streetname]', 
         state = '$_POST[state]', suburb='$_POST[suburb]', 
         postcode='$_POST[postcode]', email='$_POST[email]', 
         mobno='$_POST[mobno]',colour='$_POST[colour]', 
         toping='$_POST[toping]', 
         chocosentence='$_POST[chocosentence]', 
         quantity='$_POST[quantity]', date='$_POST[date]', 
         file='$_POST[file]',
         tea='$_POST[tea]' 
      WHERE id='$_POST[id]' ");
      $message = "Record Modified Successfully";
   }
   $result = pg_query($conn,"SELECT * FROM cakesweetorder WHERE id='" . $_GET['id'] . "'");
   $row= pg_fetch_array($result);
?>
<html>
<head>
<?php 
    include_once 'head.php';
?>

<p>
<div class='text-box'>
    <div class='wrapper-glass'>
        <div class='shape-1'></div>
        <div class='shape-2'></div>
        <div class='shape-3'></div>
        <div class='shape-4'></div>
        <div class='shape-5'></div>
        <div class='shape-6'></div>
        <div class='shape-7'></div>
        <div class='shape-8'></div>
        <div class='shape-9'></div>
        <div class='shape-10'></div>
        <div class='container-glass'>  

<title>Cafe Database</title>
</head>
<body>
<form name="frmUser" method="post" action="">
<div><?php if(isset($message)) { echo $message; } 

?>    
</div>
ID: <br>
<input type="hidden" name="id" class="txtField" value="<?php echo $row['id']; ?>">
<input readonly='readonly' name="id"  value="<?php echo $row['id']; ?>">
<br>

<legend>Name</legend>

Firstname:<div class="bar"><div class="text-bar"><input type="text" class="form-control" name="firstname" id="name" placeholder="Firstname..." value="<?php echo $row['firstname']; ?>"required/></div></div>
Surname:<div class="bar"><div class="text-bar2"><input type="text" class="form-control" name="surname" id="name" placeholder="Surname..."  value="<?php echo $row['surname']; ?>"required/></div></div>


<legend>Address For Delivery</legend>
 Street No: <div class="bar"><div class="text-bar3"><input type="number" id="text-bar" class="form-control" name="streetno" placeholder="Street No..."  value="<?php echo $row['streetno']; ?>"required/><br/></div></div>
 Street: <div class="bar"><div class="text-bar4"><input type="text" id="text-bar" class="form-control" name="streetname" placeholder="Street Name..."  value="<?php echo $row['streetname']; ?>"required/><br/></div></div>
 State:  
 
 State:  
     <div class="bar"><div class="text-bar5"><select id="state" class="form-control" name="state" value="<?php echo $row['state']; ?>" name="state"></div></div>
 <optgroup label="States">
    <option value="NSW">NSW</option>
    <option value="QLD">QLD</option>
    <option value="VIC">VIC</option>
    <option value="SA">SA</option>
    <option value="NT">NT</option>
    <option value="TAS">TAS</option>
  </select><br></div></div>
 Suburb: <div class="bar"><div class="text-bar6"><input type="text" class="form-control" name="suburb" placeholder="Suburb..."  value="<?php echo $row['suburb']; ?>" required/><br/></div></div>
 Postcode: <div class="bar"><div class="text-bar7"><input type="number" class="form-control" name="postcode" placeholder="Postcode..."   value="<?php echo $row['postcode']; ?>" required/><br/></div></div>

<legend>Personal Details</legend>
 Email:<div class="bar"><div class="text-bar8"> <input type="email" class="form-control" name="email" placeholder="Email..."  value="<?php echo $row['email']; ?>" required/><br/></div></div>
 Phone Number: <div class="bar"><div class="text-bar9"><input type="number" class="form-control" name="mobno" placeholder="Phone Number..."   value="<?php echo $row['mobno']; ?>"required/><br/></div></div>

 Colour of Cake: <div class="input-color-container">
 <div class="bar"><div class="text-bar11"><input id="input-color" name="colour" class="input-color" type="color"  value="<?php echo $row['colour']; ?>"></div></div>
</div>
<label class="input-color-label" for="input-color">
</label>
 Topings: <br><br>
 <label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = 'Vanilla') echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark"></span><span class="checkbox-txt"> Vanilla</span><br/></br></label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = 'Chocolate') echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark"></span><span class="checkbox-txt" >Chocolate</span><br/></br></label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = 'Caramel') echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark"></span><span class="checkbox-txt" >Caramel</span><br/></br></label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = "Strawberry") echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark"></span><span class="checkbox-txt" >Strawberry</span><br/></br></label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = "M&M's") echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark"></span><span class="checkbox-txt" >M&M's</span><br/></br></label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = "Oreo") echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark"></span><span class="checkbox-txt" >Oreo</span><br/></br></label>
<label class="checkbox-container">
<input type="checkbox" name="toping" <?php if(isset($_POST['toping']) && $_POST['toping'] = "Meringue") echo "checked" ?> value="<?php echo $row['toping']; ?>"><span class="checkmark "></span><span class="checkbox-txt" >Meringue</span><br/></label>

 Chocolate Sentences: <div class="bar"><div class="text-bar12"><input type="text" class="form-control" name="chocosentence" placeholder="Chocolate sentence..." value="<?php echo $row['chocosentence']; ?>"/><br/></div></div>
 Quantity of Cakes or Sweets: <div class="bar"><div class="text-bar13"><input type="number" class="form-control" min="1" value="1" max="10" name="quantity" placeholder="Quantity..."  value="<?php echo $row['quantity']; ?>" required/><br/></div></div>
 Complimentary Tea: <br>
    <div class="container">
        <input type="radio" name="tea" value="true" <?php if(isset($_POST['tea']) && $_POST['tea'] = 't') echo "checked" ?>value="<?php echo $row['tea']; ?>">
        <label for="tea" class="label1">
            <span>YES</span>
        </label>
        <input type="radio" name="tea" <?php if(isset($_POST['tea']) && $_POST['tea'] = 'f') echo "checked" ?>value="<?php echo $row['tea']; ?>">
        <label for="tea" class="label2">    
            <span>NO</span>
        </label>
    </div>
<br><br><br><br><br>
<legend>Miscellaneos</legend>
 Date Of Delivery: <div class="bar"><div class="text-bar14"><input type="date" class="form-control" name="date" placeholder="Date of Delivery..."  value="<?php echo $row['date']; ?>" required/><br/><br><br></div></div>
 Custom Order with Instructions: <input type="file" class="form-control" name="file" placeholder="Custom Order..." /><br/>
OR Type in Instructions:
<div class="container3">
<div class="bar"><div class="text-bar15"><textarea id="my-text" name="file" rows="5" placeholder="Type Custom Order Here.." value="<?php echo $row['file']; ?>" ></textarea>
        <p id="result"></p>
    </div></div></div>
<br>
<input type="submit" name="submit"  class='signup-btn' value="Submit" class="button">

</form>
</body>
</html>TAS</option>
      </select><br></div></div>’
    

I have tried something like this but it didn’t really work: (Below), Its a very vague attempt but as I said before it doesn’t work I can’t really figure it out

    `<?php
    $db = pg_connect("host=localhost port=5432 dbname=SRIS user=postgres password=password");
    $result = pg_query($db, "SELECT * FROM customer where customer_id = '$_POST[customer_id]'");
    $row = pg_fetch_assoc($result);
     
    // isset is set to submit in line 14
    if (isset($_POST['submit']))
    {
     
    if ($row[pref_gender] == 'M') { 
    echo "</br>"; 
    $statement1 = "<option value='M' selected>Male</option>
    <option value='F'>Female</option>";
    }        
     
    if ($row[pref_gender] == 'F') { 
    echo "</br>"; 
    $statement1 = "<option value='F' selected>Female</option>
    <option value='M'>Male</option>";
    }        
     
    // retrieves one row of data from the table and displays the fields in the form
    // Book id is a primary key so we can't edit it        
    echo "<p>
     
    <form name='update' action='enter_primarykey.php' method='POST' >
    <li>Customer Id</li><li><input type='text' name='customer_id_updated' value='$row[customer_id]' /></li>
     
    <li>Firstname</li><li><input type='text' name='firstname_updated' value='$row[firstname]' /></li>
    <li>Surname</li><li><input type='text' name='surname updated' value='$row[surname]' /></li> 
     
    <li>Gender</li>
    <li>
    <select name='pref_gender_updated'>
    $statement1                
    </select>
    </li>
     
    <li><input type='submit' name='new' /></li>  </form>
    </p>";
    }
     
    // isset is set to new in line 37
    // uses the primary key to save all the values back into the database when submit is clicked
    if (isset($_POST['new']))
    {
    $result1 = pg_query($db, "UPDATE customer SET firstname = '$_POST[firstname_updated]', 
    surname = '$_POST[surname_updated]', pref_gender = '$_POST[pref_gender_updated]' WHERE customer_id= '$_POST[customer_id_updated]'”);`

Below is what I want to change. When displaying the extracted data from the table I want it to show ‘QLD’ instead it shows the default ‘NSW’, how do I adapt the code to do this?

`State:  
         <div class="bar"><div class="text-bar5"><select id="state" class="form-control" name="state" value="<?php echo $row['state']; ?>" name="state"></div></div>
     <optgroup label="States">
        <option value="NSW">NSW</option>
        <option value="QLD">QLD</option>
        <option value="VIC">VIC</option>
        <option value="SA">SA</option>
        <option value="NT">NT</option>
        <option value="TAS">TAS</option>
      </select><br></div></div>`

Advertisement

Answer

It will look somewhat like this:

PHP

//Do Query (In your case, we can use $row['states']

HTML

`State:  
<div class="bar">
  <div class="text-bar5">
     <select id="state" class="form-control" name="state" name="state"></div></div>
       <optgroup label="States">
       <option value="NSW" <?php if($row['states'] == 'NSW')echo 'selected'; ?> >NSW</option>
       <option value="QLD" <?php if($row['states'] == 'QLD')echo 'selected'; ?> >QLD</option>
<!-- VIC will be selected -->
       <option value="VIC" <?php if($row['states'] == 'VIC')echo 'selected'; ?> >VIC</option>
<!-- VIC will be selected -->
       <option value="SA" <?php if($row['states'] == 'SA')echo 'selected'; ?> >SA</option>
       <option value="NT" <?php if($row['states'] == 'NT')echo 'selected'; ?> >NT</option>
       <option value="TAS" <?php if($row['states'] == 'TAS')echo 'selected'; ?> >TAS</option>
     </select><br>
  </div>
</div>`
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement