Jump to content

Help with Radio Button HTML and PHP


madsci

Recommended Posts

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.

Link to comment
Share on other sites

The problem is the php script. Instead of the if then's I re wrote this as a switch. I think it's better :D

 

<?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;
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.";
}

?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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: :confused: :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  :confused:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.