简体   繁体   中英

PHP Dynamic Radio Buttons and Submit Button

I am trying to create a php file that would scan a directory, make a list of dynamic radio buttons and have the user select one and submit it and it shall pass it into another php file that would send it off, but I am having trouble with the submit button. Was wondering if I could get some answers, been looking around the net and haven't been able to find anything. Here is my code:

<?php

ini_set('display_errors',true);
//Scan directory (ripped from another site)
$directory = opendir("download");
while($entry = readdir($directory)) {
    $dirArray[] = $entry;
}
closedir($directory);
$indexCount = count($dirArray);
sort($dirArray);

//Make the Radio Button list
$RList = "";
for($i=2; $i<count($dirArray); $i++) {
    $value = $dirArray[$i];
    $RList .= "<input type=\"radio\" name=\"Files\" value=".$value." />".$value."<br />";
}
//Submit button
$RButton = '<input type="button" value="Submit" 
onclick="location.href=\'http://localhost/appClient/load.php?file='.$value.'\'">';
echo $RList.$RButton;
#echo $_POST["Files"];

?>

Thanks

It looks like you're passing $value to the onclick event. Since that's outside of the PHP loop, it will end up always being the last $value .

To fix this, use javascript to get the selected value from the radio and append that to the onclick redirect.

Better yet, your load.php file should just simple check the 'Files' key in your $_POST and use that, instead of using GET. That's the point of submitting the radio button, after all.

I don't see the <form> tag, try to add it because the all buttons and inputs must be within the <form>

Does your script is calling the file load.php already or the button just does not work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM