Html Select Box and php

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
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Html Select Box and php

Post by Nik »

Hello, i was wondering if there was a way to give data to an html select box using php and reading that data from a text file

The html syntex for creating such a select box is the following!

What changes must i make in order to make it read data from a php file?

<select name="cd"> <option name="1">One <option name="2">Two </select>

I can also load the contents of the php file to an array using the following

$data=file("data.txt")

Pls help me out with this.....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and what data is in that file?
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

dont you need </option> tags?

i normally use a while loop

Code: Select all

$selectfeed = '<select name=monkey>';
while($data=/*datasource*/)
&#123;
   $selectfeed .= '<option value="' . $data&#1111;'this'] . '">' . $data&#1111;'that'] . '</option>';
&#125;
$selectfeed .= '</select>';
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

file.txt

1|one
2|two
etc

$data = explode('|',file('file.txt'));
echo '<select name="selex" />
';
foreach($data as $dataline)
{
echo '<option value="' .$dataline[0]. '">' .$dataline[1]. '</option>
';
}
echo '</select>
';
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

Thank u very much guys for your help! Can u pls tell me pootergeist how can i paste this solution to the following cause i didnt fugure it out.

<form action="check.php" method="post">
<table border=1 width=50% align=Center bordercolor=Yellow style='border-style: solid; border-width: 5' background=pics/blue.jpg>
<tr>
<td align=center> <font size=5 color=LightGreen> Τίτλος 1ης ταινίας? </td>
<td align=center> <select name=dvd1> <option name=1> Die Another Day </select></td>
</tr>
<tr>
<td align=center> <font size=5 color=LightGreen> Τίτλος 2ης ταινίας? </td>
<td align=center> <select name=dvd2> <option name=2> Ένα </select></td>
</tr>
<tr>
<td align=center> <font size=5 color=LightGreen> Τίτλος 3ης ταινίας? </td>
<td align=center> <select name=dvd3> <option name=3> Ένα </select></td>
</tr>
<tr>
<td align=center> <font size=5 color=LightGreen> Τίτλος 4ης ταινίας? </td>
<td align=center> <select name=dvd4> <option name=4> Ένα </select></td>
</tr>
<tr>
<td align=center> <font size=5 color=LightGreen> Τίτλος 5ης ταινίας? </td>
<td align=center> <select name=dvd5> <option name=5> Ένα </select></td>
</tr>
</table><br><br>

Aso the txt file is like

text1
text2
text3

with no pipes before it

Thanks again!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

It's recommended to htmlspecialchars() php vars added to forms (or before any kind of browser output) - maybe that's been done already.
Post Reply