ashis
ashis

Reputation: 381

php comparison of values in two radio buttons

this is one radio button set

<input type="radio" name="image" id="100" value="a"/>
<input type="radio" name="image" id="100" value="b"/>
<input type="radio" name="image" id="100" value="c"/>
<input type="radio" name="image" id="200" value="d"/>
<input type="radio" name="image" id="200" value="e"/>
<input type="radio" name="image" id="300" value="f"/>
<input type="radio" name="image" id="300" value="g"/>
<input type="radio" name="image" id="400" value="h"/>

and so on...

and this is another radio button set

<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>200
<input type="radio" name="number" value="300"/>300
<input type="radio" name="number" value="400"/>400
<input type="radio" name="number" value="500"/>500
<input type="radio" name="number" value="600"/>600

my question is how can i compare value of one with value="100" to other with id="100" in php

there is a comparison i want to make but for the field with id = 100 i have values as abcd.. so i cant assign it value = 100 cuz i m saving it in database means.. there are other fields with id=100 but their values shall be diffrent otherwise i m not able to identify them after they are send to databse still i have to do comparison of both.. and that too serverside so what are my options?

more detailed explanation.... there are two sets of radio buttons.. in one set there are values.. and in other sets there are images related to those values.. when user clicks set of value... and user click on set of images so php shall match that image to the value.. if user clicked on 100 as value and later he clicks on an image under the id 200 database shall not allow it to enter and gave him some error

Upvotes: 1

Views: 965

Answers (2)

Tim Lieberman
Tim Lieberman

Reputation: 571

Sorry Had to see what your talking about. Help to edit this so to understand your requirement, 'shall match that image to the value'

<input type="radio" name="image" class="100" value="a_100"/>
<input type="radio" name="image" class="100" value="b_100"/>
<input type="radio" name="image" class="100" value="c_100"/>
<br>
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>
<input type="radio" name="number" value="300"/>

<?php

    $image = strstr($_POST['image'],'_'); 
    $number = '_'.$_POST['number'];

    if($image == $number) {
        //Do something
    }

Upvotes: 3

SDC
SDC

Reputation: 14222

The question is looking for to compare the two fields in PHP. Presumably this means that you want to examine them when the form is posted.

This means that you'll only have available to you the parts of the field that are sent via the POST -- ie the field name and the value. PHP will never see the id of any of the fields, nor the class, nor any other attributes in the HTML code other than name and value.

Therefore the discussion in the comments about whether or not to use id is a somewhat moot point -- even if it is a good point for you to know in general for your HTML code, you can't really use either id or class here anyway because PHP will never see them.

You need to take an entirely different approach.

[EDITED after question edit]

Add the 100 or 200, etc to the value of the image radio buttons, like so:

<input type="radio" name="image" value="a_100"/>
<input type="radio" name="image" value="b_100"/>
<input type="radio" name="image" value="c_100"/>
<input type="radio" name="image" value="d_200"/>
<input type="radio" name="image" value="e_200"/>
<input type="radio" name="image" value="f_300"/>
<input type="radio" name="image" value="g_300"/>
<input type="radio" name="image" value="h_400"/>

(The number radio button set remains unchanged from your question)

You can then read it in PHP as follows:

$number = $_POST['number'];
list($imgNumber, $imgValue) = explode('_',$_POST['image']);

You can now compare $number with $imgNumber.

Hope that helps.

It's worth asking a follow-up question though: What are you trying to achieve here? Is this a validation exersise? ie where you're checking that the user is selecting an image option that matches the number option he's picked? If that's what you're doing, you may find it's more user friendly to use Javascript to filter the options available when they select the number option so that the image radio button set only shows options that are valid for the selected number.

This would be an entirely different question, so I won't go into any detail here, but I would suggest that it might be a more user-friendly way of doing things if you did it like that.

Upvotes: 0

Related Questions