I trying to create dynamic server side select input, after i submit, the set_value('nilai[]') not showing any value.
Here's my Controller below:
$this->load->library('form_validation');
$this->form_validation->set_rules('nilai[]', 'Nilai Pantuhir', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('pantuhir/pantuhir_form');
} else {
$list_pantuhir = $this->input->post('nilai');
foreach ($list_pantuhir as $key => $value) {
echo $value."<br />";
}
}
Here's my view below :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
I want to show set_value and get selected in option field if validation not correct.
Hope this will help you :
Use set_select instead of set_value. If you use a menu, this function permits you to display the menu item that was selected, after the form validation throws any error
It should be like this :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select name="nilai[]" >
<option value="" >---Choose----</option>
<option
value="<?=$rowPerson['intUserId'].'-A';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-A');?>
>A</option>
<option
value="<?php echo $rowPerson['intUserId'].'-B';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-B');?>
>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html#set_select
Use this code in the view
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]',rowPerson['intUserId'].'-'.'A') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]',$rowPerson['intUserId'].'-'.'B') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
Related
Is there any way to set the selected item in a drop down box using the following 'type' code?
<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>
The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?
You need to set the selected attribute of the correct option tag:
<option value="January" selected="selected">January</option>
Your PHP would look something like this:
<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>
I usually find it neater to create an array of values and loop through that to create a dropdown.
You mark the selected item on the <option> tag, not the <select> tag.
So your code should read something like this:
<select>
<option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
<option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
...
...
<option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>
You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.
You can use this method if you use a MySQL database:
include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
if ($_GET['to'] == $row['id'])
{
$selected = 'selected="selected"';
}
else
{
$selected = '';
}
echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);
It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...
Simple and easy to understand example by using ternary operators to set selected value in php
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
<option value="<?php echo $id;?>" <?php echo ($id== '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.
function selectdCheck($value1,$value2)
{
if ($value1 == $value2)
{
echo 'selected="selected"';
} else
{
echo '';
}
return;
}
and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns
<option <?php selectdCheck($row[month],january); ?> value="january">january</option>
:) I hope this function help others
Simple way
<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">
<option value=""><?php echo "Select"; ?></option>
<?php foreach ($dropdowndetails as $dropdowndetails) { ?>
<option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>
This is the solution that I came up with...
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select_month">
<?php
if (isset($_POST['select_month'])) {
if($_POST["select_month"] == "January"){
echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
}
elseif($_POST["select_month"] == "February"){
echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
}
}
else{
echo '<option value="January">January</option><option value="February">February</option>';
}
?>
</select>
<input name="submit_button" type="submit" value="Search Month">
</form>
A Simple Solution:
It work's for me
<div class="form-group">
<label for="mcategory">Select Category</label>
<select class="form-control" id="mcategory" name="mcategory" required>
<option value="">Please select category</option>
<?php foreach ($result_cat as $result): ?>
<option value="<?php echo $result['name'];?>"<?php
if($result['name']==$mcategory){
echo 'selected';
} ?>><?php echo $result['name']; ?></option>
}
<?php endforeach; ?>
</select>
</div>
Check this:
<select class="form-control" id="department" name="department" type="text">
<option value="medical-furniture" #if($list->department == "medical-furniture") selected #endif>Medical furniture</option>
<option value="medical-table" #if($list->department == "medical-table") selected #endif>Medical table</option>
<option value="service" #if($list->department == "service") selected #endif>Service</option>
</select>
My suggestion is to leverage the hidden/collapse attribute. Try with this example:
<select>
<option value="echo $row[month]" selected disabled hidden><? echo $row[month] ?></option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
</select>
in case of null for $row[month] the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc...
If you have a big drop down. it's much easier to use jQuery with PHP.
This is how to do it:
<script>
$(document).ready(function () {
$('select[name="country"]').val('<?=$data[0]['Country']?>');
});
</script>
The easiest solution for the selected option in dropdown using PHP is following
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
You can try this after select tag:
<option value="yes" selected>yes</option>
<option value="no">no</option>
I have some options to select and would like to set some textinputs to visible or hidden depending on what user selects. I have the functionality already but in this case I need to take the second value of the echo and check if its equal to something, I can currently only check the first value of the echo:
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
So, I need to get ONLY the value of $row['serial_or_imei'] and compare onchange="if (this.value=='') , how can I do that?
EDIT: further explanation
I need to read the value of $row['serial_or_imei'] which could be empty, 'serial' OR 'imei', so, one of these three.
P.S: I must keep the $row['id'] in the value attribute because I am passing that value later to server so dont suggest removing that.
You can add one attribute to option element and test with it. something like
<select id="type_id" name="device_type_id" onchange="if (this.getAttribute('data')==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option data="<?php echo $row['serial_or_imei'] ?>" value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You can compare the value of an array in the onchange by assign the value to a variable which will be a quite neat format as
<?php $value = echo $row['serial_or_imei']; ?>
<select id="type_id" name="device_type_id" onchange="if (this.value=='<?php empty($value)?>'){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You add a new custom attribute serial and supply the value of serial_or_imei to it and then compare in javascript with onchange="if (this.value=='')
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-serial="<?php echo $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
EDIT: Attribute “serial” is not allowed on element “option”. Changed this to data-seial.
You say you need the values back to server because they change dynamically. A clean way would be to have a dedicated hidden field:
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
Here's a full self-contained script to illustrate it:
<?php
$random_serial_or_imei = range(1000, 9999);
shuffle($random_serial_or_imei);
$result = [];
for ($i = 0; $i < 5; $i++) {
$result[] = [
'id' => $i,
'serial_or_imei' => $random_serial_or_imei[$i],
'name' => "Name #$i",
];
}
?>
<select name="device_type_id">
<option value="">Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
This prints something like:
<select name="device_type_id">
<option value="">Select</option>
<option value="0">Name #0</option>
<option value="1">Name #1</option>
<option value="2">Name #2</option>
<option value="3">Name #3</option>
<option value="4">Name #4</option>
</select>
<input type="hidden" name="map[0]" value="9513">
<input type="hidden" name="map[1]" value="3931">
<input type="hidden" name="map[2]" value="3971">
<input type="hidden" name="map[3]" value="4476">
<input type="hidden" name="map[4]" value="6429">
Selected type can be found at $_POST['device_type_id'] and the corresponding IMEI at $_POST['map'][ $_POST['device_type_id'] ].
Based on the information from the comments, I think this solution is what you are looking for. Note I move the onchange to a function for clarity.
You can see the "three" option hides the text input as it doesn't have a imei.
Also notice how I use the data- attribute inside your option so you can retrieve that information as well, independent of the option id. Also note the this.form['device_imei_textinput'] didn't worked. The elements reference is required.
function check_imei(obj, form) {
var imei = obj.options[obj.selectedIndex].getAttribute('data-imei');
if (imei == '') {
form.elements.device_imei_textinput.style.visibility='hidden';
} else {
form.elements.device_imei_textinput.style.visibility='visible';
}
}
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<option value="1" data-imei="foo">One</option>
<option value="2" data-imei="bar">Two</option>
<option value="3" data-imei="">Three</option>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Mirror of example : https://jsfiddle.net/asp1hewx/
EDIT : To address the PHP side of things, the form above could be generated with :
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-imei="<?php $row['serial_or_imei'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Here is the code that i am using for drop down to select.
<select name="formreg" value="">
<option value="Registered" <?php if($f_reg == 'Registered') { ?> selected <?php echo $f_reg; } ?> >Registered</option>
<option value="NonReg" <?php if($f_reg == 'NonReg') { ?> selected <?php echo $f_reg; } ?> >NonReg</option>
</select>
<select name="regform">
<option value="Registered" <?php if (isset($register) && $register=='Registered') {?> selected='selected' <?php echo $register; }?>>Registered</option>
<option value="NonReg" <?php if (isset($nonreg) && $nonreg == 'NonReg') {?> selected='selected' <?php echo $nonreg;} ?>>NonReg</option>
</select>
At the top of script:
$f_reg = $_POST["f_reg"]; // or $f_reg = $_GET["f_reg"] if you pass parameters as the url part
then your < select > tag:
<select name="formreg">
<option value="Registered" <?php if ($f_reg == "Registered") print "selected"?>>Registered</option>
<option value="NonReg" <?php if ($f_reg == "NonReg") print "selected"?>>NonReg</option>
</select>
I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>
Once submitted selected option, the data is not stored.
just want to know how to post back the data if validation fails
The following line doesnt really work for me.
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
if someone could give me a hand?
Many thanks, here is my code
<?php
if(isset($_POST['numbers']) &&($_POST['fruits']) && $_POST['numbers'] != "null" && $_POST['fruits'] !== "null")
{
echo "Thank you!";
} elseif (isset($_POST['numbers']) && $_POST['numbers'] = "null") {
echo "you forgot to choose a number";
}
elseif(isset($_POST['fruits']) && $_POST['fruits'] = "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
<option value="null" selected="selected">-</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<select id="fruits" name="fruits" value="<?php echo (isset($_POST['fruits']))? $_POST['fruits'] : ''; ?>"/>
<option value="null" selected="selected">-</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
<input type="submit" value="Send" />
</form>
Solved it, Maybe not a best way, but at least got it sorted:
<?php
$item = null; #
$itemyear = null;
if(isset($_POST['numbers'])){
$item = $_POST['numbers'];
}
if(isset($_POST['fruits'])){
$itemyear = $_POST['fruits'];
}
if(isset($item) && isset($itemyear) && $item != "null" && $itemyear !== "null")
{
echo "Thank you!";
} elseif ($item == "null") {
echo "you forgot to choose a number";
}
elseif($itemyear == "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" />
<option value="null" selected="selected">-</option>
<option value="01" <?php if($item == '01'): echo "selected='selected'"; endif; ?>>01</option>
<option value="02" <?php if($item == '02'): echo "selected='selected'"; endif; ?>>02</option>
<option value="03" <?php if($item == '03'): echo "selected='selected'"; endif; ?>>03</option>
</select>
<select id="fruits" name="fruits" />
<option value="null" selected="selected">-</option>
<option value="Apple"<?php if($itemyear == 'Apple'): echo "selected='selected'"; endif; ?> >Apple</option>
<option value="Banana"<?php if($itemyear == 'Banana'): echo "selected='selected'"; endif; ?>>Banana</option>
<option value="Pear"<?php if($itemyear == 'Pear'): echo "selected='selected'"; endif; ?>>Pear</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
echo $item ."-". $itemyear;
?>
the PHP isset() function returns either true or false (depending on whether the input is.. well... set.
You would want to use this:
value='<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : ""; ?>
You can't set a value attribute on a <select>. You have to find the correct <option> and set its selected attribute. Personally, I put a data-default attribute on the <select> and then use JavaScript to loop through the options and find the right one.
Oh now I see.
I don't know what the usual thing to do is but one way is to put all the data as a query string when you redirect the user back. The reason why it doesn't stay in the $_POST global is because it's only kept on the page you post to then it's gone.
So when you redirect the user back
if(validationFailed)
{
header("Location: page.php?data=example");
}
The data can then be retrieved in page.php by using
$data = $_GET['data']; // contains "example"