Jump to content

Recommended Posts

I'm trying to pull up an identical form after submission by using its "id" and ECHO for the form lines.

 

Question: What is the correct approach for ECHOing an HTML dropdown that is written as:

<select name='chixcutlet' value='' >

        <option value='0.00' selected>  --- </option>

        <option value='1.00'>  1  </option>

        <option value='2.00'>  2  </option>

        <option value='3.00'>  3  </option>

</select>

 

I want it to get the info from the database so that the option that is saved in the database will be new default when the page is loaded.

 

If I don't change it to the previous info it will update the database with the default option rather then the actual option.

Link to comment
https://forums.phpfreaks.com/topic/257560-retrieving-an-html-dropdown-select/
Share on other sites

  • Replies 73
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Something like...

$options = array(
'0.00' => '---',
'1.00' => '1',
'2.00' => '2',
'3.00' => '3'
);

$selected = '1.00';

foreach($options as $key => $val)
{
echo '<option value="' . $key . '" ' . ($key == $selected ? 'selected="selected"' : '') . '>' . $val . '</option>';
}

The way I normally tackle this (with psuedo code and ignoring the rest of the code is

<select name='chixcutlet' >
<option value='$valueoptions' 
   <?php
      if ($valueoptions =  $savedvalue)
      {echo 'selected '}>  $name  </option>
     ?>
</select>

if you are using something like dreamweaver you can create a recordset but if not your going to have to make a connection something like this:

 

<?php
require_once('yourConnection.php');
$query1 = mysql_query("SELECT* FROM your_table ORDER BY id);
$info = mysql_fetch_array($query1);
?>



<select name='chixcutlet' value='' >
        <option value='0.00' selected>  --- </option>
               <?php do{ ?>
        <option value='<?php echo $info['value_from_db'] ;?>'><?php echo $info['name_from_db'] ;?></option>
                <?php } while ($info = mysql_fetch_assoc($info)); ?>
</select>

 

I dont know if thats completely going to work as is...but basically you need to display content as you would any other time but one as the value and one as whatever you want people to choose from and repeat the region for as many records as you have.

I've got this in my query:

 

$result = mysql_query("SELECT * FROM pass WHERE id = 4 ")

or die(mysql_error());

 

$row = mysql_fetch_array( $result );

 

And inside my form:

echo "cutlets  <select name='chixcutlet' value=''>  ";

echo "        <option value='0.00' selected>  --- </option>";

echo "        <option value='1.00'>  1  </option>";

echo "        <option value='2.00'>  2  </option>";

echo "        <option value='3.00'>  3  </option>";

echo " </select>";

 

My form comes up fine, but no values are extracted from the DB.

 

I want it to get the info from the database so that the option that is saved in the database will be new default when the page is loaded.

 

If I don't change it to the previous info it will update the database with the default option rather then the actual option.

 

 

Then you should build the select field dynamically, so you can check the value stored in the database against the value of the <option> tag, and echo 'selected="selected"' when they match. Otherwise, you'd need to incorporate the logic into every single <option> tag as you manually write the html.

 

I'll do it manually (as long as it works) but I don't know what to write.

How can I do it dynamically if every item has a different field name and I need the SUBMITTED value for EVERY item to become the NEW DEFAULT with the intention of the form being used to update data.

 

example: customer ordered 1 chicken cutlet and now wants 3 instead.  I want to view the original order, verify that ONE was ordered, change the selection to 3, and UPDATe the record.

 

If this cannot be handled in one page, I will gladly do it it two steps to avoid complications

I'll do it manually (as long as it works) but I don't know what to write.

How can I do it dynamically if every item has a different field name and I need the SUBMITTED value for EVERY item to become the NEW DEFAULT with the intention of the form being used to update data.

 

example: customer ordered 1 chicken cutlet and now wants 3 instead.  I want to view the original order, verify that ONE was ordered, change the selection to 3, and UPDATe the record.

 

If this cannot be handled in one page, I will gladly do it it two steps to avoid complications

 

I showed you how, right here:

Something like...

$options = array(
'0.00' => '---',
'1.00' => '1',
'2.00' => '2',
'3.00' => '3'
);

$selected = '1.00';

foreach($options as $key => $val)
{
echo '<option value="' . $key . '" ' . ($key == $selected ? 'selected="selected"' : '') . '>' . $val . '</option>';
}

This looks like it's establishing a PHP dropdown menu, not takiing the data from my DB, or placing it in my HTML dropdown (the same one that submitted it) from my HTML form.

 

I can change the form to PHP, but I prefer the HTML dropdown for design purposes.

 

 

This looks like it's establishing a PHP dropdown menu, not takiing the data from my DB, or placing it in my HTML dropdown (the same one that submitted it) from my HTML form.

 

I can change the form to PHP, but I prefer the HTML dropdown for design purposes.

 

 

 

You cannot "change the form to PHP". PHP outputs HTML, you end up with the same HTML either way.

The point of this being that this code:

$values = range(0, 10);
echo "<select name=\"my_field\">\n";
foreach($values as $v) {
echo "<option value=\"$v\">$v</option>\n";
}
echo "</select>\n";

 

Generates this output:

<select name="my_field">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

 

So the end result is the same output, with much less typing, and it's more easily maintainable. If you wanted to make it 20 options instead of 10, you would only need to change one character; in range(0,10), 10 would become 20.

OKAY! Now I'm beginning to understand how PHP could have made my life easier.  However, I am WAY beyond that.  All the code has already been handwritten and the form has already submitted data INTO the DB.

 

Now I have generated the identical form that comes up when I make a request for data FROM the DB.

 

My difficulty at this point is that no VALUES are being placed in the form.

 

ALL the data is generated INTo the database with HTML dropdown menus.

 

I want it to get the info from the database so that the option that is saved in the database will be new default when the page is loaded, and will SHOW in the form.

 

If I don't change it to the previous info it will update the database with the default option rather then the actual option.

 

 

There are MANY, but let's work with CUTLETS.  (i'd list them all but it's lunchtime and we'd all get hungry.. LOL)

By the way, I want to thank you all for your patience.  I do appreciate it.

[Can you comment on MargateSteve's code.  It seems short and concise, but seems to have an unknown variable.]

Right. Well, it would still be easier to rewrite the form. This is somewhat simplified, but here's why:

 

With your database query already done:

$query = "SELECT wings FROM table WHERE order = $order_num";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);

 

By adding one line, and editing another, the field will select the option that was stored in the database.

$values = range(0, 10);
echo "<select name=\"my_field\">\n";
foreach($values as $v) {
     $selected = $array['wings'] == $v ? 'selected="selected"' : ''; // Compare value of the option to value from DB. Select the option if they're equal.
     echo "<option value=\"$v\" $selected>$v</option>\n"; // ADDED $selected
}
echo "</select>\n";

 

If it was hard coded in the html, it would need to look like this:

<select name="my_field">
<option value="0" <?php echo $array['wings'] == 0 ? 'selected="selected"' : '' ?>>0</option>
<option value="1" <?php echo $array['wings'] == 1 ? 'selected="selected"' : '' ?>>1</option>
<option value="2" <?php echo $array['wings'] == 2 ? 'selected="selected"' : '' ?>>2</option>
<option value="3" <?php echo $array['wings'] == 3 ? 'selected="selected"' : '' ?>>3</option>
<option value="4" <?php echo $array['wings'] == 4 ? 'selected="selected"' : '' ?>>4</option>
<option value="5" <?php echo $array['wings'] == 5 ? 'selected="selected"' : '' ?>>5</option>
<option value="6" <?php echo $array['wings'] == 6 ? 'selected="selected"' : '' ?>>6</option>
<option value="7" <?php echo $array['wings'] == 7 ? 'selected="selected"' : '' ?>>7</option>
<option value="8" <?php echo $array['wings'] == 8 ? 'selected="selected"' : '' ?>>8</option>
<option value="9" <?php echo $array['wings'] == 9 ? 'selected="selected"' : '' ?>>9</option>
<option value="10" <?php echo $array['wings'] == 10 ? 'selected="selected"' : '' ?>>10</option>
</select>

 

Edited to fix syntax error.

Pasted it in with my code from Reply #4.  I have a feeling I'm duplicating ECHOs because I received error message for first line of modified code. 

 

Also, I need to ensure that unselected items maintain their default.  Is there a way to incorporate that?

There are MANY, but let's work with CUTLETS.

 

Then by modifying my first response, this should work:

$result = mysql_query("SELECT * FROM pass WHERE id = 4 ");

$row = mysql_fetch_assoc($result);

$selected = $row['cutlets'];

$options = array(
'0.00' => '---',
'1.00' => '1',
'2.00' => '2',
'3.00' => '3'
);

foreach($options as $key => $val)
{
echo '<option value="' . $key . '" ' . ($key == $selected ? 'selected="selected"' : '') . '>' . $val . '</option>';
}

So am I abandoning Pikachu??

 

I modified:

echo " <option value='1.00'>1  <?php echo $array['roastturkey'] == 0 ? 'selected="selected"' : ''>0</option>";

 

To this

echo  "<option value='1.00'  $array['roastturkey'] == 0 ? 'selected="selected"' : ''>0</option>";

 

BUT NOT WORKING!

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.