Forum Moderators: coopster

Message Too Old, No Replies

Help: PHP with html and passing form info

PHP with html and passing form info (radio buttons)

         

ryanoasis

6:23 am on Jan 15, 2003 (gmt 0)

10+ Year Member



I am just beginning php. (as I am sure you will see) I have 2 pages. One page lists a bunch of radio buttons for the user to select one. Then the user hits submit and on the next page it will display info on the selected topic using a if else condition.

I KNOW how to do the if else statements, I just do not fully understand how to display certain information off of the selected radio buttons. I know I am supposed to do it in a simplistic manner, by using a variable. Because I have read that with forms you can simply reference the variable like ... "hello" would just be "$hello".

Here are the pages (shortened):

FORM PAGE THAT COLLECTS USERS CHOICE:

<html>
<head>
</head>
<body>
<?
print('<form action="chapter1b.php" method="post">
<input type="radio" name="something" value="opt11">Figure 1.1<br />
<input type="radio" name="something" value="opt12">Figure 1.2<br />
<input type="radio" name="something" value="opt13">Figure 1.3<br />
<input type="radio" name="something" value="opt14">Figure 1.4<br />
<input type="submit" value="Display" name="cmdDisplay">
</form>');
?>
</body>
</html>

RESULTS PAGE THAT DISPLAY USERS CHOICE:

<?
if ($value == $opt11)
{
print ("<h4>Figure 1.1</h4>");
} elseif($value == $opt12)
{
print ("<h4>Figure 1.2</h4>");
}else
{
print ("<h4>No selection made</h4>");
}
?>

If you can't help me/don't want to it's not the end of the world. Thanks.

jatar_k

7:16 am on Jan 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to the boards ryanoasis

I see a few problems here..

1. radio buttons normaly have the same name attribute implying they are in a group.

do you you drink milk?
<input type="radio" name="drink">yes
<input type="radio" name="drink">no

for me to allow the user to select only one I have to make each radio button part of the same group allowing them to only make one choice. The values of the three radio buttons would then be

value="yes"
value="no"

when it gets to the php page, for this particular group ,I can just check the variable $drink and see what it's value is

if ($drink == "yes")
{
print ("<h4>Figure 1.1</h4>");
} elseif($value == "no")
{
print ("<h4>Figure 1.2</h4>");
}else
{
print ("<h4>No selection made</h4>");
}

The one you had probably didn't work based on a few things

$value == $opt11

this reads, if the value of the variable $value is equal to the value of variable $opt11.

I think you mean this

if($value == "opt11") which reads, if the value of the variable $value is equal to the string opt11.

make sense? Try it out and tell me how that works, and again welcome to WebmasterWorld

dingman

1:41 pm on Jan 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Umm... wouldn't that be

if ($something == "opt11")

<input type="html_input_type" name="variable_name" value="value_of_variable">

Jatar_k's example code is fine, as far as I can see. It's just that last line that might be confusing. But I do think he's right about your problem with $ vs ".

lorax

2:04 pm on Jan 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld ryanoasis,
I'm not sure if you erred on the side of brevity or not but the quotes in your print statement should be escaped as well like this print('<form action=\"chapter1b.php\" method=\"post\"> ...

jatar_k

4:33 pm on Jan 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



good point dingman, it was late and the value, something was starting to confuse me.

lorax, I totally missed that

ryanoasis, you don't even need the print in your html page you could just go straight html, like this

<html>
<head>
</head>
<body>
<form action="chapter1b.php" method="post">
<input type="radio" name="something" value="opt11">Figure 1.1<br />
<input type="radio" name="something" value="opt12">Figure 1.2<br />
<input type="radio" name="something" value="opt13">Figure 1.3<br />
<input type="radio" name="something" value="opt14">Figure 1.4<br />
<input type="submit" value="Display" name="cmdDisplay">
</form>
</body>
</html>

dingman

4:47 pm on Jan 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it was late

I didn't figure there was any other explanation. After all, I know you know how to do this stuff. :)

Escaping quotes:

Given that he started the string with a ' rather than a ", the only thing that should need to be escaped would be any ' internal to the string. Of course, there's really no reason to use the print statement at all in this context. And I have to admit that I totally missed teh fact that there even was a print statement until Lorax pointed it out.

ryanoasis

5:00 pm on Jan 15, 2003 (gmt 0)

10+ Year Member



Actually I tried putting the values into strings and variables.

Okay it is how jatar_k said.
I was using the name attribute (of the group)
instead of the value attribute to check the value.

Yes I know it does not seem logic to put the whole form into a php page when its all just
html anyway.

Well now I know you use the name of the group as the variable and check the string with the
value attribute.

Thanks to all.

lorax

5:27 pm on Jan 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> lorax, I totally missed that

S'allright - I've missed obvious stuff too - and pounded my head against the wall for hours swearing the code should work only to have a coworker (or worse - someone on this Board) point out the obvious oversight. Geesh - talk about embarrasing!

In the end, a solution is achieved and that's what counts.