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"];
}
Related
So, I have a questions regarding the input type radio in HTML. As you know, you can put checked as a value, this will mark it as checked.
The story is I am getting a 0 or 1 value from my database. I am then checking if it's 0 or 1 and will then mark one of the radio button's as checked.
My code is as follows:
<?php if($pay_op == 0) { ?>
<input type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input type="radio" value="other" id="other_op">Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } elseif ($pay_op == 1) { ?>
<input type="radio" value="paypal" id="pay_op">PayPal<br />
<input type="radio" value="other" id="other_op" checked>Other<br/>
<input type="submit" id="pay_op_submit" />
<?php } ?>
My problem now is, whenever I try to mark the other radio button as checked by clicking on it, both radio buttons are checked?
I thought that this might have something to do with me checking if the value returned from the database is 0 or 1 and it will keep one of the radio buttons checked until that value is changed. Now my question is, does anyone know a solution to this issue so that whenever someone clicks on something different than the default checked radio button it will actually check that one and not both of them?
Any tips are highly appreciated! =)
Thanks!
Radio buttons work basically as a named group. The browser only un-checks a radio button if it is linked to the other radio buttons with a property called name.
<?php
if($pay_op == 0)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op" checked>PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op">Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
elseif($pay_op == 1)
{ ?>
<input name ="myGroup" type="radio" value="paypal" id="pay_op">PayPal<br />
<input name ="myGroup" type="radio" value="other" id="other_op" checked>Other<br/>
<input name ="myGroup" type="submit" id="pay_op_submit" />
<?php
}
?>
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.
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.
I have a radio button array that I need help with. Here is the code:
<input type="radio" name="radio" id="academic" value="1"<?php
if ($row_EventInfo['academic'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['academic'] = '';}
?>>
<label for="academic">Academic</label><br />
<input type="radio" name="radio" id="personal" value="1"<?php
if ($row_EventInfo['personal'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['personal'] = '';}
?>>
<label for="personal">Personal</label><br />
<input type="radio" name="radio" id="diversity" value="1"<?php
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
?>>
<label for="diversity">Diversity</label><br />
What I'm trying to do is this. I have a column in my database table for each radio button because we have to have their inputs separately. However, I want them to only be able to select one of the buttons at a time. I changed all the names to be the same ("radio"), but since PHP MYSQL uses the names to know where to post the information in the table it doesn't know where to post.
Is there any way to create an if statement to tell it to only allow one button at a time to be selected and keep the inputs separate for the database table?
Please let me know if you need clarification. Thanks!
set the value to the column name, eg
<input type="radio" name="radio" id="diversity" value="diversity"
on the php end, simply do something like
$sql = "UPDATE table SET {$_POST['radio']} = 1";
this is in a form, right?
Then you get the data in the POST. I wouldn't set the value to 1, set the value to the value you want to select, e.g.
Then in the post, get the value and use this to set the data in the DB.
Hope this helps!
BR,
TJ
If you use:
<input type="radio" name="radio" id="diversity" value="value1">
<input type="radio" name="radio" id="diversity" value="value2">
<input type="radio" name="radio" id="diversity" value="value3">
<input type="radio" name="radio" id="diversity" value="value4">
it will be available in $_POST['radio']; with what ever radio button you selected. eg.value3 dont confuse yourself by naming input types with the same name
If you use:
<input type="radio" name="myradio" id="diversity" value="value1">
It will be available in $_POST['myradio'];
and so on
plus you may want to look into using the ternary operator
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
into
echo ($row_EventInfo['diversity'] == '1') ? ' checked="checked"':'';