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.
Related
I'm using javascript to clone a form according to the int value. I'm using [] brackets for taking array value from other input type.
<input type="text" name="state[]" class="form-control" id="state" required>
and used same for radio input.
<input type="radio" name="gender[]" value="Male">Male
using loop in server side to save those data into db.
for ($i = 0; $i <= $request->limit; $i++) {
$user->name = $request->name[$i];
$user->gender = $request->gender[$i];
$user->address = $request->address[$i];
$user->save();
}
I'm getting error of Undefined offset: 1 on the line:
$user->gender = $request->gender[$i];
as because radio input cannot hold data in array form. When I dump and die request all inputs except radio has data in array form. And value of radio input is succeeded by the value of next radio input of cloned form.
How can I get array value from radio input?
The problem is that if you all your radio buttons have the name gender[], then they are all in the same group, and you can only select one of the options out of all the form clones.
So, for example, if your form looks like this:
<form>
<input type="text" name="name[]" required>
<input type="radio" name="gender[]" value="Male">Male
<input type="radio" name="gender[]" value="Female">Female
<input type="text" name="name[]" required>
<input type="radio" name="gender[]" value="Male">Male
<input type="radio" name="gender[]" value="Female">Female
</form>
If I try to fill it and put "John" in the first name field and then select "Male" from the first two button, then type in "Marry" in the second name field and select "Female" from the last two buttons, the "Male" selection for the first set will disappear - because the browser sees all radio buttons as the same group because they have the same name.
I suggest replacing radio buttons with <select> boxes.
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 have a form such as the one below:
<input type="checkbox" name="type" value="wash"><label>Wash</label><br>
<input type="checkbox" name="type" value="no wash"><label>No Wash</label><br>
<label>Other (Specify)</label><br>
<input name="type"><br>
If you notice for all three i am using "type" as the input name. The point being that the user will be given two options, if none of the two options apply to them they should enter a value in other. Now in the database i have the field type, so if they selected the first two and entered a value in the field or if they only wrote a value in the field i still want it to to be part of the type field. So how can i make it so that if they select the input field it should also insert in "type".
Do you mean something like this?
HTML:
<input type="checkbox" name="type[]" value="wash"/><label>Wash</label>
<input type="checkbox" name="type[]" value="no_wash"/><label>No wash</label>
Other type:
<input type="text" name="other_type"/>
PHP:
if (!empty($_REQUEST['other_type']))
$_REQUEST['type'][] = $_REQUEST['other_type'];
var_dump($_REQUEST['type']);
First of all you should better use radio buttons instead of checkboxes.
Then you could do the following
<input type="radio" name="type" value="wash"/><label>Wash</label>
<input type="radio" name="type" value="no_wash"/><label>No wash</label>
<input type="radio" name="type" value="other"/><label>Other</label>
<input type="text" name="other_type"/>
Your PHP would then look like this:
if ($_REQUEST["type"] == "wash"){
echo "Wash me please";
}else if ($_REQUEST["type"] == "no_wash"){
echo "no wash";
}else if ($_REQUEST["type"] == "other"){
echo "you want to ".$_REQUEST["other_type"];
}
If you use JS you could even disable the textbox unless the user selects the third option.
Edit: If I got your comment right it would be the easiest like this:
<input type="checkbox" name="wash_me"/><label>Wash your car?</label>
<input type="text" name="other"/><label>What else can we do for you?</label>
PHP
if (isset($_REQUEST["wash_me"]){
echo "wash my car please";
}
if (strlen($_REQUEST["other"]) != 0){
echo "and do the following: ".$_REQUEST["other"];
}
Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.
I have a multiple checkbox option on my page...
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
</li>
I'm not sure however how to collect all checkbox values using POST method?
if i use
$service = $_POST['service'];
I only get 'other' returned
Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:
Check if a certain value was selected:
if (in_array("Other", $_POST['service'])) { /* Other was selected */}
Get a single newline-separated string with all selected options:
echo implode("\n", $_POST['service']);
Loop through all selected checkboxes:
foreach ($_POST['service'] as $service) {
echo "You selected: $service <br>";
}
Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the
<input type="text" name="other" style="display:none;"/>
and you can show it with javascript when the "Other" box is checked. Something like that.
Just make the name attribute service[]
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br />
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br />
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br />
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br />
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>
Then in your PHP you can access it like so
$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes
etc...
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.
Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"
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.