php slelect tag

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
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

php slelect tag

Post by acwe88 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi there,

i have a mysql database table called dictionary.

the fields in this table are letter, word and description

what i need is when a user selects for example Alex (in the field "word")  the text in the description field is displayed? 

This code  displays a select form which gets all the "A" values from the field "letter" in my database

Code: Select all

$sql = 'SELECT word FROM dictionary WHERE letter="A"';
  $res = mysql_query($sql) or die(mysql_error());
  while ($rec = mysql_fetch_assoc($res)) $word[] = $rec;


  echo '<SELECT name="dropdown">';
  foreach ($word as $c)
    echo "<OPTION>{$c['word']}</OPTION>\n";
  echo '</SELECT>';
Any help will be much appreciated

Thanks
Alex


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I think this is what you are trying to do:

Code: Select all

$sql = "SELECT word FROM dictionary WHERE letter LIKE 'A%'";
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

Post by acwe88 »

thanks

So how would i display the description result when a user selects a value from the select box

so basically if i select alex from the drop down box i need a peice of text to appear below which shows the description

Thanks
Al
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Do you plan on using AJAX or reloading the page?
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

Post by acwe88 »

Not sure really ive had a little look at ajax and still dont know what to do

id like it to automatically dispay the result but not to sure if i can even do this

Thank
Al
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

If you are a noob, I'd recommend refreshing the page first and then try to do it using AJAX. To display the description:

Option (a) Create an onChange event in the select box (b) Add a button to submit the form

Reload the page but with a GET parameter with the value of the selected item.

When the page is loaded check for that GET parameter. If it is there display the info. If not do not display any info...

This should be enough info to get you started...
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

Post by acwe88 »

hi cant i do something like this


if the word is on the same row as the description display that description



so then on my <select> part i could put onchange=\"get_description()\"


Does this make sense and if you could post how i would do this i would appreciate it very much

Thanks
AL
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try something like this:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" name="form1">
  <select name="sel_test" onChange="this.form.submit();">
    <option value="Alex">Alex</option>
    <option value="Alex 2">Alex 2</option>
  </select>
</form>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*cough* XSS attack.

;)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

feyd wrote:*cough* XSS attack.

;)
The point was to give an example...no guarantees ;)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>
Always sanitize PHP_SELF
Post Reply