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

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'>

Related

Selecting value from database using where condition like below

<select name="sample" class="form-control" id='select'>
<?php
$sam1="attingal"; $sam2="kollam";
$getstd="SELECT stdid,firstname,lastname FROM student WHERE centre='$sam1' AND centre='$sam2' ORDER BY firstname ASC";
$qs=$conn->query($getstd);
$qs->setFetchMode(PDO::FETCH_NUM);
while($getstd=$qs->fetch()) {
?>
<option value='<?php echo $getstd[0]; ?>'>
<?php echo $getstd[1]." ".$getstd[2]; ?>
</option>
<?php } ?>
</select>
Nothing is displaying when I run the above code
Your problem is here:
WHERE centre='$sam1' AND centre='$sam2'
That condition can never be true. a field can't hold 2 different values at once.
You supposedly wanted OR, so change it to
WHERE centre='$sam1' OR centre='$sam2'

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>

Codeigniter, problems using set_select() and set_checked()

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>

changing from dropdown menu to mutli select open box

I have a drop down menu that I want to change to a multiple select box. The code below is working if you only select 1 option (the way I had it before), but of you select 2 it will only show 1 of the two, how can I make it show both options selected, here is the code:
<?php $makes = array("volvo","Saab","Opel","Audi","BMW") ?>
<form method="post" name="store" action="<?php $_SERVER['PHP_SELF'] ?>" >
<select multiple="multiple" name="cars">
<?php foreach ($makes as $make){echo "<option value=\"$make\">". $make ."</option>"; $vehicles = $_POST['cars'];} ?>
<input name="submit" type="submit">
</select>
</form>
<?php
if($_POST['submit']){
echo $vehicles;
}
?>
</body>
</html>
I hope I read this correctly that you would like to retrieve an array of results from the HTML multi select box.
By the way your code snippit is not correct; the HTML <form> closing tag should be after your closing <select> tag and I'm not sure why you have the following in your PHP for() loop:
$vehicles = $_POST['cars'];
You will want to make the HTML tags' name attribute an array as follows (Note I did not test this code):
<select multiple="multiple" name="cars[]">
<?php
foreach ($makes as $make) {
echo "<option value=\"$make\">". $make ."</option>";
}
?>
</select>
<?php
if($_POST['submit']) {
print_r($_POST['cars']);
}
?>
PHP.net - How do I get all the results from a select multiple HTML tag?
It isn't completely clear from your question but I think you mean the following bit of code only echoes one value:
if($_POST['submit']){
echo $vehicles;
}
To turn your selected cars into an array you need to add [] onto the end of the name:
<select multiple="multiple" name="cars[]">
Then to echo each of the selections you can use a foreach loop:
foreach ($_POST['cars'] as $car)
echo $car.'<br />';
you can try with if($_POST['submit']){
print_r($vehicles);
}
please let me know if any issue then..

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