create dynamic dropdown list using append in javascript - javascript

Hi when I'm using static code into jquery for create dynamic dropdown list using append() then its working, like
$('#optionNameCity')
.empty()
.append('<option selected="selected" value="Wakad">Wakad</option><option value="Hinjewadi"> Hinjewadi </option>');
}
but when I'm using php script for create dynamic list then its failure and not showing any output, like
$('#optionNameCity')
.empty()
.append($(<?php include('db.php');$loc= mysql_query("select city from location");while($row=mysql_fetch_array($loc)){echo "<option selected='selected' value='".$row['city']."'>" . $row['city'] . "</option>";}?>));
}
Please guide me where I'm going wrong.
Thanks

<?php include('db.php');
$loc= mysql_query("select city from location");
$cityList="";
while($row=mysql_fetch_array($loc))
{
$cityList="<option selected='selected' value='".$row['city']."'>" . $row['city']."</option>";
}
?>
<?php if($cityList!=""){ ?>
$('#optionNameCity option').remove();
$('#optionNameCity).append("<?php echo $citylist; ?>");
<?php } ?>
Replace your code with above code

Related

dynamic dropdown is not working in php, data is not fetching from the database

I want to make a dropdown and values should be selected from the database. But in my case values are not shown in the dropdown.
[1]: https://i.stack.imgur.com/6F6O9.png
Do you get any errors?
Also here's the way I usually do this kind of thing
<?php
$query = "SELECT * FROM parent_category";
$results=mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($results))
{
?>
<option.....<?php echo $row['parent_id']; ?>
<?php
}
//php continues
?>

How can I get the ID as value and Name to display in dropdownlist php

i have a problem with that i need to get the value from the dropdown list to be a number and the name for a kategory to be the name that the user picks.
<select name="kategori">
<?php
$query=mysql_query("SELECT KategoriID from Kategori");
$second=mysql_query("SELECT KategoriNavn from Kategori");
while($r=mysql_fetch_row($query) && $v=mysql_fetch_row($second)){
echo "<option value='$r[0]>$v[0]</option>";
}
?>
This is the code i have, but i cant make it to work.
Im kinda new to PHP. Thanks!
There's no need to write two different queries. You could have written just a single one. I think mysql fetch_assoc is a tad easier to understand.
You can try something like this:
<?php
$query = mysql_query("SELECT KategoriID, KategoriNavn from Kategori") or die(mysql_error()); // Debugging displays SQL syntax errors, if any.
echo "<pre>";
print_r($query);
exit; // Let me know what the array looks like.
while ($r= mysql_fetch_assoc($query)) { ?>
<option value=<?php echo $r['KategoriID']; ?> >
<?php echo $r['KategoriNavn']; ?>
</option>
<?php } ?>
<?php
echo "<pre>";
print_r($_POST); // Do this where you're checking your POST data
exit;
?>
Assuming you want option value to be KategoriNavn and the option to display to be KategoriID.
Hope this helps.
Peace! xD

js mysql php select2.js add 2 values to dropdown one of them should to call from js

Please need a help on how to call the current select color to the selective item in the dropdown.
dropdown:
database:
php code:
<select data-placeholder="Escolha Categoria..." class="select-search" name="categoria_id" tabindex="2" id="category_drop">
<option value="">Categoria</option>
<?php foreach($categorias_produtos as $categoria_produto){ ?>
<option value="<?php echo $categoria_produto->categoria_id; ?>"><?php echo $categoria_produto->categoria_nome; ?></option>
<?php } ?>
</select>
js code:
<script>
function format(produto) {
if (!produto.id) return produto.text; // optgroup
return "<em class='fa fa-circle space_for_product' style='color:<?php
foreach($categorias_produtos as $categoria_produto){
echo $categoria_produto->categoria_cor;
}
?>'></em>" + produto.text;
}
$("#category_drop").select2({
formatResult: format
});
and this is the result:
<div class="select2-result-label"><em class="fa fa-circle space_for_product" style="color:#ff0040;#ff4000;#00ff40;#00bfff;#ffff00;#bfff00;#4000ff;#ffbf00;#ff00bf;"></em>Farinhas</div>
so how to call just the correct color from database for each item in dropdown ?
to be interpreted PHP code must pass through the server.
for javascript, it's the console inside the browser who does it. So if you put some php inside Js code. it won't work.
the solution is to get the colors first and write them in an invisible block with an id then get the values later by javascript
<?php
echo '<div id="colors" display = "none">';
foreach ($ categorias_produtos as $ categoria_produto) {
         echo "$ categoria_produto-> categoria_cor"." ,";
     }
       echo '</ div>';
?>
then you will use :
<script>
colorString = document.getElementById("colors").value;
colorArray = colorString.split(",");
</script>
i wish that was usefull for you. good day.

Drop down menu in PhP javascript html

Need your help in my application i have a php page (profile.php) that give me this result:
Test1/Test
Test2/Test0
...
I want to put each line in this result in a selection bar like this one:
but in fact the result was like this one :
this is my code for m drop down menu:
<option value="0">please select an existing profile</option>
<?php
require('profile.php');
?>
code profile.php:
<?php
$output = shell_exec('/etc/init.d/dima --get-profilelist');
echo "<option value=\"" . $output ."\">".$output."</option>";
?>
Your result should looks like :
<option value="0">please select an existing profile</option>
<option value="1">Test1/Test</option>
<option value="2">Test2/Test0</option>
Yours is like that :
<option value="0">please select an existing profile</option>
<option value="0">Test1/Test Test2/Test0</option>
You have to enclose each result line in the tag so that it can work as expected
$output is taking all the content in one line...you can use var_dump() to see how data is returned in $output and edit that... You need to use for each value in the dropdown
You should loop your $output and echo every option, Try This :
<?php
$output = shell_exec('/etc/init.d/dima --get-profilelist');
$optionsArray = explode(' ',$output);
foreach(optionsArray as $option)
echo "<option value='". $option."'>".$option."</option>";
?>
Hope this will help.

repopulating multiple select form with a drop down form through ajax

i want to repopulate a multiple select form from the database with ajax by just selecting a drop down value.
here is the code for the drop down menu:
<?php
$sql2 = "select _id, title from sub_category order by title;";
$sel2 = mysqli_query($connect,$sql2);
$array2 = array();
while($row2 = mysqli_fetch_assoc($sel2)){
$array2[] = $row2;
}
?>
<div class="span2">
<select name="des_pos" id="des_pos">
<?php
foreach($array2 as $value2){ ?>
<option value ="<?php echo $value2['_id']; ?>" <?php if($value2["title"] == $desired_position){ echo 'selected="selected"';} ?>><?php echo $value2['title']; ?> </option>
<?php
}
?>
</select>
</div>
and here is the code for the multiple select form:
$sql4 = "SELECT _id, score_type from test_category where sub_code='$des_pos_id'";
$sel4 = mysqli_query($connect,$sql4);
$array4 = array();
while($row4 = mysqli_fetch_assoc($sel4)){
$array4[] = $row4;
}
<select name = 'test_tags[]' multiple>
<?php
foreach($array4 as $value4){ ?>
<option value ="<?php echo $value4['_id']; ?>" <?php echo in_array($value4['_id'], $test_tag) ? 'selected="true"' : null; ?>><?php echo $value4['score_type']; ?></option>
<?php
}
?>
</select>
so the the output that i want is, the values of the multiple select form should change depending on the choice on the dropdown menu...
i need a jquery.ajax code for this but i don't know where to begin... i am getting the value through a $_POST.. but i want to do it without going through another page and redirecting.
some helpful stuff:
AJAX Tutorial: W3schools ajax tutorial
then you need to learn about HTML DOM
and then you need to learn about Select DOM Object
mixing those will teach you how to figure out your question.
In the first dropdown #des_pos select event send the ajax post to the php page as follow.
$("#des_pos").select(function(e){
$.ajax({
url:"getdropdata.php",
dataType:"json",
success: function(data){
$.each(data,function(index,value){
$("#multiselect").append("<option value="+value+">"+value+"</option>"));
});
}
})
});

Categories