Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm not sure if this is even possible but I would like to modify script 10.3.  I have created a simple running log where users can input the details of their run using a form that includes text input, radio buttons and drop-down menus.  Is it possible to use the same or similar form to edit/update their records?  I have no problem retrieving the values for the text boxes but I don't know how the form is supposed to show the values from the drop down menus or to "select" the appropriate radio button.

 

Any help would be appreciated.  Thanks!

Link to post
Share on other sites
For select (drop-down list) you can preset a selection using "selected":



<select name="colours">
  <option value="blue" selected>blue</option>
</select>


For radio buttons you can preset a value using "checked":



<input type="radio" name="status" value="on" checked>On


So you need to compare the value you have stored, against the value of each option and then include selected or checked in the html for that element.

Link to post
Share on other sites

Hi, you can certainly use a modified version of your form to pre-fill it with existing data, normally retrieved from the database.  Then the user can modify the required data via the form and the program can then use that modified data to update the database. It is a technique that works well.

 

Here's an example (from within a form):

 

 

<label class="maintext_right v_align_top" for="new_woa_name">Work of Art Name: </label><input name="new_woa_name" type="text" size="60" id="new_woa_name" value="<?php echo $old_woa_name; ?>">
          <small><small><i>  Change if needed - max length 60 characters.</i></small></small>
    <div class="small_lineheight clear_left"></div>

 

$old_woa_name is from an MySQL select where that datum is retrieved from a database record.  The other code is just formatting and line spacing.

 

Hope it helps.

 

Cheers from Oz.

 

The programming technique is PHP redux as per Larry's book examples.

Link to post
Share on other sites

Here is a small part of what I am trying to do.  When a user wants to edit their running log entries, I would like this select menu to appear with their previously selected value already selected.  Like I mentioned earlier, I don't have any problem recalling text values using value="' . $row[0] . '".  My issue is trying to figure out how to do it with select menus and/or radio menus.

         	<label for="select">Primary Terrain:</label>
		<select class="form-control" id="terrain" name="terrain_id">
            	   <option select>Select One</option>
            	   <option value="1">Asphalt (Road)</option>
            	   <option value="2">Concrete (Sidewalk)
            	   <option value="3">Crushed Gravel</option>
            	   <option value="4">Grass</option>
            	   <option value="5">Rubber Track</option>
            	   <option value="6">Trails</option>
          	</select>
Link to post
Share on other sites

OK, here's an example where I've retrieved a day number and a month from a database record:

.............
$days = range (1, 31);
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
...........
 echo '<select name="day" id="day">';
    echo "<option value=$t_day>$t_day</option>";
    foreach ($days as $value)
     {
    echo "<option value=\"$value\">$value</option>";
   }
echo '</select> ';
 echo '<select name="month" id="month">';
    echo "<option value=$t_month>$t_month_alpha</option>";
    foreach ($months as $key => $value)
     {
    echo "<option value=\"$key\">$value</option>";
   }
    echo '</select> ';

$t_day is the day number retrieved from the database record, i.e., the day number previously stored from a previous transaction. The 'foreach' then just lists the other day numbers that the user could then select from if they wish to change the day number.  After a new day number has been selected (if indeed it was changed) then the database record is updated.

 

Also shown is the PHP for the month drop-down box. $t_month is the numeric month number from the database record retrieval and $t_month_alpha is the spelled-out month from the $months array.

 

The code shown is PHP echoing HTML.

 

When a day-number is selected, I do an in-form validity check via jQuery that that day number is valid for that month - logic not shown.

 

Hope it helps.

 

Cheers from Oz.

Link to post
Share on other sites
$dbValue = the value stored in the database.

<select name="colours">
  <option value="blue" <?php if(isset($dbValue) && $dbValue == "blue") echo "selected"; ?>>blue</option>
</select>

You can use the same principle to pre-populate radio buttons.

Link to post
Share on other sites

Further to rob's post, here's an example of setting a radio button depending on a value from a database record.

 

$black = "";
$blue = "";
$red = "";
$green = "";
....................
if ($colourx == "green")
    $green = 'checked="checked"';
elseif ($colourx == "blue")
    $blue = 'checked="checked"';
elseif ($colourx == "red")
    $red = 'checked="checked"';
else
    $black = 'checked="checked"'; // the default
....................
echo <<<EOT
<label class="maintext_right v_align_top" for="test">$text_1 </label><textarea name="$id" type="text" rows="5" cols="75" id="$id">$text_2</textarea>
<span class='v_align_top maintext'> Colour: 
<input class="para_radio_inline" type="radio" name="$name" id="black" value="black" $black /><span class="maintext">Black (Default)  </span>
<input class="para_radio_inline" type="radio" name="$name" id="red" value="red" $red /><span class="maintext">Red  </span>
<input class="para_radio_inline" type="radio" name="$name" id="blue" value="blue" $blue /><span class="maintext">Blue  </span>
<input class="para_radio_inline" type="radio" name="$name" id="green" value="green" $green /><span class="maintext">Green</span>
          		</span>
EOT;
where $colourx is from the retrieved database record.

 

Hope it helps.

 

Cheers from Oz.

  • Upvote 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...