homepage Welcome to WebmasterWorld Guest from 184.73.52.98
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




msg:3841508
 8:55 pm on Feb 3, 2009 (gmt 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" : ""?>>
<?=stripslashes($value)?>
</option>
<?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?

 

scalp8




msg:3841543
 9:19 pm on Feb 3, 2009 (gmt 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])."',
'".addslashes($_POST[Start_date])."',
'".addslashes($_POST[End_date])."',
'".addslashes($_POST[Start_time])."',
'".addslashes($_POST[End_time])."',
'".addslashes($_POST[County])."',
'".addslashes($_POST[State])."',
'".addslashes($_POST[Website])."',
'".addslashes($_POST[Description])."',
'".addslashes($_POST[Venue_name])."',
'".addslashes($_POST[interestCode])."',
'".addslashes($_POST[interestDesc])."',
'".addslashes($_POST[User_hosted])."',
'{$user_id}')";
$res= @mysql_query($sql, $this->dblink);
if (!$res)
$message.= "Unable to add event, please try again later.<br>";
return $message;
}

pinterface




msg:3841580
 10:16 pm on Feb 3, 2009 (gmt 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>
</SELECT>
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.

astupidname




msg:3841624
 10:59 pm on Feb 3, 2009 (gmt 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

scalp8




msg:3841673
 12:59 am on Feb 4, 2009 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved