set_select() not working in codeigniter framework - php

I want the select box show the same value before the from submit after the redirection in this code $cat value come to the session i am not using validation rule for select box i want only the select box not change after the redirection
Select Leave Category...
leave_category_id ?>"
<?php echo set_select('leave_category_id', $cat, true); ?>> <?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>

Try
<select name="leave_category_id" class="form-control" onchange="check_leave_category(this.value)" required >
<option>Select Leave Category...</option>
<?php foreach ($all_leave_category as $v_category) :
// Using short tag
//$v_category->leave_category_id == $cat ? $selected = 'selected' : $selected = '';
// without using short tag
if($v_category->leave_category_id == $cat) $selected = 'selected'; else $selected = '';
?>
<option value="<?php echo $v_category->leave_category_id ?>" <?php echo $selected?> > <?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>

There are two things to keep in mind: First you have to use the validation class to be able to use the set_select() function. Second you have to pass the database information to that function, like this:
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', ($model->selection == 'one')); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two', ($model->selection == 'two')); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three', ($model->selection == 'three')); ?> >Three</option>
</select>
If $model->selection is two, then the second option will be selected in the example above.
see the link

Related

Whats the best way to show selected options on a form when reloaded using PHP

I would like the select option the user selected to be shown when the form is reloaded. I am currently using bootstrap select picker to style the drop down. Right now I am creating an array and echoing out a selected class based on the value stored in the database.
This is not a major problem with a small amount of options like the code provided below, however with a select box with larger amounts of options this obviously amounts to lots of extra code.
There must be a smarter way to do this than the code i'm using below. Can anyone help?
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == '') echo "selected='selected'"; ?>>Please Select</option>
<option value="Yes" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == 'Yes') echo "selected='selected'"; ?>>Yes</option>
<option value="No" <?php if ($user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'] == 'No') echo "selected='selected'"; ?>>No</option>
</select>
If the option's are static you could use ternary operators, so it will be something like :
<?php $selected_option = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email']; ?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="" <?php echo $selected_option==''?'selected':''; ?>>Please Select</option>
<option value="Yes" <?php echo $selected_option=='Yes'?'selected':''; ?>>Yes</option>
<option value="No" <?php echo $selected_option=='No'?'selected':''; ?>>No</option>
</select>
Hope this helps.
<?php
$text = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value="">Please Select</option>
<option value="Yes" <?php echo $text == 'Yes' ? 'selected' : ''; ?>> Yes </option>
<option value="No" <?php echo $text == 'No' ? 'selected' : ''; ?>> No </option>
</select>
Alternative Way
"..however with a select box with larger amounts of options this
obviously amounts to lots of extra code.".
In this case, when you are having lots of data for dropdown. You can create a table where you can have both option name and option value.
job_specification_table
id option_value option_name
1 Please Select
2 Yes Yes
3 No No
.
.
.
Then, retreive those values and check for user_data and according to it, option will get selected.
<?php
$sql = "SELECT * FROM job_specification_table";
$result = mysqli_query($db_con,$sql);
$text = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
?>
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<?php
while ($row = mysqli_fetch_array($result)){
$option_value = $row['option_value'];
$selected = ($option_value == $text) ? "selected" : "";
?>
<option value="<?=$option_value;?>" <?=$selected;?>>
<?=$row['option_name'];?>
</option>
<?php }?>
</select>
Here is more readable solution:
<select class="selectpicker" name="Do_You_Have_A_Job_Spec_You_Can_Email">
<option value=
<?php
$choise = $user_data_array['Do_You_Have_A_Job_Spec_You_Can_Email'];
switch ($choise) {
case 'Yes':
echo '"Yes" selected="selected" >Yes';
break;
case 'No':
echo '"No" selected="selected" >No';
break;
case '':
echo '"" selected="selected" >Please Select';
break;
}
?>
</option>
</select>

Php string replace of values

In opencart shop i have a product limit in category page, for example the default number of products per page.I setet up the maximul number to be 1000, but i wanna strig replace that number to "ALL".
Belo is the code for the string. <?php echo $limits['text']; ?>
I wanna string replace $limits['text'] for the value 1000 to be ALL
<select onchange="location = this.value;">
<?php foreach ($limits as $limits) { ?>
<?php if ($limits['value'] == $limit) { ?>
<option value="<?php echo $limits['href']; ?>" selected="selected"><?php echo $limits['text']; ?></option>
<?php } else { ?>
<option value="<?php echo $limits['href']; ?>"><?php echo str_replace('1000', 'ALL', $limits['text']); ?></option>
<?php } ?>
<?php } ?>
</select>
This works but in the link of the page it sould be like this
/index.php?route=product/category&path=142#/sort=p.sort_order/order=ASC/limit=1000
but is like this
/index.php?route=product/category&path=142#/sort=p.sort_order/order=ASC/limit=All
In inspect element the code is generated corectly
<select>
<option value="http://diamanti.teamweb.ro/index.php?route=product/category&path=142&limit=30" selected="selected">30</option>
<option value="http://diamanti.teamweb.ro/index.php?route=product/category&path=142&limit=90">90</option>
<option value="http://diamanti.teamweb.ro/index.php?route=product/category&path=142&limit=1000">All</option>
</select>
But when i click ALL , the link modifiecs not with the "value" But with the text...

how to set_value use for dropdown input using codeigniter?

I want to used set_value for input where type=text and i have no problem with that.
I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .
My code:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
if($item->sport_name==$mySport)
{?>
<option value="<?php echo $item->sport_id; ?>" selected><?php echo $item->sport_name; ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id; ?>"><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
You Can try This:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):?>
<option value="<?php echo $item->sport_id; ?>" <?php if($item->sport_name==$mySport)
{ echo "Selected";} ?>><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
As per the docs, you would not use set_value for a select element:
"Permits you to set the value of an input form (text input) or textarea.".
Rather, you would use set_select.
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
set_select($field[, $value = ''[, $default = FALSE]])
Parameters:
$field (string) – Field name
$value (string) – Value to check for
$default (string) – Whether the value is also a default one
Returns: ‘selected’ attribute or an empty string
Return type: string
Example:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
<option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
</select>
your code should be work correctly, but best way: you can declare var called $selected and make comparision to assign it with selected word inside loop when the value of select == current selected value:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php
foreach($getSport as $item):
$selected='';
if($item->sport_name==$mySport)
{
$selected='selected';
}
?>
<option value="<?php echo $item->sport_id; ?>" <?php echo $selected; ?> >
<?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
The best way of doing this, while remaining readability would be as follow:
<select class="form-control" name="sport_name" id="sport_name">
<option selected>--Select Sport--</option>
<?php foreach($getSport as $item): ?>
<option <?php if($item->sport_name == $mySport){ echo "selected"; } value="<?php echo $item->sport_id;?>"><?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
Ofcourse this is thinking you've got your data correctly set as objects or if you have arrays you'd use
<?php echo $item['sport_name']; ?>

Using Selected="Selected" from dynamic list of options

I'm posting variables from my form on to another form (a search records form), this will have all the variables that are being posted to the current form and place within INPUT field or SELECT fields. Any fields which are INPUT type I can POST to using the Value="" section of the INPUT. Now for SELECT I have a little problem, any small list that I prepopulate in the code are fine I have used this code below
<label>Fuel</label>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>
As I said when I run the Sear records form and return the posted variables, this works great. My problem is with big data sets within SELECT which are from the database, this is what I have currently, which is perfect to get data into a SELECT but how can I post a variable to it in a similar way to the previous code?
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) { echo 'Problem'; } else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option>
<?php } } ?>
</select>
Like this:
<option
value="<?php echo $fueltype->PropertyFuelType; ?>"
<?php echo $fueltype->PropertyFuelType == $searchfuel ? "selected" : ""; ?>
><?php echo $fueltype->PropertyFuelType; ?></option>
Having said that, I would recommend using a PHP templating system; or at least convert this logic into a function.

How to make a form remember what was selected in a form with PHP?

I have a form that validates the information submitted within a form, method is POST and is submitted to another file "Check.php", I am using sessions to remember what the POST data is, when ever there is a problem with the form I redirect it back to the form input page.
How do you get the post data to remember which OPTION was selected in the SELECT input?
CODE:
<select name="referred">
<option value="facebook">facebook</option>
<option value="youtube">youtube</option>
<option value="reddit">reddit</option>
<option value="google">google</option>
</select>
I've tried to do this but does not work:
<select name="referred">
<option value="facebook"
<?php
if (isset($_POST['referred']) && $_POST['referred'] == "Facebook") {
print "selected=\'selected\'";
}
?>?> >facebook</option>
I've tried to do this but still, does not work :(
<select name="referred">
<option value="facebook"
<?php
if (isset($_POST['referred'])) {
if ($_POST['referred'] == "youtube") {
echo "selected=\'selected\'";
} else {
echo "";
}
}
?> >facebook
Please help!
EDIT:
I APOLOGISE, I'm not using the $_POST here to check if it's set, I should be using the $_SESSION, see below:
<select name="referred">
<option name="google"<?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "google"){ echo "selected='selected'";}else{ echo "";}}?>>google</option>
<option name="youtube"<?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "youtube"){ echo "selected='selected'";}else{ echo "";}}?>>youtube</option>
<option name="reddit" <?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "reddit"){ echo "selected='selected'";}else{ echo "";}}?>>reddit</option>
<option name="facebook" <?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "facebook"){ echo "selected='selected'";}else{ echo "";}}?>>facebook</option>
</select>
separate your business logic from presentation logic
define your variables.
avoid repetitions.
the code
$form['referred'] = array('facebook','youtube','reddit','google');
if ($_SERVER['REQUEST_METHOD']=='POST') {
//form validations
//if failed, fill variables with actual values
$form['ref'] = $_POST['referred'];
} else {
//filling variables with empty values
$form['ref'] = '';
}
the template
<select name="referred">
<? foreach($form['referred'] as $val): ?>
<option<? if($form['ref'] == $val):?> selected="selected"<? endif ?>><?=$val?></option>
<? endforeach ?>
</select>
You don't need to escape single quotes in a string wrapped in double quotes.
echo "selected=\'selected\'" // incorrect
Like that your html markup would end up like <option value="facebook" selected=\'selected'\> Which is bad html.
This should work.
EDIT
<select name="referred">
<?php
if (!empty($_SESSION['referred']) && 'option1' == $_SESSION['referred']) {
$selected = 'selected="selected"';
} else {
$selected = null;
}
?>
<option value="option1" <?= $selected; ?>>option1</option>
....
</select>
If you have a known set of options then make an array and loop through them:
<?php
$options = array(
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3' // ...
);
$referredIsset = isset($_SESSION['referred']);
?>
<select name="referred">
<?php foreach ($options as $value => $name): ?>
<?php
$sel = ($referredIsset && $_SESSION['referred'] == $value)
? 'selected="selected"'
: null;
?>
<option value="<?= $value; ?>" <?= $sel; ?>>
<?= $name; ?>
</option>
<?php endforeach; ?>
</select>
First of all var_dump($_POST); to see what you get after submitting your form.
(You don't need the SESSION to store your POST data. Everything you need is in $_POST. And if an error occurs you can redirect to your form without the need to store the information additionally in the session.)

Categories