Keep values in <select> for loop after submit - php

I am trying to retain the values in my select with a for loop option if form is submitted incomplete. It inserts the values into database and functions perfectly fine but I don't want people to have to fill out all the for loop selects again if they submit the form incomplete.
if(!empty($_POST['height'])){
$height = $_POST['height'];
}
<select class="form-control selectpicker" name="height">
<option value="" disabled selected>Select Your Height</option>
<?php
for ($i=1; $i<=250; $i++)
{?>
<option value="<?php echo $i;?>"><?php echo $i;?>cm</option>
<?php
}
?>
</select>
I have my other select functioning perfectly:
<?php
if(isset($_POST['city'])){
$city = $_POST['city'];}?>
<select class=" form-control selectpicker" name="city" value="">
<optgroup label="Thailand">
<option disabled selected>Select A City</option>
<option
<?php
if (isset($city) && $city=="Bangkok") echo "selected";?>
>Bangkok</option>
<option <?php
if (isset($city) && $city=="Phuket") echo "selected";?>
>Phuket</option>
<option <?php
if (isset($city) && $city=="Pattaya") echo "selected";?>
>Pattaya</option>
</optgroup>
</select>
Essentially I want to achieve the same result as the City select but with the for loop in the Height select.
I have spent the last couple of days researching and trying to implement different codes and strategies to make it work but I still can't get it to function. Any help is greatly appreciated and if there is anymore code or explanation required I will be happy to provide it.

You have added value in $height. But in select, you haven't use it to retain selected value. change your drop-down for height as below-
if(!empty($_POST['height'])){
$height = $_POST['height'];
}
<select class="form-control selectpicker" name="height">
<option value="" disabled selected>Select Your Height</option>
<?php
for ($i=1; $i<=250; $i++)
{?>
<option value="<?php echo $i;?>" <?php if (isset($height) && $height==$i) echo "selected";?>><?php echo $i;?>cm</option>
<?php
}
?>
</select>

Related

About Using Multi Select?

Hi I'm working on the code now and the ticket system sending a notification to a user that selects him about.
I want a notification to select more than one user and more than one user.
I tried to use this for Multi Select more than one user selection, again when I say save too, a single user is registering on this can you help me ?
<div class="form-group select-placeholder">
<label for="assigned" class="control-label">
<?php echo _l('ticket_settings_assign_to'); ?>
</label>
<select name="assigned" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
<option value=""><?php echo _l('ticket_settings_none_assigned'); ?></option>
<?php foreach($staff as $member){ ?>
<option value="<?php echo $member['staffid']; ?>" <?php if($member['staffid'] == get_staff_user_id()){echo '';} ?>>
<?php echo $member['firstname'] . ' ' . $member['lastname'] ; ?>
</option>
<?php } ?>
</select>
</div>
If you want to be able to select multiple you have to add the multiple attribute to the box.
<select multiple>
To receive the values in your code as an array you should change the name from name="assigned" to name="assigned[]"
This will send the multi selected data back as an array to PHP or whichever language you are using
<select multiple name="assigned[]" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
You have to put the multiple attribute in your <select> tag.
Note: The javascript there is used to avoid holding the ctrl-key while selecting multiple options.
window.onmousedown = function (e) {
var el = e.target;
if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
e.preventDefault();
// toggle selection
if (el.hasAttribute('selected')) el.removeAttribute('selected');
else el.setAttribute('selected', '');
// hack to correct buggy behavior
var select = el.parentNode.cloneNode(true);
el.parentNode.parentNode.replaceChild(select, el.parentNode);
}
}
<select name="option-list" size="5" multiple>
<option value="option-1">Option</option>
<option value="option-2">Option</option>
<option value="option-3">Option</option>
<option value="option-4">Option</option>
<option value="option-5">Option</option>
<option value="option-6">Option</option>
<option value="option-7">Option</option>
<option value="option-8">Option</option>
<option value="option-9">Option</option>
</select>
I organized the code this way. But I couldn't get a successful result.
<div class="form-group select-placeholder">
<label for="assigned" class="control-label">
<?php echo _l('ticket_settings_assign_to'); ?>
</label>
<select multiple name="assigned[]" id="assigned" class="form-control selectpicker" data-live-search="true" data-none-selected-text="<?php echo _l('dropdown_non_selected_tex'); ?>" data-width="100%">
<option value=""><?php echo _l('ticket_settings_none_assigned'); ?></option>
<?php foreach($staff as $member){ ?>
<option value="<?php echo $member['staffid']; ?>" <?php if($member['staffid'] == get_staff_user_id()){echo '';} ?>>
<?php echo $member['firstname'] . ' ' . $member['lastname'] ; ?>
</option>
<?php } ?>
</select>
</div>

select multiple options at a time

<select class="form-control default-select2 " id="group" data-size="10" data-live-search="true" data-style="btn-white" multiple="multiple">
<option value="" disabled="disabled" selected="selected">Select Group</option>
<!-- <option value="1">Admin</option>
<option value="4">User</option> -->
<?php $groups = $objUser->GetGroups(); ?>
<?php foreach($groups as $group){ ?>
<option value="<?php echo $group['id'] ?>"<?php echo ($data[0]['group_id'] == $group['id'] ? "selected='selected'" : "") ?>><?php echo $group['primary_name'] ?></option> <? } ?>
</select>
Now I want user to select multiple option at a time. i searched the whole internet but i couldn't find the answer to fix this issue. i dont know what i am missing.
Your code can actually manage multiple selections. Just hold Ctrl key and click on different options.
Exemple of multiple select : here
When you submit the form, you will be able to get all the selected options into an array.

prevent double datas in select box

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.

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.

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