I have this php file for country/state/town list using json method.
PHP file:
sleep(1);
$stateID = $_GET['stateID'];
$countyID = $_GET['countyID'];
$townID = $_GET['townID'];
$html = $_GET['html'];
$states = array();
$states['MA'] = "Massachusetts";
$states['VT'] = "Vermont";
$states['SC'] = "South Carolina";
$counties = array();
$counties['MA']['BARN'] = 'Barnstable';
$counties['MA']['PLYM'] = 'Plymouth';
$counties['VT']['CHIT'] = 'Chittenden';
$counties['SC']['ANDE'] = 'Anderson';
$towns = array();
$towns['MA']['BARN']['CHA'] = "Chatham";
$towns['MA']['BARN']['DEN'] = "Dennis";
$towns['MA']['BARN']['YAR'] = "Yarmouth";
$towns['MA']['PLYM']['BRI'] = "Bridgewater";
$towns['MA']['PLYM']['MAR'] = "Marshfield";
$towns['MA']['PLYM']['WAR'] = "Wareham";
$towns['VT']['CHIT']['BUR'] = "Burlington";
$towns['VT']['CHIT']['ESS'] = "Essex";
if($stateID && !$countyID && !$townID){
echo json_encode( $counties[$stateID] );
} elseif( $stateID && $countyID && !$townID ) {
echo json_encode( $towns[$stateID][$countyID] );
} elseif( isset($villages[$stateID][$countyID][$townID]) ) {
echo json_encode( $villages[$stateID][$countyID][$townID] );
} else {
echo '{}';
}
I retrieve value from MySql database(example:MA for country).
Now I need to print country/state/town name into select box dropdown like this.
<select id="country" class="validate[required]" name="country">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$country['selector'].'">Show Country Name From php File</option>
</select>
<select id="state" class="validate[required]" name="state">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$state['selector'].'">Show state Name From php File</option>
</select>
<select id="town" class="validate[required]" name="town">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$town['selector'].'">Show town Name From php File</option>
</select>
How can I print this?
Create a php file that makes the call to the database. The results are, normally, stored in a php array. You can turn this array into Json using json_encode()(http://www.php.net/manual/en/function.json-encode.php).
Simply echo this in the php-file.
Now call that script with jQuery. There is a simple jQuery method called getJson(). - Read more about it here: http://api.jquery.com/jquery.getjson/
This will bring the MySQL results to you - in Json form - and you could now use JavaScript to loop the results, and display the form accordingly. A simple example would be (I haven't tested this but I hope you get the idea):
$.each(data, function() {
$('#mySelect')
.append($("<option></option>")
.attr("value",data[row].NameOfColumn)
.text(value));
row ++;
});
Note the name of column - that's the name of the key for the value you are looking for. In your case you might want to loop the keys as well - with an outer loop - since the data seems to be presented in such a matter.
Related
<select name="size_select" class="long form-control">
<option selected="selected" value="">Please select</option>
<option value="6 (xxs)" title="6 (XXS)">6 (XXS)</option>
<option value="8 (xs)" title="8 (XS)">8 (XS)</option>
<option value="10 (s)" title="10 (S)">10 (S)</option>
<option value="12 (m)" title="12 (M)">12 (M)</option>
<option value="14 (l)" title="14 (L)">14 (L)</option>
<option value="16 (xl)" title="16 (XL)">16 (XL)</option>
<option value="18 (xxl)" title="18 (XXL)">18 (XXL)</option>
<option value="20 (xxxl)" title="20 (XXXL)">20 (XXXL)</option>
How to get all value in this select?
$element = $html->find('#sizeDdl',0);
foreach($element as $elemen) {
echo ($elemen->plaintext);
}
I try this output:
Notice: Trying to get property of non-object in /h2/home/website/website.com/test.php on line 11
The idea is that each individual value should be taken and recorded in SQL base. Now is a common result , not every value separately
First add id in select tag as
<select name="size_select" id="sizeDdl" class="long form-control">
then change these lines
$element = $html->find('#sizeDdl',0);
foreach($element as $elemen) {
echo ($elemen->plaintext);
}
to these:
$text_array = array();
$html = "Your html";
foreach($html->find('#sizeDdl') as $element) {
$options = $element->find('option');
foreach($options as $element1) {
$text_array[] = ($element1->plaintext);
}
}
var_dump($text_array);
print_r(text_array);
change this line
<select name="size_select" class="long form-control">
to this line
<select name="size_select" id="sizeDdl" class="long form-control">
as your dom element should have id attribute which u r using in javascript and missing in html.
There is two issues with your current code.
1) You are trying to fetch an element by an id that does not exist.
2) You are iterating through a list of elements, not its children.
Either find the element by name or give it an id (sizeDbl) and iterate its children (the options) instead.
You could also modify the selector to select a list of the children instead of the select itself:
$options = $html->find('#sizeDdl option');
or
$options = $html->find('#sizeDbl')->find('option');
for getting select values with simple html dom
just try this code it will help you
$element = $html->find('#selectIDGoesHere',0)->find('option');
foreach($element as $elemen) {
echo "Display text:".($elemen->plaintext)."<br>";
echo "value:".($elemen->value)."<br>";
}
Hi I have come across a problem now I do have a solution however it involves many of if statements and I would like to know it there is a neater option
what I would like is
<select>
<option value ="00:00">00:00</option>
<option value ="01:00">01:00</option>
<option value ="02:00">02:00</option>
<option value ="03:00">03:00</option>
</select>
then get an value from and have that box selected
what I'm am currently thinking is
$selectedbox = array();
if($tablevalue == "00:00"){
$selectedbox[0] = 'selected="selected"';
}
if($tablevalue == "01:00"){
$selectedbox[1] = 'selected="selected"';
}
then have
<select>
<option value ="00:00" <?php echo $selectedbox[0]; ?>>00:00</option>
<option value ="01:00" <?php echo $selectedbox[1]; ?>>01:00</option>
<option value ="02:00" <?php echo $selectedbox[2]; ?>>02:00</option>
<option value ="03:00" <?php echo $selectedbox[3]; ?>>03:00</option>
</select>
is there an easier way of getting the same result
for ($i = 0; $i <= 3; $i++) {
$sel = ("0{$i}:00" == $tablevalue) ? ' selected="selected"' : '';
echo <<<EOL
<option value="0{$i}:00"{$sel}>0{$i}:00</option>
EOL;
}
I would get rid of the if statements and put the code inline with the option tag with a ternary. Ex:
<select>
<option value ="00:00" <?=$tablevalue=="00:00"?'selected="selected"':''?>>00:00</option>
<option value ="01:00" <?=$tablevalue=="01:00"?'selected="selected"':''?>>01:00</option>
<option value ="02:00" <?=$tablevalue=="02:00"?'selected="selected"':''?>>02:00</option>
<option value ="03:00" <?=$tablevalue=="03:00"?'selected="selected"':''?>>03:00</option>
</select>
Take a look at PHP switch statements: http://php.net/manual/en/control-structures.switch.php
I think you would want something like this:
switch ($tablevalue) {
case "00:00":
$selectedbox[0] = 'selected="selected"';
break;
case "01:00":
$selectedbox[0] = 'selected="selected"';
break;
//etc, etc
}
PHP switch is a better way to handle if statements if you're always comparing the same variable.
I have a database of Employees and a dropdown list which I generated with PHP, then with javascript I tried to put the right type of blood of each employee in the database.
I have a database of Employees and a dropdown list which I generated with PHP, then with javascript I try to put the right type of blood for each employee in the database.
First I retrieved the data:
$rsEmployee = employee::getEmployees();
while($row = mysql_fetch_assoc($rsEmployee))
{
echo "
<select id='slcBloodType' name='slcBloodType' onload='chooseItem(this, '$row['slcBloodType']')'>
<option value='A+'> A+ </option>
<option value='A-'> A- </option>
<option value='B+'> B+ </option>
<option value='B-'> B- </option>
<option value='AB+'> AB+ </option>
<option value='AB-'> AB- </option>
<option value='O+'> O+ </option>
<option value='O-'> O- </option>
</select>";
}
The javascript function:
function elegirOption(list, value){
array = ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"];
index = -1;
for(var i = 0; i < array.length; i++){
if(value == array[i]){
index = i;
break;
}
}
if(index != -1){
this.selectedIndex = index;
}
}
But is not working, it seems that I can't pass a php variable as a parameter to a javascript function...
There are others dropdown list with which I want to do that.
Why not do it like this:
$blood_types = array("A+", "A-", ...);
echo '<select>';
while(....) {
foreach($blood_types as $blood_type) {
echo '<option';
if($blood_type == $row['slcBloodType'])
echo 'selected="selected"';
echo '>'.$blood_type.'</option>';
}
}
You don't have to specify the value attribute since it's the same as the text.
First you have several issues with your code such as duplicated IDs which is bad. Gere is how i'd do it, this is a massive cleanup of your code but should make it very close to what you had:
At the beginning of your page
It's always better to load your data in memory and then reuse it later below in your output. And no, unless you have 1 million lines, memory shouldn't be an issue...
<?php
//Read my data i need to work with
$rsEmployee = employee::getEmployee();
$employees = array();
while($row = mysql_fetch_assoc($rsEmployee)){
$employees[] = $row;
}
Further down in your page header
Now at this point we'll create the initialization code... You need JQUERY to make this work. It's a very good browser agnostic way to work and saves you HEAPS of trouble.
?><script>
$(function(){ <?php
foreach($employees as $employee){ ?>
$('#slcBloodType<?php echo $employee['id']; ?>').val('<?php echo $employee['slcBloodType']; ?>');
<?php } ?>
});
</script><?php
Further down in the HTML portion of your page
Allright, at this point, now you output the HTML and make sure your id's are unique:
<?php foreach($employees as $employee){ ?>
<select id="slcBloodType<?php echo $employee['id']; ?>" name="slcBloodType[<?php echo $employee['id']; ?>]">
<option value='A+'> A+ </option>
<option value='A-'> A- </option>
<option value='B+'> B+ </option>
<option value='B-'> B- </option>
<option value='AB+'> AB+ </option>
<option value='AB-'> AB- </option>
<option value='O+'> O+ </option>
<option value='O-'> O- </option>
</select>
<?php } ?>
Good luck applying this better coding technique, it'll save you HEAPS of trouble later if you start coding like this right now!
But is not working, it seems that I can't pass a php variable as a parameter to a javascript function...
This is because you cannot directly pass a PHP variable to javascript.
Your options are:
Setting cookies in PHP which javascript can later read
Have PHP echo the javascript code and inbetween have PHP variables printed out. For example:
< script language="javascript" type="text/javascript" >
var levels ="< ?php echo $levels ? >" ;
< /script >
Hi guys I am trying to post values which is getting number from another text box for MySQL select query but i am stuck can u please help me here is my code when I try to get result I cannot add comma(,) between values. also tried implode() and explode() function but the result only got number of array element please help me. I will be glad to try your ideas thanks.
on my sql query i get only row as a result which is my first select
thanks a lot for your help again guys
function exportselectionlist(){
var qcolumns=document.getElementById('selectionlist');
for (i=0; i < qcolumns.length; i++) {
qcolumns.options[i].selected = true;
}
document.selectionlist_form.submit();
}
<form id="selectionlist_form" action="xxx.php" method="post"
name="selectionlist_form">
<select id="selectionlist" style="width:300px;" multiple="multiple" size="4"
name="selectionlist[]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
<input type="submit" value="x" />
<a onclick="exportselectionlist()" href="javascript:;">Export</a>
</form>
//xxx.php
<?php foreach ($selectionlist as $value) {
$resultstr = array();
foreach ($selectionlist as $result)
$resultstr[] = $result;
echo $x=implode(",",$resultstr);
sql = mysql_query("SELECT * FROM table where idArticle in ('$x')");
Try changing your js function to:
function exportselectionlist() {
var qcolumns = document.getElementById('selectionlist');
for (i=0; i < qcolumns.length; i++) {
qcolumns.options[i].selected = true;
}
document.selectionlist_form.submit();
}
and then your "xxx.php" to:
$selectionlist = $_POST['selectionlist'];
echo implode(',', $selectionlist);
As a side note, your php code indicates to me that you have register_globals turned on? I would recommend turning that off in favor of creating the variable you need from the $_POST superglobal.
I've a select menu hardcoded in a wordpress (php) theme, but the manager requires to edit those frequently. Is it possible to populate the select dropdown options from a text file import? So he just has to edit the text file and the menu options would change.
The current menu looks like this:
<select name="location" id="sort-location" class="sort-dropdown">
<option value="" selected="selected">LOCATION:</option>
<option value="" disabled="">--------------</option>
<option value="hongkong">Hong Kong</option>
<option value="taiwan">Taiwan</option>
<option value="mainland_china">Mainland China</option>
<option value="" disabled="">--------------</option>
<option value="">SHOW ALL</option>
</select>
Sure -- make yourself a small loop that runs through the lines in the format you choose.
<?php
$select = file_get_contents('select.txt');
$lines = explode("\n", $select);
foreach ($lines as $line) {
// let's say our format is like this:
// value|name|selected|disabled
// or:
// -
// for separator
if ($line == '-') {
echo '<option disabled="disabled">----------</option>';
} else {
list($value, $name, $selected, $disabled) = explode("|",$line);
echo '<option value="'.$value.'"',
$selected?' selected="selected"':'',
$disabled?' disabled="disabled"':'',
'>'.$name.'</option>';
}
}
?>
Sure, just fetch the text file into an array using file() and create the select out of it. A very simple implementation: menu.txt:
hongkong Hong Kong
taiwan Taiwan
mainland_china Mainland China
Note the tabs between value and label.
Then in PHP:
$menu_items = file("menu.txt");
foreach ($menu_items as $menu_item)
{
// Explode
$menu_item_exploded = explode("\t", $menu_item);
$option_value = htmlspecialchars(trim($menu_item_exploded[0]));
$option_label = htmlspecialchars(trim($menu_item_exploded[1]));
echo "<option value='$option_value'>$option_label</option>";
}
As far as I can see, you have the following left to solve:
How to pre-set a predefined value (You need to echo selected in the right item)
How to deal with the user editing out a value from the text file that was pre-set in your select.
Error handling if the file is not existent or not accessible
Error handling if the user screws up the line breaks, or something similar - maybe count the lines, and/or detect whether there are tabs inside the file