conditional php in html form - a question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noclist
Forum Newbie
Posts: 7
Joined: Mon Jun 29, 2009 11:12 am

conditional php in html form - a question

Post by noclist »

Hi, I'm working on a site for a graduate class and I'm having one small problem with it. I want to show specific information about records in a database in an HTML form on a page.

This was fairly easy to accomplish to do with text boxes as I just set the value of the box to <?php echo($item)?>
However, I also have a drop down select box with 3 option values and I'm having trouble getting the default option set as what identifies the record in my database.

Ex:

Code: Select all

 
<select name = "category">
    <option value = ""></option>
    <option value = "one">1</option>
    <option value = "two">2</option>
    <option value = "three">3</option>
Is there way to set this so the default option value would change, depending on the database record it is displaying?
Last edited by Benjamin on Mon Jun 29, 2009 11:34 pm, edited 1 time in total.
Reason: Added [code=html] tags.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: conditional php in html form - a question

Post by Eric! »

Just echo the value you want from your database

In the area of html where you want the list

Code: Select all

<? //get data
// $value=value to set
// $text=text for value to display
     echo "<OPTION VALUE=\"".$value."\" SELECTED>".$text."</OPTION>";
?>
You can remove the 'SELECTED' bit of text if you want
noclist
Forum Newbie
Posts: 7
Joined: Mon Jun 29, 2009 11:12 am

Re: conditional php in html form - a question

Post by noclist »

I can use that code to display the value in the select box?
Here is the exact select box I have:

Code: Select all

<tr><th>Category:</th><td><select name = "cat">
    <option value = ""></option>
    <option value = "blu" selected>Blu-ray</option>
    <option value = "live">Live Action</option>
    <option value = "anim">Animation</option>
</select></td></tr>
I just want the default option to be the one that corresponds with a record.
If a record happened to be category Blu-Ray, I would want that option selected in my box, etc.
Last edited by Benjamin on Mon Jun 29, 2009 11:34 pm, edited 1 time in total.
Reason: Changed code type from text to html.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: conditional php in html form - a question

Post by Eric! »

Something like this

Code: Select all

<? // php code looks in database for the selected value
// then lets say user has blu-ray
// set $blu=1;
// don't set the others anywhere else  in your script
// only set them if you want that item selected
?>
<tr><th>Category:</th><td><select name = "cat">
      <option value = ""></option>
  .     <option value = "blu" <? if(isset($blu)) echo 'selected';?>>Blu-ray</option>
  .     <option value = "live" <? if(isset($live)) echo 'selected';?>>Live Action</option>
      <option value = "anim" <? if(isset($anim)) echo 'selected';?>>Animation</option>
    </select></td></tr>
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: conditional php in html form - a question

Post by BornForCode »

And now the untested solution is (i assume you have the records somewhere as you should do if you don't have them, because if you want to add/remove an item you will have to edit some html instead of adding/delete a new element in the array):

Code: Select all

 
// with no key, straight values 
foreach($set as $value) {
    echo '<option value="'.$value.'" '.(($value == $dbValue) ? ' selected>':'>').$value.'</option>';
}
 
//with a key
foreach($set as $key=>$value) {
    echo '<option value="'.$key.'" '.(($key == $dbValue) ? ' selected>':'>').$value.'</option>';
}
 
 
Last edited by Benjamin on Mon Jun 29, 2009 11:35 pm, edited 3 times in total.
Reason: Changed code type from text to php.
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: conditional php in html form - a question

Post by danielrs1 »

Or easier:

Code: Select all

 
<tr><th>Category:</th><td><select name = "cat">
  <option value = ""<?php if($option_from_db=='')echo ' selected'; ?>></option>
  <option value = "blu"<?php if($option_from_db=='blu')echo ' selected'; ?>>Blu-ray</option>
  <option value = "live"<?php if($option_from_db=='live')echo ' selected'; ?>>Live Action</option>
  <option value = "anim"<?php if($option_from_db=='anim')echo ' selected'; ?>>Animation</option>
  </select></td></tr>
 
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: conditional php in html form - a question

Post by BornForCode »

Pick mine, pick mine :drunk:
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: conditional php in html form - a question

Post by danielrs1 »

lol
I'd use yours :oops:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: conditional php in html form - a question

Post by Benjamin »

Make life easy, create something reusable:

Code: Select all

<?php
 
function request($i) {
    return (isset($_REQUEST[$i])) ? trim($_REQUEST[$i]) : null;
}
 
 
function select($name, $values, $class = null, $javascript = null) {
    $form = '<select id="' . $name . '" ' . (($class === null) ? '' : 'class="' . trim($class) . '" ') . (($javascript !== null) ? $javascript : '') . ' name="' . $name . '">';
    foreach ($values as $key => $value) {
        $form .= (((request($name) !== null) ? request($name) : '') == (string) $key) ? '<option selected="selected" value="' . $key . '">' . $value . '</option>' : '<option value="' . $key . '">' . $value . '</option>';
    }
    $form .= "</select>";
    return $form;
}
 
$colors = array(
    '1' => 'red',
    '2' => 'green',
    '3' => 'blue',
);
 
?>
<form name="sample" method="post" action="#">
  Colors: <?php echo select('colors', $colors); ?>
  <button type="submit">Submit</button>
</form>
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: conditional php in html form - a question

Post by BornForCode »

And we also can create an engine that loads form definition from database and render itself and has also error management based on data. Also implement the decorators pattern to draw the form as i like and we are ready to go :D.

Your solution is not reusable astions because you are assuming that all the forms will have only that select box and its submit button.

Something reusable should look like:

Code: Select all

 
$form = new Form('contact')->method('post')->action('submit.php');
$form->addElement('select',$selectData,$options);
$form->addElement('submit','Contact Us')
$form->render();
 
Last edited by Benjamin on Tue Jun 30, 2009 10:28 am, edited 1 time in total.
Reason: Changed code type from text to php.
noclist
Forum Newbie
Posts: 7
Joined: Mon Jun 29, 2009 11:12 am

Re: conditional php in html form - a question

Post by noclist »

Thanks a lot guys, I got it figured out with basically a combination of everyones input. Great job.
Post Reply