refresh and re-populate select box - php

I populate my select box using php code below, my problem now is that i have my reference form which add new items to this select box. what I want is when I add new drawer item from my reference, i want to refresh this select box only not affecting the whole page. I read about ajax/jquery/js solution, but i don't know how to integrate it.
<td>Drawer</td>
<td>
<select name="municipality" class="country" style="width: 200px;">
<option selected="selected"></option>
<?php
$sql=mysql_query("select idDrawer,drawerName from drawer");
while($row=mysql_fetch_array($sql))
{
$id=$row['idDrawer'];
$data=$row['drawerName'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
?>
</select>*
</td>

Your question leads to a broad discussion. I would suggest following these references:
http://www.youtube.com/watch?v=0CMTQtnZ0G0
http://www.youtube.com/watch?v=SbQUJdimia4
http://www.youtube.com/watch?v=GVHphoGi5dY

Related

display result based on drop down list

I would like to display the result which I have selected based on the drop down list. This is my drop down list code which is retrieved from the finish_product under bom table (calculate.php)
<tr>
Select product:
<select class="itemTypes">
<?php
while ($row1 = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
</tr>
For example, under the drop down list I selected 'Table', how do I display the result in doCalculate.php?
Right now I just hard code Table which is not feasible. Anyone to help me out?
There are many, many ways which you could accomplish your goal. I would suggest using a JavaSctipt "onchange" event, detect the selected option, and then update the DOM with its contents.

Populating html select box by data from mysql database depending on another select box

I've been struggling with this problem for a while. I have read tons of posts and tutorials who do something simmilar but cant really get it working for me.
I need to make php form which contains 2 html select boxes : school and class.
I can easly get it to show all schools from my database but cant get my head around how to depending on selected school show classes from that school.
I know this question is more time consuming then others might be, but I will be very happy if you chose to help me!
Kristaps
Since, you have not provided database structure of your database. I
assumed from myself and wrote query accordingly. Change your query as
per your need.
<div>
<select class='Schools' name='SchoolName'>
<option value="">Select School</option>
<?
$Query="SELECT SchoolIDColumnName,SchoolNameColumn FROM SchoolsTable";
foreach($pdo->query($Query) as $row)
{?>
<option value="<?echo {$row['SchoolIDColumnName']};?>"><?echo {$row['SchoolNameColumn']};?></option>
<?}?>
</select>
</div>
<div class='ShowClassName'>
<select>
<option value="">Select Class Name</option>
</select>
</div>
<script>
$('.Schools').change(function(){
var SchoolID=$('.Schools').val();
$.ajax({url:"Ajax-ShowClassName.php?SchoolID="+SchoolID,cache:false,success:function(result){
$('.ShowClassName').html(result);
}});
});
</script>
Ajax-ShowClassName.php
<?
$SchoolID=$_GET['SchoolID'];
$QueryClassName="SELECT * FROM ClassTable WHERE SchoolIDColumnName='$SchoolID'";
?>
<select name='ClassName'>
<?
foreach($pdo->query($QueryClassName) as $row)
{?>
<option value="<?echo {$row['ClassIDColumnName']};?>"><?echo {$row['ClassColumnName']};?></option>
<?}?>
</select>

select drop down from db query based on previous selection (PHP/HTML)

i need to populate another drop down based on previous selected drop down. please help.My code is below. Now, how i pass the previous selected value to next dropdown where clause?
<td align="right">Country</td>
<td>
<select type="text" name="Country">
<option selected value="">Country Code</option>
<?php $result=mysql_query("SELECT CountryName FROM tbl_country");
while($row_result=mysql_fetch_row($result))
{ echo "<option value=\"$row_result[0]\">$row_result[1]</option>";}
?>
</select>*
</td>
<td align="right">Area</td>
<td>
<select type="text" name="Area">
<option selected value="">Area</option>
<?php $result=mysql_query("select Area from tbl_area where CountryName='???'");
while($row_result=mysql_fetch_row($result))
{ echo "<option value=\"$row_result[0]\">$row_result[0]</option>";}
?>
</select>*
</td>
How can i set the selected country name into the second queries where clause?
Thanks in advance.
As PHP is run on the server you will need to request the server to run the second query for you by either having the page refresh or do it via AJAX so that the page refreshes but without looking like it has.
The easiest solution would be to do it across 2 pages, if you want to do it on the one then AJAX is the way forward.
Give me a minute and i'll look for an example.

select field in form is not sending data using php

so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine?
<form method='POST' action='add.php'>
<select>
<option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option>
<?php
$dis=show_groups_select_list($_SESSION['user_id']);
foreach($dis as $key=>$list){
echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>';
}
?>
</select>
</form>
You need to put the name attribute on the select tag, not the option tag.
<select name="user_page_id">
<?php foreach ($dis as $key => $list): ?>
<option value="...">...</option>
<?php endforeach; ?>
</select>
I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:
<select name="any_name" class="form-control">
<option value="this is what would be sent if selected"> sth </option>
</select>
for more information take a glance at this discussion too!

how do i trigger the javascript event with dropdown and change the values?

I am creating a form where the users will have to select the country from the drop down list. as soon as they selects it should activate another drop down list where it should show the states belonging to the country and hence it should process the value from country Id of element. and as soon as the user selects the states another drop down list should activate listing the cities belonging to that particular states, and hence so on to areas.
for that i have written the php and html code like this.
Select Countries : <select name="countries"><br/>
<?php
foreach($property->getAllCountries() as $countries)
{
?>
<option value="<?php echo $countries['id']; ?>"><?php echo $countries['name']; ?></option>
<? } ?>
</select><br/>
Select State : <select name="state">
<?php
foreach($property->getStatesFromCountryId($countryId) as $states)
{
?>
<option value="<?php echo $states['id']; ?>"><?php echo $states['name']; ?></option>
<? } ?>
</select>
the javascript should automatically insert $countryId from and populate the states after the user selects it.
i am noob at javascript i would appreciate if someone could guide me how to achieve this?
thank you.
sorry for bad english.
you should add onchange="showstates()" to select countries and a div to add states of country to it
function showstates()
{
var country=document.getElemtById("countries").value;
if (country=="anywhere")
{
document.getElemtById("statesofcountries").innerHTML='<div>"states list"</div>';
}
}
check this link
You can always google it up:
http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html

Categories