Page 1 of 1

conditional php in html form - a question

Posted: Mon Jun 29, 2009 11:21 am
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?

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 11:31 am
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

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 12:17 pm
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.

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 2:44 pm
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>

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 6:10 pm
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>';
}
 
 

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 6:17 pm
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>
 

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 6:24 pm
by BornForCode
Pick mine, pick mine :drunk:

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 6:27 pm
by danielrs1
lol
I'd use yours :oops:

Re: conditional php in html form - a question

Posted: Mon Jun 29, 2009 11:45 pm
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>

Re: conditional php in html form - a question

Posted: Tue Jun 30, 2009 3:12 am
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();
 

Re: conditional php in html form - a question

Posted: Tue Jun 30, 2009 8:23 am
by noclist
Thanks a lot guys, I got it figured out with basically a combination of everyones input. Great job.