Jump to content

Radio Buttons HTML and PHP


PYRRHIC

Recommended Posts

I have been searching for something like this to use so I am trying to implement these ideas for my form but unable to get it to work. The concept is similar so I am posting here but if I would make a new post just let me know.

 

it is not posting the info when the radio button is checked. Someone with a keener eye that mine can give a look and tell what I have done incorrectly it would be appreciated.

 

echo '
<form action="" method="get"><div id="avatar_section">

<div id="avatar_stack">
<p>
<img src="herotmb/face0.png" alt="" /><input name="avatar[head]" type="radio" value="face0" checked="checked" />
</p>

<p>
<img src="herotmb/face0.png" alt="" /><input type="radio" name="avatar[face]" value="face0" />
<img src="herotmb/face1.png" alt="" /><input type="radio" name="avatar[face]" value="face1" />
<img src="herotmb/face2.png" alt="" /><input type="radio" name="avatar[face]" value="face2" />
<img src="herotmb/face3.png" alt="" /><input type="radio" name="avatar[face]" value="face3" />
</p>

<span name="color">
<p>
<img src="herotmb/color0.png" alt="" /><input type="radio" name="avatar[color]" value="black" id="black" onclick="color(black);" />
<img src="herotmb/color1.png" alt="" /><input type="radio" name="avatar[color]" value="brown" id="brown"  />
<img src="herotmb/color2.png" alt="" /><input type="radio" name="avatar[color]" value="darkbrown" id="darkbrown" onclick="color(darkbrown);" />
<img src="herotmb/color3.png" alt="" /><input type="radio" name="avatar[color]" value="yellow" id="red" onclick="color(red);" />
<img src="herotmb/color4.png" alt="" /><input type="radio" name="avatar[color]" value="red" id="yellow" onclick="color(yellow);" />
</p>
</span>	';


$selected_radio = '$_POST[avatar[color]]';
if ($selected_radio == 'black') {
echo '				<p>
			<img src="herotmb/hair0-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair0-black"  />
			<img src="herotmb/hair1-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair1-black"  />
			<img src="herotmb/hair2-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair2-black" />
			<img src="herotmb/hair3-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair3-black" />
			<img src="herotmb/hair4-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair4-black" />
			<img src="herotmb/hair5-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair5-black" />
			<img src="herotmb/hair6-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hair6-black" />
			<img src="herotmb/hairNone-black.png" alt="" /><input name="avatar[hair]" type="radio" value="hairNone" />
			</p>';
}
else if ($selected_radio == 'brown') {
echo 'Table 2 Here.';
}
else if ($selected_radio == 'darkbrown') {
echo 'Table 3 Here.';
}
else if ($selected_radio == 'red') {
echo 'Table 4 Here.';
}
else if ($selected_radio == 'yellow') {
echo 'Table 5 Here.';
}

echo '			

<p id="submit" >
<input type="submit" value="Save" />
</p>

</form>';

 

Link to comment
Share on other sites

The variable in this line is being interpreted as a string literal because it is in single quotes, also the square brackets are misplaced and the indices should be quoted.

$selected_radio = '$_POST[avatar[color]]';

 

It should read;

$selected_radio = $_POST['avatar']['color'];

Link to comment
Share on other sites

wow, didn't know this was moved thought it got deleted and almost posted it again. good thing I was searching and thanks for just  moving it

 

Thanks for the replies, no I  am not getting an error messages the tab info is just not showing.

I tried $selected_radio = $_POST['avatar']['color']; but the tab info is now being skip. what I mean by that is the info from  2 tabs down is showing in its place.

any other suggestions. I can post the JS info which is making the tabs if need be.

 

thanks in advance

Link to comment
Share on other sites

I will keep that in mind, thanks

 

I added error_reporting and  now I am getting an error line 113

 

112          $selected_radio = $_POST["avatar[color]"];
113          if ($selected_radio == "black") {
114          echo '			

 

when I change ==  to !=  the info shows up but does not switch to (Table 2 here) so I am assuming, and correct me if Im wrong, that the value from selected_radio  is not being recognized.

 

how would I fix this ?

 

 

 

 

 

 

Link to comment
Share on other sites

$selected_radio = $_POST["avatar[color]"]; // <--- is wrong (still)
$_POST['avatar']['color'] // <--- Should be

 

The root problem here is that your form method is get, but you're attempting to access variables in the $_POST array. Either change the form method to post, or access the data using the $_GET array.

Link to comment
Share on other sites

ok thanks for your input. 

 

Since Im getting "Undefined index: avatar",  even in array the problem still wouldn't be solved, would it

I've even removed the extra brackets, now its avatar_color.

 

would I be trying to make the array for both the avatar_color and  avatar_hair or just one of the sections? 

Link to comment
Share on other sites

tried changing method to post and using $_GET[avatar_color]  way no luck

 

<form action="" method="post"><div id="avatar_section">

<div id="avatar_stack">
<p>
<img src="herotmb/face0.png" alt="" /><input name="avatar_head" type="radio" value="face0" checked="checked" />
</p>

<p>
<img src="herotmb/face.png" alt="" /><input type="radio" name="avatar_face" value="face0" />
<img src="herotmb/face1.png" alt="" /><input name="avatar_face" type="radio" value="face1" />
<img src="herotmb/face2.png" alt="" /><input name="avatar_face" type="radio" value="face2" />
<img src="herotmb/face3.png" alt="" /><input name="avatar_face" type="radio" value="face3" />
</p>


<p>
<img src="herotmb/color0.png" alt="" /><input type="radio" name="avatar_color" value="black" id="black" />
<img src="herotmb/color1.png" alt="" /><input type="radio" name="avatar_color" value="brown" id="brown" onclick="color(brown);" />
<img src="herotmb/color2.png" alt="" /><input type="radio" name="avatar_color" value="darkbrown" id="darkbrown" onclick="color(darkbrown);" />
<img src="herotmb/color3.png" alt="" /><input type="radio" name="avatar_color" value="yellow" id="red" onclick="color(red);" />
<img src="herotmb/color4.png" alt="" /><input type="radio" name="avatar_color" value="red" id="yellow" onclick="color(yellow);" />
</p>

<?php
$selected_radio = $_GET['avatar_color'];
---line102-----  if ($selected_radio == 'black') {
echo '				<p>
			<img src="herotmb/hair0-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair0-black"  />
			<img src="herotmb/hair1-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair1-black"  />
			<img src="herotmb/hair2-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair2-black" />
			<img src="herotmb/hair3-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair3-black" />
			<img src="herotmb/hair4-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair4-black" />
			<img src="herotmb/hair5-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair5-black" />
			<img src="herotmb/hair6-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair6-black" />
			<img src="herotmb/hairNone-black.png" alt="" /><input name="avatar_hair" type="radio" value="hairNone" />
			</p>';
}
else if ($selected_radio == 'brown') {
echo '
  				<p>
			<img src="herotmb/hair0-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair0-brown" />
			<img src="herotmb/hair1-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair1-brown" />
			<img src="herotmb/hair2-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair2-brown" />
			<img src="herotmb/hair3-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair3-brown" />
			<img src="herotmb/hair4-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair4-brown" />
			<img src="herotmb/hair4-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair4-brown" />
			<img src="herotmb/hairNone-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hairNone" />
			</p>';
}
else if ($selected_radio == 'darkbrown') {
echo 'Table 3 Here.';
}

 

Notice: Undefined index: avatar_color in index20.php on line 102

 

(yes this is attempt 20)

 

 

 

Trying to make an array as you suggested, how does this look

 

$array = array('color'=> 'black','brown','darkbrown','red','yellow');

echo '

<p>

<img src="herotmb/$array.png" alt="" /><input type="radio" name="avatar_color" value="$array" id="$array" />';

</p>';

Link to comment
Share on other sites

The data from a form will be available in the superglobal array that matches the form's method. I.E. a for with the method set to post will pass the data in the $_POST array. A form with the method set to get will send data inthe $_GET array (and the data should be visible in the URL string). You're getting an undefined index error because you don't check to see if the index is present in the array (which it won't be, because of the $_POST/$_GET mismatch) before attempting to access it.

 

<?php
if( !empty($_POST['avatar_color']) ) {
$selected_radio = $_POST['avatar_color'];
if ($selected_radio == 'black') {
	echo '				<p>
			<img src="herotmb/hair0-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair0-black"  />
			<img src="herotmb/hair1-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair1-black"  />
			<img src="herotmb/hair2-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair2-black" />
			<img src="herotmb/hair3-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair3-black" />
			<img src="herotmb/hair4-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair4-black" />
			<img src="herotmb/hair5-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair5-black" />
			<img src="herotmb/hair6-black.png" alt="" /><input name="avatar_hair" type="radio" value="hair6-black" />
			<img src="herotmb/hairNone-black.png" alt="" /><input name="avatar_hair" type="radio" value="hairNone" />
			</p>';
}
else if ($selected_radio == 'brown') {
	echo '
  				<p>
			<img src="herotmb/hair0-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair0-brown" />
			<img src="herotmb/hair1-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair1-brown" />
			<img src="herotmb/hair2-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair2-brown" />
			<img src="herotmb/hair3-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair3-brown" />
			<img src="herotmb/hair4-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair4-brown" />
			<img src="herotmb/hair4-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hair4-brown" />
			<img src="herotmb/hairNone-brown.png" alt="" /><input name="avatar_hair" type="radio" value="hairNone" />
			</p>';
}
else if ($selected_radio == 'darkbrown') {
	echo 'Table 3 Here.';
}
}

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.