homepage Welcome to WebmasterWorld Guest from 54.211.180.175
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
HTML <SELECT> and PHP
So Simple I'm embarassed
bysonary




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

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




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

<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




msg:3232707
 12:08 pm on Jan 26, 2007 (gmt 0)
what so?

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

adb64




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

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




msg:3232713
 12:17 pm on Jan 26, 2007 (gmt 0)
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




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

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




msg:3232809
 2:29 pm on Jan 26, 2007 (gmt 0)
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




msg:3235045
 8:17 pm on Jan 28, 2007 (gmt 0)
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




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

it works, :-) cool

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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved