Page 1 of 1

changing dropdown based on selected radio button

Posted: Fri Aug 06, 2010 4:58 am
by stow
I've been struggling with this piece of code for a good couple of days now. What I'm trying to do it change the options of a drop down list depending on which radio button is selected. What I'm having trouble with is passing the variable from the radio button to form link to read. Here's the code below:

Code: Select all

<?php 
$connection = mysql_connect("server","user","pass"); 
$db=mysql_select_db("db", $connection); 

$furniture = "SELECT furniture FROM items"; 
$sql = mysql_query($furniture) or die(mysql_error()); 
while ($row = mysql_fetch_array($sql)) 
{ 
    echo "<input type=\"radio\" name=\"web\" value='".$row['furniture']."'>".$row['furniture']."</input>"; 
} 

$furn = $_POST['web'];//The problem?? 
$dir = $furn."/upload"; 
echo "<br /><select name=\"up\">"; 

if(file_exists($dir)) 
{ 
    if($dh=opendir($dir))//Opening directory 
    { 
        if(($dhf = scandir($dir)) && (count($dhf) == 2)) 
        { 
            echo "<option value=\"empty\">Directory is empty!</option>"; 
        } 
        else 
        {     
            while (($sd = (readdir($dh))) !== false)//while the directory being read is
            { 
                if($sd != "." && $sd != "..") 
                { 
                    echo "<option value='".$sd."'>".$sd."</option>"; 
                } 
            } 
        } 
    } 
} 
echo "</select>"; 

echo"<br />If your can find the type of furniture you are looking for create a new one:"; 
echo"<br /><input type=\"text\" name=\"newFolder\" />"; 

?>
The piece of code is linked to the php/html below where the code above is form.php:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Upload File</title> 
</head> 

<body> 
<form action="upload.php" method="post" enctype="multipart/form-data"> 
<input type="hidden" name="MAX_FILE_SIZE" value="104857600" /> 
File:<input type="file" name="file" /><br /> 
<?php include 'form.php'; ?><br /><!--lists furniture depending on type--> 
<input type="submit" name="submit" /> 
</form> 

</body> 
</html>
I've tried a var_dump on the $_POST['web']. Before the radio button is selected it produces NULL and After a radio button is selected NULL still appears.

Any help would be apprieciated. Thanks,

Stow

Re: changing dropdown based on selected radio button

Posted: Fri Aug 06, 2010 6:50 am
by internet-solution
There is no form on your page. So nothing is posted.

You have to include your radio buttons in a form and

Code: Select all

echo"<form action='action.php' method='post'>";
while ($row  = mysql_fetch_array($sql))
{
    echo "<input type=\"radio\" name=\"web\" value='".$row['furniture']."'>".$row['furniture']."</input>";
} 
echo"<input type='submit'>
</form>";
user has to submit it by pressing a submit button before $_POST['web'] is populated and this will be available in the action.php (the php file designated as action page for the form). If you want to do this in PHP, then you will have to put second part of your code in page action.php

If you want both radio buttons and file upload on a same page, then you will have to rely on javascript and Ajax.

Re: changing dropdown based on selected radio button

Posted: Fri Aug 06, 2010 7:31 am
by stow
the first php code is part of the html/php. i have used the following:

Code: Select all

<form action="upload.php" method="post" enctype="multipart/form-data"> 
<input type="hidden" name="MAX_FILE_SIZE" value="104857600" /> 
File:<input type="file" name="file" /><br /> 
<?php include 'form.php'; ?><br /><!--lists furniture depending on type--> 
<input type="submit" name="submit" /> 
</form> 
where form.php is the first php code I stated. What I wanted was to pass the selected radio button onto the dropdown box so that the dropdown box is altered depending on the radio button selection. this is so that the user doesn't need to submit after every button press and makes the website a little bit more dynamic.