lamboman Posted May 27, 2011 Share Posted May 27, 2011 I am wondering if this is posible. I have a group of radio butons. I would like to make them as variables so that I can use an "if then" statment in another part of the page to show an image depending on which button is selected. <form id="form1" name="form1" method="post" action=""> <p> <label> <input type="radio" name="color" value="red" id="color_0" /> red</label> <br /> <label> <input type="radio" name="color" value="blue" id="color_1" /> blue</label> Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/ Share on other sites More sharing options...
Maq Posted May 27, 2011 Share Posted May 27, 2011 Sure it's possible. What part do you need help with? Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221188 Share on other sites More sharing options...
lamboman Posted May 27, 2011 Author Share Posted May 27, 2011 Just on how exactly do I define the button as a variable? Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221190 Share on other sites More sharing options...
wildteen88 Posted May 27, 2011 Share Posted May 27, 2011 When the form has been submitted use the $_POST['color'] superglobal variable. This will return the value of the selected radio button Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221192 Share on other sites More sharing options...
lamboman Posted May 27, 2011 Author Share Posted May 27, 2011 I need to do everything before the post. When a user click on the radio button, certain images on the same page will change as will some text. I need to set each button as a variable so that when it is clicked it will change the information on the same page at the same time Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221194 Share on other sites More sharing options...
Maq Posted May 27, 2011 Share Posted May 27, 2011 I need to do everything before the post. When a user click on the radio button, certain images on the same page will change as will some text. I need to set each button as a variable so that when it is clicked it will change the information on the same page at the same time Then you have to use Javascript, more specifically, the onClick() event and change the DOM that way. Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221197 Share on other sites More sharing options...
grissom Posted May 27, 2011 Share Posted May 27, 2011 In which case, you don't need to use variables at all, just use your HTML as it stands, and, as MAq suggests, use the javascript onClick event. One little thing, your naming convention will probably work OK, but I'm paranoid and I try to keep names of elements different to any of the defined words in javascript or PHP. As a precaution, I'd recommend changing the name to : name = "my_color' (actually being British, I'd spell it "my_colour" but that's by the by) Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221200 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 27, 2011 Share Posted May 27, 2011 Javascript is the only language that can handle what you are trying to do. PHP can only run conditions when an action is made. Checking a checkbox or clicking a radio button does not constitute as an actual 'action'. Javascript will do whatever you want. Crazy thing is, just this morning I went to W3Schools and learned a large amount of Javascript. What you wish to achieve can be done so simply with Javascript!!! Basically you need to do something like the following (and please, any Javascript experts correct me if I am wrong) : if (document.getElementById('checkbox_id').checked) { //Do some stuff... }else if(document.getElementById('other_checkbox_id').checked) { //do_something_else } Remember, your id can be a variable, an array, whatever you need it to be to get the results. Now when it comes to what exactly goes between the 'if' or the 'elseif' statement still confuses me....Good Luck!!!! Link to comment https://forums.phpfreaks.com/topic/237648-variable-on-radio-buton/#findComment-1221287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.