6 Replies - 3547 Views - Last Post: 09 March 2010 - 06:13 AM Rate Topic: -----

#1 z99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-February 10

drop down menu without button

Posted 08 March 2010 - 10:05 AM

I'm trying to write a php script that will populate a second drop down menu based on the selection of the a primary drop down menu
the second drop menu will get it's items from a table called city [mysql database],
with what i've done i must press the submit button to do that,
but i don't want to press any button,
i mean,
when the first item from the primary menu is selected the second menu,show the items automatically,without pressing any button
how can i do that??!!
each value in the option in the html part is equal to the id in it's table called ostan i mean
ostan table:
id,name=>1,mazandaraz
2,gilan
3,tehran
<?php
$l=mysql_connect('localhost','root','');
mysql_query('use test',$l);

$oid=$_POST['ostan'];
foreach($oid as $r)
$roid=$r;
//echo $roid.'<br>';

$qf="select id,name from ostan";
$exeqf=mysql_query($qf,$l);

while($res=mysql_fetch_array($exeqf))
{
if($res['id']==$roid)
//echo 'ostan='.$res['name'];
{
$qs="select id,name from city where oid='$roid'";
$exeqs=mysql_query($qs,$l);

echo "<select name='city'><option value=''>Select one</option>";
echo '<br>';
while($res2=mysql_fetch_array($exeqs))
{
//echo 'city='.$res2['name'];
echo "<option selected value='$res2[id]'>$res2[name]</option>"."<BR>";
}//second while

}//first if
}//first while
echo '</select>';
?>

<html><body>
<form name='form' method='post' action="<?php print $SERVER['PHP_SELF'] ?>" >
select your ostan:
<select name='ostan[]'>
<option value='' >Select One</option>
<option value='1'>mazandaran</option>
<option value='2'>gilan</option>
<option value='3'>tehran</option>
</select>
<br>
<input type='submit' value='submit'>
</form></body></html>

This post has been edited by JackOfAllTrades: 08 March 2010 - 05:05 PM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: drop down menu without button

#2 KingCuddles  Icon User is offline

  • D.I.C Regular

Reputation: 176
  • View blog
  • Posts: 496
  • Joined: 20-December 08

Re: drop down menu without button

Posted 08 March 2010 - 10:39 AM

Ok before I will even read that, please use code tags:

[ php ] code here [ / php]

Remove the spaces though.
Was This Post Helpful? 0
  • +
  • -

#3 z99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-February 10

Re: drop down menu without button

Posted 08 March 2010 - 10:58 AM

sorry, but what do you mean code tags!!
Was This Post Helpful? 0
  • +
  • -

#4 hadi_php  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: drop down menu without button

Posted 08 March 2010 - 11:05 AM

use Posted Image tags . and i cannt understand what you are trying to say...well as i see you need ajax which will automatically change while you select value from a list box
Was This Post Helpful? 0
  • +
  • -

#5 z99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-February 10

Re: drop down menu without button

Posted 08 March 2010 - 04:51 PM

thanks for your reply,
would you please give me some ajax resource that can help me with this
or i don't know a piece of code may be,or whatever that can solve my problem.
thanks in advance
Was This Post Helpful? 0
  • +
  • -

#6 garfinkle  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 87
  • Joined: 08-September 09

Re: drop down menu without button

Posted 08 March 2010 - 05:07 PM

Hava a look at JQuery (http://jquery.com) and have a look at their AJAX implementation system.

You will want to assign your drop down menu an ID and assign the an javascript function to your select box as follows

<!-- in the header -->
<script type="text/javascript">
<!--

$(document).ready(function()
{
    $("#first_select").onchange(function()
    {
        $.ajax({ 
           url: "getSecondSelectData.php",
           data: "first=" + $("#first_select").val(),
           success: function(data){
               $("#second_select").html(data);
           }
        });
    });
});

-->
</script>
...

<!--  later in the body code -->

<select id="first_select">
<!-- enter select content -->
</select>

<select id="second_select">
<!-- leave empty as this will be set by the javascript when the first select value is changed -->
</select>




Hope this helps.

Have a good read through the JQuery ajax documentation at http://api.jquery.com/jQuery.ajax/ to learn how to use it completely

Hope this helps

This post has been edited by garfinkle: 08 March 2010 - 05:08 PM

Was This Post Helpful? 1
  • +
  • -

#7 z99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-February 10

Re: drop down menu without button

Posted 09 March 2010 - 06:13 AM

thanks for helping dude
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1