Welcome to WebmasterWorld Guest from 54.227.1.130

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

HTML <SELECT> and PHP

So Simple I'm embarassed

     

bysonary

11:45 am on Jan 26, 2007 (gmt 0)

5+ Year Member



hello, I am just trying this for testing of a bigger idea and well I hit a baby problem. here is what i want to do, I have created a HTML list with some items in it. I basically want to select an Item from the list, press submit and have it echo "you have selected "ITEM" from the list, here is the ciode for the list below.

<form method="post" action="test.php">
<select name="OS" SIZE=3>
<option selected="selected">Windows
<option>Macintosh
<option>UNIX
<option>Amiga
<option>Next
<option>AppleII
</selectT>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

all looks good upto here as it should, I then tried this very basic PHP script to see if echoing a list was possible.

<?php
echo "you have selected ".$OS." from the list";
?>

I even tried

<?php
echo $OS;
?>

with no luck, so can anyone tell me where I am going wrong and how to get my little echo to work as i want it, for instance if I select Windows from the list and press submit i want it to display

you have selected Windows from the list.

I would appreciate a hand haha sorry if its novice but I am still starting out and aren't a php guru yet.

mcibor

12:02 pm on Jan 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<form name="form" method="post" action="test.php">
<select name="OS" SIZE=3>
<option value="Win" selected="selected">Windows
<option value="Mac">Macintosh
<option value="Unix">UNIX
<option value="Amiga">Amiga
<option value="Next">Next
<option value="AppleII">AppleII
</select>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

it should be now in $_POST['OS'];

if not, then
print_r($_POST);

bysonary

12:08 pm on Jan 26, 2007 (gmt 0)

5+ Year Member


what so?

<?php
echo "you have chosen " .$_POST['OS']. " from the list";
?>

adb64

12:09 pm on Jan 26, 2007 (gmt 0)

10+ Year Member



You should also close the <option> tags with </option> and remove the extra T in the </select> close tag.


<form method="post" action="test.php">
<select name="OS" SIZE=3>
<option selected="selected">Windows</option>
<option>Macintosh</option>
<option>UNIX</option>
<option>Amiga</option>
<option>Next</option>
<option>AppleII</option>
</select>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

bysonary

12:17 pm on Jan 26, 2007 (gmt 0)

5+ Year Member


Thats great thanks, my sloppy HTML, its only 11 am here I havent been out of bed long.

I have a question regarding this, is it possible to list the contents of a directory in the list? and then do something like

unlink($_POST['OS']);

I have a directory of images and need PHP to delete the images, this seemes like a good way of doing it.

any help relating to this would be much appreciated.

mcibor

12:28 pm on Jan 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can unlink only one file at a time
[de.php.net...]

there you've got a way to remove whole dir

<?php
function rmdirr($dir) {
if (substr($dir,-1)!= "/") $dir .= "/";
if (!is_dir($dir)) return false;

if (($dh = opendir($dir))!== false) {
while (($entry = readdir($dh))!== false) {
if ($entry!= "." && $entry!= "..") {
if (is_file($dir . $entry) ¦¦ is_link($dir . $entry)) unlink($dir . $entry);
else if (is_dir($dir . $entry)) rmdirr($dir . $entry);
}
}
closedir($dh);
rmdir($dir);

return true;
}
return false;
}

// Example:
$dir = "./MyDirectory/";
$res = rmdirr($dir); // Bye bye
if ($res == true) print "$dir got deleted.";
else print "Error while trying to delete $dir.";
?>

bysonary

2:29 pm on Jan 26, 2007 (gmt 0)

5+ Year Member


I really only want to unlink one file at a time as I dont want to delete entire directory as some images in the directory need to stay intact.

bysonary

8:17 pm on Jan 28, 2007 (gmt 0)

5+ Year Member


Hello I have the following PHP code.

<?php
$dir = "/home/www/juttuffi/gallery/images";
$d = dir($dir);

while($entry = $d->read())
{
if ($entry!= "." && $entry!= ".."[smilestopper])
{
echo "<br>".$entry."<br>";
}
}
$d->close();
?>

will this work in a HTML list box form thingy?

sort of wrap the $entry in <option> tags?

kind of like this....

<form method="post" action="unlink.php">
<select name="OS" SIZE=3>

<?
$dir = "/home/www/juttuffi/gallery/images";
$d = dir($dir);

while($entry = $d->read())
{
if ($entry!= "." && $entry!= ".."[smilestopper])
{
echo "<option>".$entry."</option>";
}
}
$d->close();
?>

</select>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

anyone comment on the above?

bysonary

8:29 pm on Jan 28, 2007 (gmt 0)

5+ Year Member



it works, :-) cool
 

Featured Threads

Hot Threads This Week

Hot Threads This Month