prevent double datas in select box - php

Here is my HTML + PHP :
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<option selected="selected"><?php echo $thirds['type']; ?></option>
<option value="CLIENT">CLIENT</option>
<option value="AFFRETE">AFFRETE</option>
<option value="DEPOT">DEPOT</option>
</select>
I am using JQUERY as Framework for javascript.
My problem is probably simple. But how can I prevent selectbox to have the same data multiple times? For example, if
$third['type'] = "AFFRETE"
Then, I will have one time AFFRETE as selected value, and again this value in my select box. I tried to remove the selected value with ready function in javascript :
<script type="text/javascript">
$(document).ready(function(){
$("#type option:selected").remove();
})
</script>
But then I have another value selected... logical.
Thank you in advance.

You can do this directly in PHP. Check the array value before adding the element as an option:
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<?php if (!in_array($thirds['type'], array('CLIENT', 'AFFRETE', 'DEPOT'))): ?>
<option selected="selected"><?php echo $thirds['type']; ?></option>
<?php endif; ?>
<option value="CLIENT">CLIENT</option>
<option value="AFFRETE">AFFRETE</option>
<option value="DEPOT">DEPOT</option>
</select>
Or maybe you're looking for:
<?php $options = array('CLIENT', 'AFFRETE', 'DEPOT'); ?>
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option; ?>"
<?php if ($option === $thirds['type']): ?>
selected="selected"
<?php endif;
><?php echo $option; ?></option>
<?php endforeach; ?>
</select>

Try doing this:
<select name="type" class="form-control form-update-user" id="type" tabindex=1>
<option value="CLIENT" <?php echo $thirds['type']=='CLIENT'?'selected=selected':''; ?>>CLIENT</option>
<option value="AFFRETE" <?php echo $thirds['type']=='AFFRETE'?'selected=selected':''; ?>>AFFRETE</option>
<option value="DEPOT" <?php echo $thirds['type']=='DEPOT'?'selected=selected':''; ?>>DEPOT</option>
</select>
Remember that this is a quick and dirty way to do this. Prefer making a loop creating the options, instead of hardcoding it.

Related

How to create a PHP dropdown list that allows you to select multiple options?

This is the basis for the code I'm using, drawing options from a database but no matter how I alter this code I will only let me select 1 option or return an error.
<select name="sargentid" id="fieldsargentid" class="form-control">
<?php foreach ($sargent as $sargent) { echo "<option value='" . $sargent->getID() .
"'>$sargent</option>"; }?>
</select>
<select name="sargentid" id="fieldsargentid" class="form-control" multiple=multiple>
<?php
foreach($sargent as $sargentKey => $sargentList){
$values = $sargentList['NameofTheRowInTable'];
?>
<options value = <?php echo $values; ?> ><?php echo $values;?></options>
<?php
}
?>
</select>
A drop-down list that allows multiple selections using the multiple attribute:
<select id="animals" name="animal" multiple>
<option value="cat">Cat</option>
<option value="dog">Dog</option>
<option value="mouse">Mouse</option>
<option value="lion">Lion</option>
</select>
Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.
Demo: https://jsfiddle.net/prk6c9q7/
<select name="sargentid" id="fieldsargentid" class="form-control" multiple>
<? foreach($argent as $data){?>
<option value="<?=$data->getID() ?>"><?= $data ?></option>
<?php } ?>
</select>
Try This

Get value of drop down menu and save as variable in PHP

I want to get the selected value of a drop down menu and save it as a variable. I tried the following as documented in another post, but it seems not working: echo $selected = $_POST['<?php $as->my_name(); ?>'];
Drop down:
<select name="<?php $as->my_name(); ?>">
<option value="">Select this</option>
<option value="91"<?php $mb->state('91'); ?>>91</option>
<option value="90"<?php $mb->state('90'); ?>>90</option>
<option value="89"<?php $mb->state('89'); ?>>89</option>
<option value="88"<?php $mb->state('88'); ?>>88</option>
<option value="87"<?php $mb->state('87'); ?>>87</option>
<option value="86"<?php $mb->state('86'); ?>>86</option>
<option value="85"<?php $mb->state('85'); ?>>85</option>
<option value="84"<?php $mb->state('84'); ?>>84</option>
<option value="83"<?php $mb->state('83'); ?>>83</option>
<option value="82"<?php $mb->state('82'); ?>>82</option>
</select>
Post the form:
You have to put the select option within form tags
<form method="post">
<select name="example">
<option></option>
</select>
<input type="submit" />
</form>
Get the posted value:
To get the value of this example you can use:
<?php
$selected = $_POST['example'];
echo $selected;
?>
To get the value in your case:
<select name="<?php echo $as->my_name(); ?>">
<option value="test">test</option>
</select>
<?php
$selected = $_POST[$as->my_name()];
echo $selected;
?>

Set a PHP variable as the value of user selected dropdown form

I'm trying to create a form with text fields and dropdowns. With the text fields, I'm using this code:
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo $user->name;?>" style="width:95%;"/>
The form then checks if the user has filled out the "name" field with this:
$name = $input->get('name', null);
if($name == null)
return false;
This works fine for the multiple text fields that the form uses, but I don't know how to check for the user input on the dropdowns. How can I send the option selected by the user, similar to the way I did it with the text field, so that I can check to make sure the user selected something? Example:
<label for="department">Department</label>
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4"><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
$department1 = $input->get('department1', null);
$department2 = $input->get('department2', null);
Etc........
This is set up the exact same way as the text fields as far as I know, but doesn't work and seems like a bad way to do it anyways.
if you want to verify and pre-select one option you should do something like this:
<option value="6" <?php if($params->get('department6')==true){ echo 'selected'; } ?>><?php echo $params->get('department6');?></option>
First of all, instead of having 8 lines like that for your departments, you could use a for loop.
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
Now, inside your for loop, you can add a check if the $input->get is true.
<?php if($input->get('department' . $i, null)) echo 'selected'; ?>
So if you mix both together, the result would be
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<?php for($i=1; $i<=8; $i++): ?>
<option value="<?php echo $i; ?>" <?php if($input->get('department' . $i, null)) echo 'selected'; ?>><?php echo $params->get('department' . $i);?></option>
<?php endfor; ?>
</select>
And if you want to a "pre selected" option use "selected" in the option you want:
<select name="department" form="quickcontact_frm">
<option value="default">Select </option>
<option value="1"><?php echo $params->get('department1');?></option>
<option value="2"><?php echo $params->get('department2');?></option>
<option value="3"><?php echo $params->get('department3');?></option>
<option value="4" selected><?php echo $params->get('department4');?></option>
<option value="5"><?php echo $params->get('department5');?></option>
<option value="6"><?php echo $params->get('department6');?></option>
<option value="7"><?php echo $params->get('department7');?></option>
<option value="8"><?php echo $params->get('department8');?></option>
</select>
As explained by W3CSchools.

PHP Mysqli HTML How to have current value from database selected in <select>

my goal is get current value from database, selected in the dropdown list.
I tried that code but it shows always "Ndone" in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option selected="selected" value=""></option>
<option selected="selected" value="DONE">DONE</option>
<option selected="selected" value="NDONE">NDONE</option>
</select>
Also this one and nothing just the first row that selected in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option value=""></option>
<option value="DONE">DONE</option>
<option value="NDONE">NDONE</option>
</select>
I don't know what to do, any help please
When i use a textfield it works, so there's no problem with the variable $work
<input type="text" name="work" value="<?php echo $work; ?>"/>
<select> does not have a "value" Attribute. You must add the selected attribute to one option.
<?php
$options = array(
'',
'DONE',
'NDONE',
);
?>
<select name="work" id="work">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>"<?php echo $option === $work ? ' selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>
As per your example you could do this:
You just need to check each option value against the $work variable and if it matches add the selected attribute.
<select name="work" id="work">
<option value=""></option>
<option value="DONE" <?php echo($work == 'DONE'?'selected="selected"':''); ?>>DONE</option>
<option value="NDONE" <?php echo($work == 'NDONE'?'selected="selected"':''); ?>>NDONE</option>
</select>

value="<?php echo htmlspecialchars($_POST['whatever']); ?>" not working in select tag

my code is written below
<select name="year" required value="<?php echo htmlspecialchars($_POST['year']); ?>" >
<option>----SELECT----</option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3<sup>rd</sup></option>
<option value="4">4<sup>th</sup></option>
</select>
htmlspecialchars() is not working for select tag.is there any mistake or there is some other way to do it
if you want to set the default value of a select you have to set the option that contains this value as selected like this:
<select name="year" required="required">
<option>----SELECT----</option>
<option <?php if((int)($_POST['year'])==1){?>selected<?php } ?> value="1">1<sup>st</sup></option>
<option <?php if((int)($_POST['year'])==2){?>selected<?php } ?> value="2">2<sup>nd</sup></option>
<option <?php if((int)($_POST['year'])==3){?>selected<?php } ?> value="3">3</sup>rd<sup></b></option>
<option <?php if((int)($_POST['year'])==4){?>selected<?php } ?> value="4">4<sup>rth</sup></option>
</select>
You don't assign a value attribute to a select tag; you assign it to option tags. It's not clear what you are trying to do, but <select ... value="..."> is just invalid HTML. See the specification.
you cannnot give value in <select> tag. Value is given in <option> tag.
like
<select name="year">
<option>----SELECT----</option>
<option value="<?php echo htmlspecialchars($_POST['year']); ?>">
<?php echo htmlspecialchars($_POST['year']); ?></option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3</sup>rd<sup></b></option>
<option value="4">4<sup>rth</sup></option>
</select>

Categories