Closed Thread Icon

Topic awaiting preservation: PHP and HTML Select Form Field (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29713" title="Pages that link to Topic awaiting preservation: PHP and HTML Select Form Field (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP and HTML Select Form Field <span class="small">(Page 1 of 1)</span>\

 
Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 11-25-2007 02:08

Hi all,

I have this PHP code for generating a select box in an html form with a date range of current year back to 1900 (see below):

code:
//snippet

function write_form() {
		$year_built_min = 1900;
    $year_built_max = date("Y");
	
    global $PHP_SELF;
    print '<form method="POST" enctype="multipart/form-data">';
    print '<table cellpadding=2 cellspacing=2 border=1>';
    print '<tr><td>Year*</td><td>';
    print '<select name="autos_yr" size="1">';
      foreach (range($year_built_max,$year_built_min) as $year) { 
        print '<option value="'.$year.'">'.$year.'</option>';
        }
    print '</select>';
    print '</td></tr>';



I use the code above to generate a select box with years from current year to 1900 for when I am INSERTing records from an HTML form into MYSQL.

Now I want to do a SELECT statement on MYSQL for a unique record and when I get that record out of the DB I want to be able to edit each field of the record in an html form. So if the record comes back from MYSQL and it has 2002in the year field I want the HTML select form field to print all years from current year to 1900 with the year 2002 SELECTED. What happens if 2002 is SELECTED in the select html form and I change it to 2003 and submit it back for a mysql UPDATE, will this cause issues since 2002 is already SELECTED?

Thanks!

jdauie
Bipolar (III) Inmate

From: Missoula, MT
Insane since: Jan 2003

posted posted 12-08-2007 10:02

Boudga,

If this is still an issue, please clarify your post by describing the format of the MySQL table you are dealing with. I don't understand fully what you are trying to accomplish.

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-08-2007 16:04

If I see this correctly, all you need to do is this:

code:
foreach (range($year_built_max,$year_built_min) as $year) { 
        print '<option value="'.$year.'">'.$year.'</option>';
        }



needs to become

code:
...
$year_of_the_record = $mysqlresult["year"];
...
foreach (range($year_built_max,$year_built_min) as $year) 
{ 
        if ($year == $year_of_the_record)
            print '<option value="'.$year.'" selected="true">'.$year.'</option>';
        else
            print '<option value="'.$year.'">'.$year.'</option>';
}



so long,

->Tyberius Prime

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 12-14-2007 15:57

TB that is exactly what I was looking for!!! Thank you so much.

« BackwardsOnwards »

Show Forum Drop Down Menu