Page 1 of 1

php slelect tag

Posted: Mon Nov 27, 2006 7:15 am
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]

Posted: Mon Nov 27, 2006 8:07 am
by hawleyjr
I think this is what you are trying to do:

Code: Select all

$sql = "SELECT word FROM dictionary WHERE letter LIKE 'A%'";

Posted: Mon Nov 27, 2006 9:45 am
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

Posted: Mon Nov 27, 2006 9:49 am
by hawleyjr
Do you plan on using AJAX or reloading the page?

Posted: Mon Nov 27, 2006 9:59 am
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

Posted: Mon Nov 27, 2006 10:09 am
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...

Posted: Mon Nov 27, 2006 11:46 am
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

Posted: Mon Nov 27, 2006 12:30 pm
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>

Posted: Mon Nov 27, 2006 2:09 pm
by feyd
*cough* XSS attack.

;)

Posted: Mon Nov 27, 2006 2:22 pm
by hawleyjr
feyd wrote:*cough* XSS attack.

;)
The point was to give an example...no guarantees ;)

Posted: Mon Nov 27, 2006 8:16 pm
by aaronhall

Code: Select all

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