I am posting a value from a radio button , its value is 1 , but when I echo var_dump($_POST) , then it shows as "on" instead of 1 . Please help. I need to get 1 instead of "on".
<input type="radio" id = "item" value="<?php echo $ItemID[0]; ?>"
name="ItemID"><label for="ItemID"><?php echo $ItemIDName[0]; ?></label>
BROWSER GENERATED HTML
<input type="radio" id = "ITEMA"
value="1" name="ITEM"><label for="ITEMA">A</label>
<input type="radio" id = "ITEMB" value="2" checked="checked" name="ITEM" ><label for="ITEMB">B</label>
<input type="radio" id = "ITEMC" value="3" name="ITEM" >
<label for="ITEMC">C</label>
The default value for radio button is "on" ! Check your php code $ItemID[0] if it's really being set to 1 !
You need to set the value for the radio button to something like value="1", it will only be used when checked
The value setting defines what will be submitted if checked.
Related
I have a page to view assets with an Edit link. When I click the link it goes to edit_case.php which has a form to edit what elements of the row are in the database as checkboxes. However the boxes do not show them as checked. I have the following code...
// get already checked box values
$repop = "SELECT * FROM case_audit WHERE case_id = $case_id";
$popresults = mysqli_query($dbc, $repop);
$row = mysqli_fetch_array($popresults, MYSQLI_ASSOC);
print_r ($row);
The print_r does show the whole record row from DB. which is either a 1 or 0, checked || not checked.
The form...
<div id="facepics">
<label><input type="checkbox" name="Facial1" value="<?php echo $row['frontrest']; ?>" >Front at Rest </label><br>
<label><input type="checkbox" name="Facial2" value="<?php echo $row['frontbigsmile']; ?>" >Front Big Smille</label><br>
<label><input type="checkbox" name="Facial3" value="<?php echo $row['profile']; ?>" >Profile</label><br>
<label><input type="checkbox" name="Facial4" value="<?php echo $row['subvertex']; ?>" >SubMento Vertex</label><br>
</div>
I know I need to turn the 1's to "checked" just not sure how best to do that.
so basically checked="true" attribute in input creates a checked checkbox.
HTML Code looks like
<input type="checkbox" checked="true">
In your case you can do it like:
<input type="checkbox" name="Facial1" value="frontrest" <?= (intval($row['frontrest']) == 1) ? 'checked' : '';>
Also note that I changed value attribute, with frontrest so that you can identify the checkbox uniquely
EDIT: I have modified the code
<input type="checkbox" name="Facial1" <?=$row['frontrest']==1?'checked':''?>>
I often have the same issue where the browser ignores checked="false" and checks all
so I use
<input type="checkbox" checked>
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 ?
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];
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.
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"]];
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.
I have a form where members details are entered,
If i click submit it takes all values and stores in db. But if i click edit it is retrieving all values except radio button value.
So, how to fetch the radio button value while editing the page?
This is my code where i try to fetch the value of radio button it fetches value but it is not fetching the value in radio button
<td>Gender</td>
<td><input type="radio" name="gender" value="<?php echo $gender; ?>" />Male
<input type="radio" name="gender" id="female" value="<?php echo $gender; ?>" >Female
</td>
When you send data through PHP and HTML you just get it like this :
$var = $_GET['field']; //if you used GET method
But it works with everything, not only text. The fact is just you have to name your radiobutons and get their values :
<input type="radio" name="radio1" /><input type="radio" name="radio2" /> and after in PHP you have to know which one was checked, basically (I'm not sure but...) it will work like that :
if ($_GET['radio1'] == 'on' /*on is valable for checkboxes*/) { /* your stuff here */ }
If I am correct, I think what you are doing incorrectly is you need to name all of your radio buttons the same with seperate values.
i.e.
<input type="radio" name="difficulty" value="Easy">
<input type="radio" name="difficulty" value="Medium">
<input type="radio" name="difficulty" value="Hard">
Now $_POST['difficulty'] should grab the data of the radio.
I know how to it with text inputs. I can easily put a php script in its value, but doing it with input groups seems different. How can I mantain the values of group inputs if the submission of the form fails?
To re-mark a checkbox or radio button as checked, you use this code:
<input type="checkbox" name="foo" id="foo" checked="checked"/>
The key is checked="checked".
If you are using groups of checkboxes, make sure the name of the field ends with brackets [], like this:
<input type="checkbox" name="foo[]" id="foo_1" value="1" checked="checked"/>
<input type="checkbox" name="foo[]" id="foo_2" value="2" checked="checked"/>
Then your $_REQUEST['foo'] variable will automatically be an array of checked values. You can use in_array to see if a particular checkbox was checked.
Update based on comment
Here's how I would set it:
<input type="checkbox" name="foo[]" id="foo_1" value="1" <?= (isset($_POST['foo'] && in_array('1', $_POST['foo'])) ? 'check="checked"' : '' ?>/>
For single items (like radios), use this:
<input type="radio" name="foo" id="foo" value="1" <?= isset($_POST['radio]) ? 'check="checked"' : '' ?>/>
Hope that helps.
Update 2:
Also, make sure you escape user input! Your example should look like this:
<input type="text" name="username" value="<?php if(isset($_POST['username']) echo htmlspecialchars($_POST['username']);?>">
Always assume the user is trying to hack your system, always escape user input!
Print the " checked" attribute for radio buttons and checkbox input tags, or the " selected" attribute for dropdown option tags.
I would like to check whether user checked box or not with php code. how can I do this?? and if checked then what kind of value I will get??
<input name="accept" type="checkbox" class="tickbox" value="" />
In this specific case, you will get $_POST['accept'] == '' , which is immensely un-useful.
You'll want to add a value to that tag:
<input name="accept" type="checkbox" class="tickbox" value="1" />
With that value, you'll get $_POST['accept'] == '1' when the checkbox is checked, and no 'accept' key at all when the checkbox is not checked.
<input type="hidden" name="accept" value="0" />
<input type="checkbox" name="accept" value="1" />
if unchecked : return hidden field’s value => 0
if checked : return checkbox’s value => 1
You need to have a value assigned to the checkbox. If the check box is ticked, this value will be returned when the form is submitted. The other option is to use a Javascript to check this before submission.