user3745594
user3745594

Reputation: 43

get hidden value of the currently selected radio button

i have a form with Variables coming from mysql query , and i passing from a form 2 value of radio and hidden input , no problem with radio .. but hidden not passing Correct value , passing only first value found it on the page . I want to get 2 value of radio and hidden together when i Choose the currently selected radio button

<input type="radio" name="radio" value="<?php echo $awardid ; ?>" />
<input type="hidden" name="point" value="<? echo $point ; ?>" />

after print :

<input type="radio" name="radio" value="1">
<input type="hidden" name="point" value="3">

<input type="radio" name="radio" value="2">
<input type="hidden" name="point" value="5">

<input type="radio" name="radio" value="3">
<input type="hidden" name="point" value="8">

elc ...

As an example , when i Choose Second radio , passing Correct value of radio 2 and passing value of hidden 3 -> " that's not Correct value , must be 5 " . every and any Choose for radio , passing with it first value of hidden on page -> 3 , when i Change hidden input to radio input and Choose it , passing the Correct value without any problem ... so this problem happen when the input is hidden .. why ? and Solution ?

Upvotes: 2

Views: 4276

Answers (4)

Jorge Campos
Jorge Campos

Reputation: 23381

So as discussed on the comments. I will show my approach.

So as you mentioned that you have a mysql query to create your inputs (point and radio) I think that is something like:

Please disconsider syntaxe as it been a while since I programmed on php

$query = "select radio, point from someTable where someconditions_here ";
while( /*there is some row*/ ){ 
    echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
    echo "<input type=\"hidden\" name=\"point\" value=\"".$row[point]."\">";
}

My suggestion would be you just print the radios and after submit it you check the point associated to it, something like this:

if ( isset($_POST['radio']) ){
    $qryToFindPoint = "select point from someTable where sameconditions_here AND radio = " . $_POST['radio'] ;   
    //do whatever you need here with the selected point
}else{    
   $query = "select radio, point from someTable where someconditions_here ";
   while( /*there is some row*/ ){ 
       echo "<input type=\"radio\" name=\"radio\" value=\"".$row[radio]."\">";
   }
}

This is just the idea. Of course you should use proper mysqli or PDO functions to do your queries and avoid sql injection.

With this approach you would avoid also a HTML injection. Say that in your final result as another answer suggests has this:

<input type="hidden" name="point1" value="4" />

Anyone can edit the html code and change this value to lets say:

<input type="hidden" name="point1" value="400000" />

And then submit it. As you would not check, the value of the point it would not be the right one.

Upvotes: 0

ameenulla0007
ameenulla0007

Reputation: 2683

you can rename point to point[radio buttons value]

so that when you request point as array, you can get the value from it something like this @$_REQUEST["point"][@$_REQUEST["radio"]];

Upvotes: 0

SyntaxTerror
SyntaxTerror

Reputation: 356

All of your form elements of the same input type share the same name. The name field of the form elements are the keys of the key/value pairs in form submissions. Thus, when you have 3 hidden input fields with the same value for the name field, it must choose one. Remember, the hidden input fields do not correlate at all with the radio input fields which are simply organized close to each other in the code. Your browser does not know that your point fields have anything to do with the radio fields.

You should have unique names for all your input fields, like so

<input type="radio" name="radio" value="1">
<input type="hidden" name="point1" value="3">

<input type="radio" name="radio" value="2">
<input type="hidden" name="point2" value="5">

<input type="radio" name="radio" value="3">
<input type="hidden" name="point3" value="8">

And then you can retreive their values with

$_POST['radio']
$_POST['point1']

and so on.

EDIT: However, that does mean that for every form submission, EVERY hidden field's data will be sent every time, regardless of which radio input is selected. To solve this, you can intercept the form submission on the front end with an event listener such as .submit(), and then disable the input fields you do not want to be submitted prior to allowing the form submission to go through.

Upvotes: 0

jsickles
jsickles

Reputation: 431

Radio inputs share a name in order to define the group.

Hidden inputs cannot share names, as they are discreet entities.

I'd suggest appending the $awardid to the hidden input's name.

<input type="hidden" name="point<?php echo $awardid; ?>" value="<?php echo $point ; ?>" />

Then, you can get the value of that particular input based on the selected radio button.

$radio = $_POST['radio'];
$point = $_POST['point'.$radio];

Upvotes: 1

Related Questions