Codeigniter, problems using set_select() and set_checked() - php

I have a bit of an issue using codeigniter's set_select and set_checked within my forms, I am adding these to my existing forms as I am the stage in development where I am trying to tidy things up and failed validation resetting forms was not a big issue when I was still working on the project but now its coming to a close its become a major headache.
Firstly the set_select, I have this code which outputs me drop down from an array which is passed to the view from the controller which gets the results from a table in my database, the form I am implementing this in has 10 drop down boxes each corresponds to a table in my database. Anyway this is the code:
<label for="rating">Rating: </label>
<select name="rating">
<?php
if(isset($rating) && $rating != 'none') {
echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
foreach($rating as $row) {
echo '<option value="'.$row->door_rating_rating.set_select('rating', $row->door_rating_rating).'">'.$row->door_rating_rating.'</option>';
}
} else {
echo '<option value="none">Nothing to list</option>';
}
?>
</select>
It is just not working and as far as I can see there shouldn't be a problem with my code but this is the first time I have used this and I have looked at examples of using it but could not find an example of using it in a for loop so is what I am doing even possible?
This is my set_checked code within the view and this too is not working after failed validation:
Temporary Refuge Door?<input type="checkbox" class="temp_ref" name="tempref" value="1" <?php echo set_checkbox('tempref', '1'); ?> />
Any help with either of these would be really appreciated.

Looks like you had the set_select within the quotes for the option value. I moved it after it. I have also made an edit to use printf for better readability.
<label for="rating">Rating: </label>
<select name="rating">
<?php
if(isset($rating) && $rating != 'none') {
echo '<option value="" '.set_select('rating', '', TRUE).'></option>';
foreach($rating as $row) {
printf('<option value="%s" %s>%s</option>', $row->door_rating_rating, set_select('rating', $row->door_rating_rating), $row->door_rating_rating);
}
} else {
echo '<option value="none">Nothing to list</option>';
}
?>
</select>

To solve set_select() try the following. Assuming you have an array named $isps which contains id and name field.
<select id="isp" name="isp">
<option value="" selected>Select a ISP</option>
<?php foreach ($isps as $row) { ?>
<option value="<?php echo $row->id ; ?>" <?php echo set_select('isp', $row->id, False); ?> ><?php echo $row->name ; ?> </option>
<?php } ?>
</select>

Related

HTML PHP: Why do two identical select elements produce different HTML displays?

Could someone kindly help out with this rather basic issue: In a simple HTML form (functionally all good) two select elements are included.
Code:
<select id="dept" name="dept" required>
<?php
foreach($stddept as $item0) {
echo "<option value='$item0'";
if ($_POST['dept'] == $item0) echo 'selected="selected"';
echo ">$item0</option>";
}
?>
</select>
<br><br>
<select id="lev" name="lev" required>
<?php
foreach($stdlev as $item1) {
echo "<option value='$item1'";
if ($_POST['lev'] == $item1) echo selected="selected"';
echo ">$item1</option>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>`
These two drop-down boxes behave differently in that only the second one honours the 'required' attribute. The first one can be submitted blank, which is obviously not right.
They also appear differently (see image) in that the first displays the first option in the list (from MySQL DB) but the second displays a blank. I have checked everything I could to see what else could cause the difference - without any joy.
Does anyone know what I am missing here? Thank you.
You can do this using jquery/javascript by getting the id of the first selected value and then select the result for the second box based on that id.
I think the problem with the code ( as it is shown ) is that you are incorrectly echoing the option in the second dropdown - it is missing quotes. You can streamline the code somewhat like this:
<select id="dept" name="dept" required='required'>
<?php
foreach($stddept as $item0){
$selected=$_POST['dept'] == $item0 ? " selected='selected'" : "";
echo "<option value='$item0'{$selected}>";
}
?>
</select>
<br /><br />
<select id="lev" name="lev" required='required'>
<?php
foreach($stdlev as $item1){
$selected=$_POST['lev'] == $item1 ? " selected='selected'" : "";
echo "<option value='$item1'{$selected}>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>

Get the value from database in selecte tag HTML PHP MySQL [duplicate]

This question already has an answer here:
Select option default based on database variable [duplicate]
(1 answer)
Closed 7 years ago.
I'm beginner in PHP MySQL. I successfully get the value from database in INPUT type but I can't get the data from database in SELECT type:
Here is my sample edit form where Gender & User Type can't output the value from my database:
and here is data from the table I created:
I'm calling the data in my input box by using this code:
<?php $userRow['agentFname']; ?> //$userRow['ROW NAME IN TABLE'];
But I can't call the data where In select box, like Gender: and User Type here is my code of Select box.
<select class="form-control" name="aGender" >
<option selected disabled>*Gender</option>
<option>Male</option>
<option>Female</option>
</select>
<select class="form-control" name="utype" >
<option selected disabled>*User Type</option>
<option>Agent</option>
<option>Team Leader</option>
<option>Admin</option>
</select>
#Edmhar has a good answer, but it is hard coded and isn't good if you want to add more genders (transgenders and what not).
What I do is something like this:
<?php
$array = array("male", "female", "other");
echo "<select class='form-control' name='aGender' >";
foreach ($array as $gender) {
if ($gender == $databaseValue) {
echo "<option selected>$gender</option>";
} else {
echo "<option>$gender</option>";
}
}
echo "</select>";
?>
Also, don't use disabled on form elements; use read-only. It does the same thing as disabled visually, but disabled does what it says. It blocks the value from being submitted to the database. read-only just prevents editing, but doesn't cause form submission problems. User type will follow the same suit.
I already have my own answer to this
Here how I solved this:
<select class="form-control" name="aGender" >
<?php
if ($userRow['agentGender'] == 'Male') {
echo "<option selected>Male</option>";
echo "<option>Female</option>";
} else {
echo "<option selected>Female</option>";
echo "<option>Male</option>";
}
?>
</select>
I think you can use below conditional code for this issue:
<?php if($userRow['aGender'] == 'Male'){
$selectedMale = 'selected="selected"'
$selectedFemale = ''
}else{
$selectedFemale = 'selected="selected"'
$selectedMale = ''
}?>
<select class="form-control" name="aGender" >
<option selected disabled>*Gender</option>
<option <?php echo $selectedMale ;?>>Male</option>
<option <?php echo $selectedFemale;?>>Female</option>
</select>
and same for the another select box.
It will work hopefully.
In my opinion it's best to use ternary operator in such a case, combined with options element fetched from db/array. Something like:
<?php $options = Array(1 => "Option 1", 2 => "Option 2")
<select>
<?php foreach($options as $key => $opt): ?>
<option value="<?= $key ?>" ($key == $userRow['value'] ? "selected" : "" )><?= $opt ?></option>
<?php endforeach; ?>
</select>

Pass a PHP Variable into HTML Select "Value"

I am trying to pass some php via a dropdown in html , the php then needs to be used to aid the execution of more php. However I do not think it is working. Any suggestions would really be appreciated, its been bugging me a while. This is not the first form to use post on this page, the dropdown is populated after a search has been performed using another previous form.
HTML:
<form method="post" name="results">
<select class="form-control textinput">
<option value="<?php $response->body->results[0]?>"><?php echo $response->body->results[0]->name; ?></option>
<option value="<?php $response->body->results[1]?>"><?php echo $response->body->results[1]->name; ?></option>
<option value="<?php $response->body->results[2]?>"><?php echo $response->body->results[2]->name; ?></option>
<option value="<?php $response->body->results[3]?>"><?php echo $response->body->results[3]->name; ?></option>
<option value="<?php $response->body->results[4]?>"><?php echo $response->body->results[4]->name; ?></option>
</select>
<button type="submit" class="btn btn-primary" style="margin-left: auto; margin-right: auto; text-align: center;">Go</button>
</form>
PHP:
<?php
//Results from dropdown are put into selection
$selection = $_POST["results"];
//Selection is put into result var, should look like $response->body->results[x]
$result = $selection;
// IF Statement to only print result if the api call is successfull
if ($response->code == 200) {
if ($result->name == null) {
$printthis = "{$gametitle} returned no results, try and enter the full and accurate name";
}
else {
$printthis = "{$result->name} has a score of {$result->score} on {$result->platform}";
}
}
?>
First, you missed the 'echo' statement, and you also can't put an entire object into a value. If
$response->body->results[0]
has an id, thats what you should be using
something like this
<?php echo $response->body->results[0]->id ?>
I would do this with a loop.
for($i = 0; $i < count($response->body); $i++) {
echo '<option value="'.$i.'"><'.$response->body->results[$i]->name.'</option>';
}

Why if and else both are true in option taq of a html select taq

Its realy strange..what is wrong with this one..??
I want to selected one option in my select drop down by default.so if a value is poseted my if condition got true then why the else is also executing.Your Help Will be really appreciated.
<select name="location" id="location" class="selector_holder_select">
<?php foreach($location as $loc){ ?>
<option value="<?php echo $loc['deal_location_id']; ?>"
<?php if($loc['deal_location_id']==$_POST['location']){ echo 'selected="selected"'; }
else {
if(strcasecmp($loc['location'], $city['cityName']) == 0) { echo
'selected="selected"'; }
} ?>><?php echo $loc['location']; ?></option>
<?php } //End of Foreach ?>
</select>
try this:
<select name="location" id="location" class="selector_holder_select">
<?php foreach($location as $loc){
echo('<option value="'.$loc['deal_location_id'].'"')
if($loc['deal_location_id'] == $_POST['location'] || strcasecmp($loc['location'], $city['cityName']) == 0)
echo(' selected="selected"');
echo(">".$loc['location']."</option>");
?>
</select>
However I recommend you to follow a tutorial about the basics of PHP, for example this from W3C. It looks like you've got no PHP-experience whatsoever.
One problem with this code is that you mix HTML and PHP quite heavily. This makes is very hard to read. I recommend using HEREDOC or something similar to create all the HTML parts instead of switching PHP on and off that often.
I guess what happens is that once in the foreach loop the first if-condition is met and once (in a different iteration step) the elseif is met. That way two elements get selected.
The problem is, that you cannot solve this problem in a single foreach. You first have to check whether the if-case will be true at some point. If so you can set the respective field selected. If not you can search for the else-if and handle that.
Try this:
<select name="location" id="location" class="selector_holder_select">
<?php
foreach($location as $loc) {
echo "<option value=$loc['deal_location_id']>";
if($loc['deal_location_id']==$_POST['location']) {
echo 'selected="selected"';
} else {
if(strcasecmp($loc['location'], $city['cityName']) == 0) {
echo 'selected="selected"';
}
}
echo "$loc['location']</option>";
}
</select>
Above not solve my issue.However this one worked.But can any body make it
shorter and cleanup so it looks more professional.
<select name="location" id="location" class="selector_holder_select">
<?php foreach($location as $loc){ ?>
<option value="<?php echo $loc['deal_location_id']; ?>"
<?php
if($loc['deal_location_id']==$_POST['location'])
{
echo 'selected="selected"';
}
else {
if(strcasecmp($loc['location'], $city['cityName']) == 0 and !isset($_POST['location']))
{ echo 'selected="selected"'; }
} ?>>
<?php echo $loc['location']; ?></option>
<?php } ?>
</select>

Using PHP $_POST to remember a option in a select box?

I have a form which POSTs to itselft so the user can do things that you would find in a Shopping Cart.
e.g. Increase quantity, select postage type.
My problem is for my form Select element called "postage" whenever the form reloads itself , it forgets what was selected.
All my other fields remember their values using this:
<input type="text" name="postcode" value="<?php echo $_POST['postcode']; ?> " />
How do I use the $_POST value to automatically select the option in the select field that was done by the user?
I tried this:
<select name="postage" selected="<?php echo $_POST['postage']; ?>" >
and this
<select name="postage" value="<?php echo $_POST['postage']; ?>" >
Thanks
You almost got it. You need to set the attribute selected="selected" (the exact form you need technically depends on your HTML doctype, but this is a safe default) on the <option> element if and only if the value of $postage equals the value of the element. So:
<select name="postage">
<option value="foo"
<?php if ($_POST['postage'] == "foo") echo 'selected="selected" '; ?>
>
</select>
Note that this violates the DRY principle because you now have two occurrences of the string "foo" in there, so it's a prime candidate for refactoring. A good approach would be to keep value/text pairs in an array and iterate over it with foreach to produce the <option> tags.
You need to foreach through all the options.
Make an array with all the dropdown options, loop through that, and compare with what is stored in post.
E.G.:
<?php
$aDropd = array("apple","orange","banana");
echo "<select>";
foreach($aDropd as $sOption){
$sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
echo "<option $sSel>$sOption</option>";
}
echo "</select>";
no its not working at all..you need to put some kind of loop for that.
For Example : foreach($record => $values){
if($values == $_POST['postage']){
$selected = "selected='selected' ";
}else{
$selected = "";
}
}
<input name="postage" value="1" <?=$selected?> >
EDITED:
if($_POST['postage'] == 1){
$selected1 = "selected='selected' ";
}else if($_POST['postage'] == 2){
$selected2 = "selected='selected' ";
} and so on..........
<select name="postage">
<option value="1" <?=$selected1;?> />
<option value="2" <?=$selected2;?> />
</select>
I think this may be helpful to you..you can ask me if anything else needed...
Thanks.
The value of selected should be selected
<select name="postage" value="1" <?php echo (($_POST['postage'] == 1)?'selected="selected"':''); ?> >
Your html syntax is wrong. The correct way to write the html is like this:
<select>
<option value ="<?php echo $_POST['postage']; ?>" selected="selected"></option>
</select>
You can also make it shorter:
<select>
<option value ="<?=$_POST['postage']; ?>" selected="selected"></option>
</select>
<select name="foo">
<option value="1" <?php echo strcmp($_POST['foo'],"1")==0?"selected=\"selected\"":"" ?>>option1</option>
<option value="2" <?php echo strcmp($_POST['foo'],"2")==0?"selected=\"selected\"":"" ?>>option2</option>

Categories