Jump to content

Recommended Posts

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

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

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.

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)

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!!!!

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.