Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Loading a different value to the database then what is shown to user

8:55 pm on Feb 3, 2009 (gmt 0)

New User

5+ Year Member

joined:Jan 5, 2009
posts: 34
votes: 0

As always, thank you so much for the contributions that everyone makes on this site. It is amazing and appreciated.
I have a drop down that I'm populating from a static array. I want the values shown in the drop down to be event categories that the user can select one of. I want a different value (an interestID) to be stored in the database though. Here is what I'm trying without any success:

$arrInterestCode= array("1"=>"Arts: Art Exhibits","2"=>"Arts: Ballet",........
//1 is the code for Art Exhibits and 2 is the code for Ballet

<td align="right" class="maintextCopy"><div align="left"><em><strong>Category:</strong></em></div></td>
<td colspan="2" align="left"><select name="Category" class="selectbox" id="Category" onchange="getInterestDescList(this)";>
<?php foreach ($arrInterestCode as $key => $value) { ?>
<option value="<?=stripslashes($value)?>" <?=$objU->Category == stripslashes($value) ? "selected" : ""?>>
<?php } ?>
//I was trying to use Ajax to get more dynamic with this, but decided to just try to get it working static for now so I know that a couple pieces of this are unnecessary.

It is showing up in the drop down correctly, but saving to the database as the Art Exhibits or Ballet. Am I on the right track?

9:19 pm on Feb 3, 2009 (gmt 0)

New User

5+ Year Member

joined:Jan 5, 2009
votes: 0

Also wanted to add the function that's writing it to the database:

function insert_event($user_id=0)
$message= "";
$sql= "INSERT INTO tbl_events (Name, Start_date, End_date, Start_time, End_time, County, State, Website, Description, Venue_name, Category, Sub_category, User_hosted, nUser_Id)
VALUES ('".addslashes($_POST[Name])."',
$res= @mysql_query($sql, $this->dblink);
if (!$res)
$message.= "Unable to add event, please try again later.<br>";
return $message;

10:16 pm on Feb 3, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 28, 2006
votes: 0

Reading about and understanding how SELECT and OPTION elements work [w3.org] might help, but that's a pretty long section so I'll help you out.

Suppose you have

<OPTION VALUE="option value">option text</OPTION>
. What this means is that "option text" will be the choice given to the user, and "option value" is what will be sent to your form.

So, for instance, if you had:

<SELECT NAME="Category">
<OPTION VALUE="1">Arts: Art Exhibits</OPTION>
<OPTION VALUE="2">Arts: Ballet</OPTION>
and the user selected "Arts: Ballet", $_POST['Category'] would be "2".

The page I linked also includes information on grouping options together, and more fun things, so might be worth a quick read.

10:59 pm on Feb 3, 2009 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
votes: 0

What pinterface is getting at, is where you have: <option value="<?=stripslashes($value)?>" you are using $value there when you should be using $key
12:59 am on Feb 4, 2009 (gmt 0)

New User

5+ Year Member

joined:Jan 5, 2009
votes: 0

Got it! Pinterface and astupidname, thank you very much. I used your fix astupidname, but I will definitely take a look at the link you attached pinterface.