drop down list
Scris: Vin Sep 09, 2011
Buna.
Am doua drop down list pt selectia judet - localitate. Codurile sunt urmatoarele:
j_judete.php
j_localitati.php
tabelul judete.sql are 2 campuri : judet varchar(30) si localitate varchar(40).
Dupa ce dau submit imi dispare selectorul cu localitati. Trebuie sa selectez din nou un judet ca sa reapara. Cum se poate rezolva problema ?
Multumesc anticipat.
Am doua drop down list pt selectia judet - localitate. Codurile sunt urmatoarele:
j_judete.php
Cod: Selectaţi tot
<SCRIPT>
function Inint_AJAX() {
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function fjud(){
var req = Inint_AJAX();
var judet = document.getElementById('judet').value;
req.open("GET", "j_localitati.php?judet=" + judet);
req.onreadystatechange = function () {
if (req.readyState==4) {
document.getElementById('div_localitate').innerHTML = this.responseText;
}
}
req.send(null);
}
</SCRIPT>
<?php
include('conectaredb.php');
$fval=array('judet'=>'');
$fval = array_replace($fval,$_POST );
$sql=mysql_query("select DISTINCTROW judet from judete order by judet ASC");
$row=mysql_num_rows($sql);
$camp = array();
while ($row = mysql_fetch_assoc($sql)) {
$camp[] = $row["judet"];
}
$judet = '<option>----------</option>';
for($i=0; $i<count($camp); $i++) {
$selattr = ($camp[$i]==$fval['judet']) ? ' selected="selected"' : '';
$judet .= '<option value="'. $camp[$i]. '"'. $selattr. '>'. $camp[$i]. '</option>';
}
echo '<select class="styleinput" id="judet" name="judet" style="margin-top:0px;" onchange="fjud();">'. $judet. '</select>';
mysql_close($con);
?>
<br>
<div id="div_localitate" name="div_localitate" style="width: 300px; height:20px; margin-left:3px; margin-top:8px;">
</div>
Cod: Selectaţi tot
<?php
$w_judet = $_GET['judet'];
$fval1=array('localitatea'=>'');
$fval1 = array_replace($fval1,$_POST );
include('conectaredb.php');
$sql=mysql_query("select localitate from judete where judet='".$w_judet."' order by localitate ASC");
$row=mysql_num_rows($sql);
$camp = array();
while ($row = mysql_fetch_assoc($sql)) {
$camp[] = $row["localitate"];
}
$localitatea = '<option>----------</option>';
for($i=0; $i<count($camp); $i++) {
$selattr = ($camp[$i]==$fval1['localitatea']) ? ' selected="selected"' : '';
$localitatea .= '<option value="'. $camp[$i]. '"'. $selattr. '>'. $camp[$i]. '</option>';
}
echo '<select class="styleinput" id="localitatea" name="localitatea" height="19px;">'.$localitatea.'</select>';
mysql_close($con);
?>
Dupa ce dau submit imi dispare selectorul cu localitati. Trebuie sa selectez din nou un judet ca sa reapara. Cum se poate rezolva problema ?
Multumesc anticipat.