madsci Posted April 26, 2011 Share Posted April 26, 2011 I am trying to make a set of radio buttons that when you make a selection, it displays a sorted database from MYSQL. I have the MYSQL part figured out but I cannot figure out how to properly display it. I have the following code and I made a select screen just to test: index.html <html> <body> <form action="select.php" method="post"> <input type="radio" name="table" value="table1"> Table 1 <br> <br> <input type="radio" name="table" value="table2">Table 2 <br> <br> <input type="radio" name="table" value="table3">Table 3 <br> <br> <input type="radio" name="table" value="table4">Table 4 <br> <br> <input type="radio" name="table" value="table5">Table 5 <br> <br> <input type="Submit" Name = "submit" value="Submit"> <input type="Reset" value="Reset"> </form> </body></html> select.php <?PHP $selected_radio = $_POST["table"]; if ($selected_radio = = 'table1) { echo "Table 1 Here."; } else if ($selected_radio = = 'table2') { echo "Table 2 Here."; } else if ($selected_radio = = 'table3') { echo "Table 3 Here."; } else if ($selected_radio = = 'table4') { echo "Table 4 Here."; } else if ($selected_radio = = 'table5') { echo "Table 5 Here."; } ?> These are two separate files. When I go to run the code, no matter what I pick, the next page is always blank. Again, the select.php is just suppose to be a check, the real code will display the table instead of echoing a message. Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/ Share on other sites More sharing options...
sunfighter Posted April 26, 2011 Share Posted April 26, 2011 The problem is the php script. Instead of the if then's I re wrote this as a switch. I think it's better <?PHP $selected_radio = $_POST["table"]; switch ($selected_radio) { case 'table1': echo "Table 1 Here."; break; case 'table2': echo "Table 2 Here."; break; case 'table3': echo "Table 3 Here."; break; case 'table4': echo "Table 4 Here."; break; case 'table5': echo "Table 5 Here."; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206504 Share on other sites More sharing options...
madsci Posted April 26, 2011 Author Share Posted April 26, 2011 Thank you for the help sunfighter. But I know I am still missing something because I am still getting a blank page. I am editing both files in Notepad and running them both from my desktop, could that be a problem since I know PHP commands are server side? Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206507 Share on other sites More sharing options...
fugix Posted April 26, 2011 Share Posted April 26, 2011 first thing that i see is that all of your input names are the same...they need to be unique names Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206513 Share on other sites More sharing options...
fugix Posted April 26, 2011 Share Posted April 26, 2011 then you could probably set up a switch function using each unique radio button name...telling it what to do when that specific button is clicked Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206515 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 All radio buttons in an array of radio buttons get the same name. That's what makes radio buttons exclusive, so that only one in an array can be selected. Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206520 Share on other sites More sharing options...
Drummin Posted April 26, 2011 Share Posted April 26, 2011 You are missing a semicolon around table1 in the php. <html> <body> <form action="select.php" method="post"> <input type="radio" name="table" value="table1"> Table 1 <br> <br> <input type="radio" name="table" value="table2">Table 2 <br> <br> <input type="radio" name="table" value="table3">Table 3 <br> <br> <input type="radio" name="table" value="table4">Table 4 <br> <br> <input type="radio" name="table" value="table5">Table 5 <br> <br> <input type="Submit" Name = "submit" value="Submit"> <input type="Reset" value="Reset"> </form> </body></html> <?PHP $selected_radio = "$_POST[table]"; if ($selected_radio == "table1") { echo "Table 1 Here."; } else if ($selected_radio == "table2") { echo "Table 2 Here."; } else if ($selected_radio == "table3") { echo "Table 3 Here."; } else if ($selected_radio == "table4") { echo "Table 4 Here."; } else if ($selected_radio == "table5") { echo "Table 5 Here."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206523 Share on other sites More sharing options...
fugix Posted April 26, 2011 Share Posted April 26, 2011 ahh..was not aware of that..thanks pikachu Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206525 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 Here's an example that may be easier to implement. It uses only the part of the value that will change as the value= attribute in the radio array (in this case the number). Then you can simply concatenate the value to the part of the string that remains constant. This isn't designed to be a drop-in fix, rather an example. <?php if( isset($_POST['table']) ) { $table = "table{$_POST['table']}"; echo $table; } ?> <form action="" method="post"> <input type="radio" name="table" value=1> <input type="radio" name="table" value=2> <input type="radio" name="table" value=3> <input type="radio" name="table" value=4> <input type="radio" name="table" value=5> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206526 Share on other sites More sharing options...
madsci Posted April 26, 2011 Author Share Posted April 26, 2011 Thank you for the example, Pikachu. I tried following your example and added that php part into my html file, but I am still getting a blank page when I hit Submit. I feel like I am overseeing a simple, little step though. /facedesk Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206549 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 Using the form structure I posted above, all the code you posted for select.php could be replaced with this and the result would be the same. if( isset($_POST['table']) ) { echo "Table {$_POST['table']} here."; } Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206555 Share on other sites More sharing options...
madsci Posted April 26, 2011 Author Share Posted April 26, 2011 Logically, it makes sense to me. I understand how it works... But I am still getting a blank screen after I click submit. There must be something else I am doing wrong that isn't related to the code. :confused: This is becoming frustrating. Everyone, thank you all for the help you've given me so far, I greatly appreciate it. Update: When I run my html in Firefox (4.0) I get the blank page, when I run it in IE (8 ), it asks to download my select.php file Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206588 Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2011 Share Posted April 26, 2011 If the code you originally posted is still in the select.php script, you're getting a blank screen because that code has several fatal parse errors. There's an unclosed single quote at 'table1 and all of the comparisons have spaces. They should be == not = = . If that code isn't there any longer, then there's another error somewhere. You should develop with error_reporting=-1 and display_errors=On in your php.ini file. Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206611 Share on other sites More sharing options...
sunfighter Posted April 27, 2011 Share Posted April 27, 2011 When I checked you html file it worked. Your php didn't, that's why I re coded it. Your html and my php works great on my computer. BUT you say you coded in notepad so I'm wondering what you're using to interpret the php. I run wamp. What's yours? Also these two file must be in the same folder. A fast way to check is to make a file called test.php that only has <?php echo 'I am working'; ?> in it. redirect your form to test.php and see if this works when the submit button is pressed. Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206704 Share on other sites More sharing options...
Pikachu2000 Posted April 27, 2011 Share Posted April 27, 2011 N/M Quote Link to comment https://forums.phpfreaks.com/topic/234767-help-with-radio-button-html-and-php/#findComment-1206706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.