hello
i am not being able to submit these value in my controller.
the code is as follows
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="1" checked="" required="" />
<span class="lbl"> YES</span>
<input name="status.<?php echo $data['LEAVE_ID']?>" type="radio" class="ace" value="0"/>
<span class="lbl"> NO</span>
For multiple radio use array in name attribute with id as a index key like this
name="status[<?php echo $data['LEAVE_ID']?>]"
You can achieve it using below code, you just have to add css.
<?php
foreach($dataArrry as $data) {
echo $_POST['status.' . $data['LEAVE_ID']]."<br/><br/>";
}
?>
Related
I have a HTML form with multiple checkbox selection. They are defined as:
<label class="container">Afghanistan
<input type="checkbox" id="Afghanistan" name="country[]" value="Afghanistan" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">Armenia
<input type="checkbox" id="Armenia" name="country[]" value="Armenia" checked="checked">
<span class="checkmark"></span>
</label>
...
After submitting, I call a PHP file where I want to store their values in an array.
for($i=0;$i<sizeof($_POST["country[]"]);$i++){
$country[i] = htmlspecialchars($_POST["country[i]"]);
}
But this code doesn't work. Can anyone help me to solve it?
$_POST["country"] is an array for which you can get the values using the index using $i
Try it like this:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
for($i=0;$i<sizeof($_POST["country"]);$i++){
$country[] = htmlspecialchars($_POST["country"][$i]);
}
echo $country[0];
}
I have a dynamic number of checkbox in my view,like this:
<?php foreach($documents as $row): ?>
<input type="checkbox" name="options[]" value="<?php echo $row->docu_title?>"><?php echo $row->docu_title?><?php endforeach; ?>
And I set the rule for this group of checkboxes to be required in my controller:
$this->form_validation->set_rules('options[]','options', 'required');
How will i know which checkboxes are checked? so whenever there are errors on the other fields i can still show the user the checkboxes that has been checked already. like this:
<input style="" type="text" class="form-control" name="ClientName" id="ClientName" value="<?php echo set_value('ClientName'); ?>">
You could use form helper 's set_checkbox() function.
This permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:
<input style="" type="checkbox" class="form-control" name="ClientName" id="ClientName"
value="<?php echo set_value('ClientName'); ?>" <?php echo set_checkbox('ClientName', '1'); ?> />
For reference visit CodeIgniter User Guide Version 2.2.0
should be something like this
<?php foreach($documents as $row): ?>
<input type="checkbox" name="option[]" value="<?php echo $row->docu_title?>" <?php echo set_checkbox('option[]', $row->docu_title); ?>>
<?php echo $row->docu_title?>
<?php endforeach; ?>
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"];
}
HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>
Is there a read only property for a checkbox?
Because I can still tick on the checkbox even if I have this code, is the read only property only for text box? What's the code if you want the check box to be read-only?
<td><input name="stats1" type="checkbox" id="SSS" readonly="readonly" value="<?php echo $row["STAT"]; ?>" <?php echo $row["STAT"] ? 'checked="checked"' : ''; ?> >SSS</td>
The readonly attribute on HTML input elements actually means that the value is readonly.
You actually want to make a checkbox uncheckable, in that case grab Javascript:
<input type="checkbox" onclick="return false;">
Try disabled="disabled" instead ;)
here i have inserted 7 checkboxes and set their name as c1,c2....c7 in the html page
<input type="checkbox" name="c1" value="english" />
<input type="checkbox" name="c2" value="hindi" />
<input type="checkbox" name="c3" value="gujarati" />
.
.
.
<input type="checkbox" name="c7" value="Marathi" />
here we will use for loop with upto 7 times iteration
for($i=1;$i<=7;$i++)
{
echo $_GET['c'.$i] ."</br>";
}
here 'echo' statement will print values from gettig name of checkboxes c1 ...c7
Here $_GET['c'.$i] will print only its value if that checkboox is checked