I've been rebuilding my site using CodeIgniter. I am currently making the sign up page and am looking for the most efficient/ strategic way to make a dynamic drop down. This is a social network for the Cystic Fibrosis community. That being said, when registering there is a drop down that ask your relation to CF.
There are two options: 1. I have CF and 2. Someone I know has CF. I am wanting to make it to where when you select 1. a NEW drop down appears with options, and the same for 2.
Here is controller code:
function relation_dropdown($relation="relation", $top_relations=array()) {
$relations = array(
"choose"=>"Choose One",
"I have CF"=>"I have CF",
"Someone I know has CF"
);
$html = "<select name='{$relation}'>";
if(!empty($top_relations)){
foreach($top_relations as $value){
if(array_key_exists($value, $relations)){
$html .="<option value='{$value}'>{$relations[$value]}</option>";
}
}
$html .="<option>----------</option>";
}
foreach($relations as $key => $relation){
$html .="<option value='{$key}'>{$relation}</option>";
}
$html .="</select>";
return $html;
}
and the form on the view:
<div id="signup_form">
<?php
echo validation_errors();
echo form_open('general/send?');
echo "<div class='form_text'>First Name</div>";
echo form_input('first_name');
echo "<div class='form_text'>Last Name</div>";
echo form_input('last_name');
echo "<div class='form_text'>Email</div>";
echo form_input('email');
echo "<div class='form_text'>Confirm Email</div>";
echo form_input('confirm_email');
echo "<div class='form_text'>Password</div>";
echo form_input('password');
echo "<div class='form_text'>Confirm Password</div>";
echo form_input('confirm_password');
echo "<div class='dropdown_structure'>";
echo "<div class='form_text'>";
echo "Location";
echo "</div>";
echo country_dropdown('country');
echo "</div>";
echo "<div class='dropdown_structure'>";
echo "<div class='form_text'>";
echo "Relation To CF";
echo "</div>";
echo relation_dropdown('relation');
echo "</div>";
echo form_close();
?>
</div>
So my question is what is the best way to do this?
thanks in advance
If you have multiple decisions the user must make, I would have a lookup table in a database to start, with a parent-child relationship among the select options. That way, you could use ajax with jquery, and select the children of the currently selected option.
So, basically, I would do this:
User selects "I Have CF"
Ajax goes and gets all the children of the "I Have CF" option from the database table
jQuery gets those options back, and displays the next drop down.
Rinse and repeat until all decisions are made
You can:
Use ajax to get the options when the first dropdown is changed
This requires a second controller that return the data for the second dropdown (in either HTML, JSON, or whatever you want) and JQuery/JS to tie it all together. This is good if there are lots of options and you don't want to have to load them all up front.
Output all of the options with the initial page load
This allows you to just use JQuery/JS to update the second dropdown. The data in the intial page load can either be a Javascript array or hidden HTML elements. This is easy, but increases the initial download, especially if you have lots of options.
Here is an example of having the options in the initial page load as hidden HTML elements:
<select id="dropdown1" name="dropdown1">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>
<select id="dropdown2" name="dropdown2" disabled="disabled">
<option value="">select an option</option>
</select>
<select id="dropdown2a" name="dropdown2a" style="display: none">
<option value="a">option a</option>
<option value="b">option b</option>
<option value="c">option c</option>
</select>
<select id="dropdown2b" name="dropdown2b" style="display: none">
<option value="i">option i</option>
<option value="ii">option ii</option>
</select>
<select id="dropdown2c" name="dropdown2c" style="display: none">
<option value="x">option x</option>
<option value="y">option y</option>
</select>
<script type="text/javascript">
$(function()
{
$('#dropdown1').change(function(){
var val = ('#dropdown1').val();
if (val == 1)
{
$('#dropdown2').html($('#dropdown2a').html());
}
else if (val == 2)
{
$('#dropdown2').html($('#dropdown2b').html());
}
else if (val == 3)
{
$('#dropdown2').html($('#dropdown2c').html());
}
$('#dropdown2').removeAttr("disabled");
});
});
</script>
Related
<select name="title">
<selected value="<?php echo $title; ?>"><?php echo $title; ?></selected>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Mr">Mr.</option>
<option value="Dr">Dr.</option>
</select>
I am trying to read a value from a column Title in a MySQL database, which is suppose to read the value, whether it be Mr., Ms., Mrs., then compare it with the values in the drop-down list. It then lets the user select another title to then update the one stored in MySQL database.
I am creating a user profile. So when the user logs in and navigates to the view to edit a profile, he/she should be presented with a drop-down list containing the title he/she selected when registered. Then if he/she wants to they can change the title in the drop-down list and press the update button and it should now update to the new title in the database.
Change your <select> dropdown list in the following way,
<select name="title">
<option value="Mrs"<?php if($title == "Mrs"){ echo " selected='selected'"; } ?>>Mrs.</option>
<option value="Ms"<?php if($title == "Ms"){ echo " selected='selected'"; } ?>>Ms.</option>
<option value="Mr"<?php if($title == "Mr"){ echo " selected='selected'"; } ?>>Mr.</option>
<option value="Dr"<?php if($title == "Dr"){ echo " selected='selected'"; } ?>>Dr.</option>
</select>
There is no selected HTML tag. Use the selected attribute of the option tag:
selected
If present, this Boolean attribute indicates that the option is initially selected. If the <option> element is the descendant of a <select> element whose multiple attribute is not set, only one single <option> of this <select> element may have the selected attribute.1
So consider this example from the Examples section of the Mozilla Developer Network page for <select>:
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
Your example code can be updated similarly:
<select name="title">
<option value="Mrs" <?php if($title=="Mrs"){ echo "selected"; } ?>>Mrs.</option>
<option value="Ms" <?php if($title=="Ms"){ echo "selected"; } ?>>Ms.</option>
<option value="Mr" <?php if($title=="Mr"){ echo "selected"; } ?>>Mr.</option>
<option value="Dr" <?php if($title =="Dr"){ echo "selected"; } ?>>Dr.</option>
</select>
A simpler way to do this would be to process the names first, using array_reduce():
<?php
$title = 'Dr';
$names = array('Mrs','Ms','Mr','Dr');
$options = array_reduce($names,function($carry,$name) use ($title) {
return $carry .= '<option value="'.$name.'"'.($title == $name?' selected':'').'>'.$name.'.</option>';
});
?>
<select name="title">
<?php echo $options;?>
</select>
See it in action in this playground example.
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
I have a problem which I'm not sure how to approach, I have a WP plugin which has a form, in the form I have a with result that could have varied amount of data (depending on what the user submits).
How can I add 'selected' to the selected item so when the user returns he can edit/view the item he selected?
<select name="supplier">
<option value="Supplier 1">Supplier 1</option>
<option value="Supplier 2">Supplier 2</option>
<option value="Supplier 3">Supplier 3</option>
<option value="Supplier 4">Supplier 4</option>
</select>
A loop comes to mind, shall I assign a number to each option? There may be 100 suppliers so it would need to count right?
I would use a loop like...
$theirChosenSupplier = $supplierVarFromDatabaseOrWherever;
?>
<select name="supplier">
foreach($allSuppliers as $individualSupplier) {
if($individualSupplier == $theirChosenSupplier) {
$selected = "selected";
} else {
$selected = "";
}
?><option value="<?php echo $individualSupplier; ?>" <?php echo $selected; ?>>
<?php echo $individualSupplier; ?>
</option>
<?php } ?>
</select>
This code isn't tested but should give you an idea. Apologies if I've misunderstood the question.
adding an id is always a good thing. Assuming this is wordpress generated and you don't have a loop already there is always the option of using javascript and maybe an onchange event.
I dont know if the form is submitted first or you do some other stuff but maybe:
<select id="foo">
<option id="provider_1">Provider 1</option>
<option id="provider_2">Provider 2</option>
</select>
<script type="text/javascript">
window.onload = function() {
var selectedValue = '<?php echo someEscapeFunction($_SESSION['id_provider']); // or _GET, _POST ?>';
document.getElementById('foo').selectedIndex = document.getElementById(selectedValue).index;
}
</script>
I have limited JavaScript knowledge and am trying to change a form so it autochanges when the drop down is selected without having to click a go button.
How would I amend the following code? There are 3 separate drop downs in there. I've tried inserting onselect in there but it didnt seem to work?
<select name="season">
<option value="0"><?= $txt_all ?></option>
<?php
while($data = mysql_fetch_array($get_seasons))
{
if($data['SeasonID'] == $defaultseasonid)
echo "<option value=\"$data[SeasonID]\" SELECTED>$data[SeasonName]</option>\n";
else
echo "<option value=\"$data[SeasonID]\">$data[SeasonName]</option>\n";
}
mysql_free_result($get_seasons);
?>
</select>
<select name="matchtype">
<option value="0"><?= $txt_all ?></option>
<?php
while($data = mysql_fetch_array($get_types))
{
if($data['MatchTypeID'] == $defaultmatchtypeid)
echo "<option value=\"$data[MatchTypeID]\" SELECTED>$data[MatchTypeName]</option>\n";
else
echo "<option value=\"$data[MatchTypeID]\">$data[MatchTypeName]</option>\n";
}
mysql_free_result($get_types);
?>
</select> <input type="submit" name="submit" value="Go"><td bgcolor="<?php echo $inside_c ?>" align="center">
Other Stats <br><select name="changeto">
<option value="1"><?= $txt_match_statistics ?></option>
<option value="2"><?= $txt_recordbook ?></option>
<option value="5"><?= $txt_opponent_list ?></option>
<option value="6"><?= $txt_this_day ?></option>
</select> <input type="submit" name="changepage" value="Go">
With jQuery you can listen your drop down list when it changes.
$('select').change(function(){
// Do stuff
});
If you want to submit your form immediately after changing drop down value you can do this:
$('select').change(function(){
$('form').submit();
});
In right context you can see example from here(and mess around with it!): http://jsfiddle.net/EB635/
I have a page with two drop-down menus. The option selected in the first drop-down menu controls what is displayed in the second.
The code was nice and easy when the drop-down menus were only used once - but I'm looking to repeat each set of drop-down menus four times.
Drop-down menu one is assigned the ID experience[<?php echo $i; ?>][manufacturer]. Drop-down menu two is assigned the ID experience[<?php echo $i; ?>][type].
Essentially, what I've got is:
<select id="experience[1][manufacturer]">...</select>
<select id="experience[2][manufacturer]">...</select>
<select id="experience[3][manufacturer]">...</select>
<select id="experience[4][manufacturer]">...</select>
... followed by:
<select id="experience[1][type]">...</select>
<select id="experience[2][type]">...</select>
<select id="experience[3][type]">...</select>
<select id="experience[4][type]">...</select>
I'm wondering: what's the best way to get the equivalent of <?php echo $i; ?> into the chunk of JavaScript that's used to output the contents of the second drop-down menu? (I know I can't use PHP directly within JavaScript like this - I just left the <?php echo $i; ?> snippets to indicate where I need to output numbers 1, 2, 3, 4 etc.
Thanks for your help!
Code:
<form>
<?php
$i=1;
while($i<=4) {
?>
<select id="experience[<?php echo $i; ?>][manufacturer]">
<option value="Ford">Ford</option>
<option value="Honda">Honda</option>
<option value="Mercedes">Mercedes</option>
</select>
<select id="experience[<?php echo $i; ?>][type]">
<script type='text/javascript'>
$(document).ready(function() {
$("#experience[<?php echo $i; ?>][manufacturer]").change(function() {
$("#experience[<?php echo $i; ?>][type]").load("get_type.php?choice=" + $("#experience[<?php echo $i; ?>][manufacturer]").val());
});
});
</script>
</select>
<?php
$i++;
}
?>
</form>
Revised code:
<form>
<script type='text/javascript'>
$(document).ready(function() {
$(".experienceManufacturer").change(function() {
$("#experience["+$(this).attr('data-id')+"][type]").load("get_type.php?choice=" + $(this).val());
});
});
</script>
<?php $i=1;
while($i<=4) { ?>
<select id="experience[<?php echo $i; ?>][manufacturer]" class="experienceManufacturer" data-id="<?php echo $i; ?>">
<option value="Ford">Ford</option>
<option value="Honda">Honda</option>
<option value="Mercedes">Mercedes</option>
</select>
<select id="experience[<?php echo $i; ?>][type]">
</select>
<?php
$i++;
}
?>
</form>
You should not need to include the script element in your PHP loop.
Instead, you should add a similar class to all common select elements. Something like class="experience"
Then, with a single script element you can attach events to all the selects:
<script type='text/javascript'>
$(document).ready(function() {
$("select.experience").each(function(i,element){
// here element is the actual item from the selected set, i is its index
$(element).on("change", function() {
$("#experience[" + i + "][type]").load("get_type.php?choice=" + $(element).val());
});
});
});
</script>
Instead of repeating the same javascript code 4 times, put it outside your loop and give classes to your select elements and use them as selectors for your jquery code. You could then add an attribute (named data-id for example) to your select elements containg their ids. It would look something like this
<select id="experience[<?php echo $i; ?>][manufacturer]" class="experienceManufacturer" data-id="<?php echo $i; ?>">
<option value="Ford">Ford</option>
<option value="Honda">Honda</option>
<option value="Mercedes">Mercedes</option>
</select>
<select id="experience[<?php echo $i; ?>][type]"></select>
And your javascript code (outside the loop) would look something like this
$(document).ready(function() {
$(".experienceManufacturer").change(function() {
$("#experience\\["+$(this).attr('data-id')+"\\]\\[type\\]").load("get_type.php?choice=" + $(this).val());
});
});
Note that I used the keyword "this" inside the change event which references to the select element.
Edit: Added backslashes to escape the brackets in the jQuery selector (or else jQuery interprets it as an attribute).
Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>