Country selection for a web form? - php

How to create a HTML dropdown with list of countries , is there any web services which i can use ? without entering manual.

You could use this jQuery plugin:
JQuery Autocomplete Plugin extension for country list

Get an array of country names like this one.
Then use this in your file:
<select name="countries">
<?php
$countries = array(
'AF'=>'AFGHANISTAN',
'AL'=>'ALBANIA',
'DZ'=>'ALGERIA',
'AS'=>'AMERICAN SAMOA',
...rest of data from site link above
);
foreach($countries as $key => $array) {
?><option value="<?=$key?>"><?=$value?></option>
}
</select>
?>
Explanation:
Creates a array of country names, then goes through one at a time and writes each country name into an <option> within your <select></select> tags.

Related

Yii: Change an input text field value depending on dropdown list selection

I inherited a YII based web application but I am not really familiar with Yii and don't find the solution yet. So if you can, please help me!
There is a form. In the form there is a dropdown list which get data from a db table. This table contains the name and the price of leases. The dropdown lists only the name.
<?php
$typeNames = array();
foreach(LeaseType::model()->findAll() as $lt) {
$typeNames[$lt->id] = $lt->name;
}
?>
<?php echo $form->dropDownListControlGroup($model, 'lease_type_id',
$typeNames,
array(
'empty'=>'Válassz!',
'onchange'=>'$("#model-payed").val($("#model-lease_type_id option:selected").text());',
)
); ?>
But I need to change the value of a specific input text field with the price of the selected lease.
<?php echo $form->textfield($model,'payed');?>
I can get the name of the lease but I can't set the value of the input for the price of the lease. This field has to be changable based on the customer payed all the price or just partial.
Other problem is that the name and price of the lease in different table than the value needed.
I hope all this are understandable because I am not fluent in English.
Thanks for any help!
Finally I found the solution, so share it maybe someone can use it.
In the views/_form.php I modified the dropdownlist:
<?php
echo $form->dropDownListControlGroup($model, 'lease_type_id', $typeNames, array(
'empty' => 'Válassz!',
'onchange' => '$("#model_comment").val($("#model_lease_type_id option:selected").$typeNames);',
//'onchange'=>'if($(this).val() == "no"){("#quantity").val("0"); }'
)
);
?>

Yii dropdownlist depending on two dropdownlists

I have seen this wiki where they populate a dropdownlist of cities, depending of the value of another dropdownlists that contains countries, using an ajax call with the update option. I need to implement something similar, but my dropdownlist depends on two dropdownlists:
<div class="row">
<?php echo CHtml::label('Countries', 'country_id'); ?>
<?php echo CHtml::dropdownlist('country_id', '',$countries); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'globaladmin'); ?>
<?php echo $form->dropDownList($model,'globaladmin',User::itemAlias('AdminStatus')); ?>
<?php echo $form->error($model,'globaladmin'); ?>
</div>
The user must select a country, and then only if in the second list "No" is selected, a new dropdown lists must be populated with the cities info (just like in the wiki example).
As I said it is similar to the example, but the new dropdown depends on 2 values (the id of the contry selected on the first list and if "No" is selected in the second one). How could I solve it?
EDIT: Explaining a little more
In the example, the country dropdown which contains the ajax call is like:
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/dynamiccities'), //url to call.
'update'=>'#city_id', //selector to update
)));
I can't define such a ajax call in my country list, because I must wait the value of the second dropbox. Only if "No" is selected in this list, the ajax will be executed (and the city dropdownlist populated and showed). If "Yes" is selected then the city dropdownlist must be hidden.
Just add an ID to your globaladmin drop down and then do like below:
$("#THE ID YOU HAVE SET").on('change',function(){
// You can use $(this).val()
// and send it via an ajax request into your own url
var valueOfMyGlobalAdminDropDown=$(this).val();
if(valueOfMyGlobalAdminDropDown=="yes"){
//DO SOMETHING
}else{
//DO SOMETHING ELSE
}
});
it is also strongly recommended to use jquery live(in jquery version -1.7) or jquery ON(in jquery version +1.7) to keep your page alive in ajax requests.

I want to add table structure in drop down list

I want to add two colums from my database table into a drop-down list.
I have a fruits table in my database, and I want to fetch the name of the fruit and it's quantity from the database and insert it into my select drop-down list. But it should be in an option tag, and it should be in table structure.
<select id="availableFruits" name="availableFruits" multiple size=7 style="width:auto;float:left;" >
<?php $fruitList=$this->fruitList;
foreach ($fruitList as $key => $val)
{
printf('<option value="%s"><table><tr><td>%s</td><td>%s</td></tr></table></option>',$val,$val,$val);
//printf('<option value="%s">%s</option>', $val, $val);
} ?>
</select>
This is my code for the drop-down list which I have written in my HTML. Here I am dynamically adding the fruit's name and quantity in the drop-down list, but it's not printing it in a table structure.
The structure should be like:
<option value="%s"><table><tr><td>%s</td><td>%s</td></tr></table></option>
For that Better to use Jquery plugin, to display the Dropdown as per your Choice
May the below link would help you
http://www.jquery4u.com/plugins/10-jquery-selectboxdrop-down-plugins/
You cannot add html elements in <option>

Adding Attributes to Select Options in Joomla

I'm trying to add extra HTML attributes to some select options in a dropdown list in Joomla 2.5, and want to use the built-in HTML helpers rather than write the HTML by myself. The current output is:
<select>
<option value="Red">Red</option>
</select>
but I would like it to be something like:
<select>
<option value="Red" data-img="red.jpg">Red</option>
</select>
so that I can access the data-img attribute using Javascript when the selected option changes.
I'm creating the select options like so:
$select = JHtml::_('select.option', "Red", "Red");
and then passing them to JHTML to create the generic list HTML:
$html = JHTML::_('select.genericlist', ...);
I've looked through the documentation and tried passing various different parameter to the functions, but it's very confusing in terms of all the options (option.attr, attr etc) that the functions use, and Google has turned up nothing either.
Can anyone tell me what extra parameters I need to pass to the functions to get it properly add the extra attributes to the <option> elements?
Thanks in advance!
I was struggling on this exact scenario today, needing to add some extra data with the select's options. After a thorough analyze of the joomla/libraries/joomla/html/html/select.php file, I succeeded to do this with a few downside...
First, in my case, the data used for the select is coming from the database, and need some preparation for this scenario :
$db =& JFactory::getDBO();
$sql = 'SELECT nom AS value , nom AS text, prix FROM #__reservation_nourritures order by `ordering` asc';
$db->setQuery($sql);
$nourritures = $db->loadObjectList();
foreach ($nourritures as $nourriture){
//the data to use MUST be in an array form for the extra html attributes...
$nourriture->data = array('data'=>$nourriture->prix);
}
Once the data is ready, you can pass it to the JHTML function to build the select :
echo JHTML::_('select.genericlist',$nourriture,'myId',array('class'=>'nourritures','option.attr'=>'data'));
In short, the 'option.attr' must be used to insert attributes into the options. Note : The select.genericlist function MUST have only 3 arguments for this to work. From what I understand from the function, attributes only get merged into the options if you pass exactly 3 arguments to the function, otherwise it just ignores it. So if you want, as exemple, to define a preselected option with the additional parameters, you are out of luck. Here is the part regarding this in the function :
if (is_array($attribs) && func_num_args() == 3)
{
// Assume we have an options array
$options = array_merge($options, $attribs);
}
To my understanding, this is a bug and/or a bad behavior. I'll fill in a bugg in the joomla tracker when I get time.
You can do something like this
$option = JHtml::_('select.option', "Red", "Red", array('option.attr' => 'optionattr', 'attr' => array('data-img' => "red.jpg")));
or like this
$option = JHtml::_('select.option', "Red", "Red");
$option->optionattr = array(
'data-img' => "red.jpg"
);

How to set any attribute of Select lke disabled='disabled' by using ViewHelper in Zend Framework

I am using Zend Frameworks ViewHelpers.
I am trying to pass something to set disabled attribute in SELECT. For example if
$countries = array(1=>'Select Option', 2=>'us', 3=>'uk')
and
formSelect('country','us',null,$this->countries)
I need to diable first option i.e. 'Select Option'
Do you have any idea ?
Thanks in addvance
I don't think you can disable one element? If you disable it then why have it at all?
You can only disable the whole <select> input.
Suggest you write validation to not accept the first element.
Edit after OP's comment about being able to do this
Here is another answer
// Get the countries element (do this after adding your options), then set the
// attribute disable for option '1'
$form->getElement("countries")->setAttrib("disable", array(1));
This is suggested here
There IS a way of doing it through Zend_Form (at least on my current ve 1.11):
$this->addElement
(
"select","selectName",
array("multiOptions"=>array("one","two","three"), "disable"=>array(0,1))
);
That one will disable first two options.
Credit goes to jakenoble.
Just reformatted the code to use the formSelect-viewhelper instead of a form-element.
<?php
$countries = array(1 => 'Select Option', 2 => 'us', 3 =>'uk');
echo $this->formSelect('country', 2, array('disable' => array(1)), $countries)
This will result in:
<select name="country" id="country">
<option value="1" label="Select Option" disabled="disabled">Select Option</option>
<option value="2" label="us" selected="selected">us</option>
<option value="3" label="uk">uk</option>
</select>

Categories