Drop down button

Secţiune dedicată începatorilor.

Moderatori: Moderatori, Start Moderator

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Drop down button

Mesajde andra2103 » Sâm Aug 13, 2011 3:42 pm

Am urmatoarele 2 coduri pt drop down bottun :

judetselect.php

Cod: Selectaţi tot

<body>
<SCRIPT language="JavaScript">
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function fjudet(form) {
var req = Inint_AJAX();

Item = form1.categorie.selectedIndex;;
Result = form1.categorie.options[Item].text;

req.open("GET", "judete1.php?Result=" + Result);

req.onreadystatechange = function () {
   if (req.readyState==4) {
      alert(this.responseText);
    }
}
req.send(null);
}
</SCRIPT>

<form name="form1">
<select id="categorie" name="categorie" "style="width: 202px" style="width: 170px" onchange="fjudet(this.form);">';
         <?php include("judete1.php");?>
</select>
</form>
</body>


judete1.php

Cod: Selectaţi tot

<?php

$host = "localhost";
$utilizator = "root";
$parola = "";
$numebd = "proiect";

$con = mysql_connect($host, $utilizator, $parola);
if (!$con) {
   echo 'Eroare la conectarea bazei de date.'; 
    exit;
}
mysql_select_db($numebd, $con);

$res=mysql_query("select DISTINCTROW judet from judete order by judet ASC");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
   $row=mysql_fetch_assoc($res);
   echo"<option>$row[judet]</option>";
}

echo $_GET['Result'];
?>


totul OK pana la echo $_GET['Result'] pt care imi da eroarea

"Notice: Undefined index: Result in C:\Program Files\EasyPHP-5.3.5.0\www\Proiect\judete1.php on line 23"

Hepl ! Ce nu-i bine in script, de ce nu transmite variabila result php-ului ...



Sonic3R
Average Member
Mesaje: 76
Membru din: Dum Dec 05, 2010 4:14 pm

Mesajde Sonic3R » Sâm Aug 13, 2011 4:01 pm

incearca
Result = form1.categorie.options[Item].value;

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 4:48 pm

am incercat si asa si nu merge. Cand intru in pagina imi da tot eroarea scrisa mai sus iar dupa ce selectez judetul, imi apare fereastra de alert(this.responseText) care imi spune 'undefined'.

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 5:04 pm

de eroarea din php am scapat, am pus in judete1.php in loc de
echo $_GET['Result'] conditia

Cod: Selectaţi tot

if(isset($_GET["Result"])){$w_Result=$_GET["Result"];}
else $w_Result="";

echo $w_Result;

si merge, in schimb in fereastra de alert tot undefined imi da si ca urmare in php nu-mi transmite nicio valaore . :(

preda.vlad
Average Member
Mesaje: 192
Membru din: Vin Mar 20, 2009 4:56 pm
Contact:

Mesajde preda.vlad » Sâm Aug 13, 2011 5:14 pm

Nu te mai complica si pune value la options.

Cod: Selectaţi tot

<option value="<?php echo $row[judet]; ?>"><?php echo $row[judet]; ?></option>


Si o sa poti sa iei valoarea asa :

Cod: Selectaţi tot

var Result = document.getElementById('categorie').value;

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Mesajde cipcip » Sâm Aug 13, 2011 5:26 pm

Mie imi merge numai ca in alert box imi apare asa ceva:

Cod: Selectaţi tot

<option>Bucuresti</option><option>Constanta</option><option>Craiova</option>Craiova


Adica in afara de valoare, apar si optiunile pe care le am in baza de date.

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 5:37 pm

si ai rulat exact codurile cum le-am postat eu mai sus ? Cum de mie imi da undefined :?

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 5:42 pm

@preda.vlad

am modificat dar tot aceeasi eroare cu undefined imi da in fereastra de alert
asa arata codurile acum

judetselect.php

Cod: Selectaţi tot

<html>
<body>
<SCRIPT language="JavaScript">
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function fjudet(form) 
{
var req = Inint_AJAX();

//Item = form1.categorie.selectedIndex;;
//Result = form1.categorie.options[Item].text;
var Result = document.getElementById('categorie').value;

req.open("GET", "judete1.php?Result=" + Result); //make connection

req.onreadystatechange = function () {
   if (req.readyState==4) {
      alert(this.responseText);
    }
}
req.send(null);

}
</SCRIPT>

<form name="form1">
<select id="categorie" name="categorie" "style="width: 202px" style="width: 170px" onchange="fjudet(this.form);">';
         <?php include("judete1.php");?>
</select>
</form>
</body>

</html>


judete1.php

Cod: Selectaţi tot

<?php

$host = "localhost";
$utilizator = "root";
$parola = "";
$numebd = "proiect";

$con = mysql_connect($host, $utilizator, $parola);
if (!$con) {
   echo 'Eroare la conectarea bazei de date.'; 
    exit;
}
mysql_select_db($numebd, $con);

$res=mysql_query("select DISTINCTROW judet from judete order by judet ASC");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
   $row=mysql_fetch_assoc($res);
   echo"<option value='$row[judet]'>$row[judet]</option>";
}
if(isset($_GET["Result"])){$w_Result=$_GET["Result"];}
else $w_Result="";

echo $w_Result;
?>

preda.vlad
Average Member
Mesaje: 192
Membru din: Vin Mar 20, 2009 4:56 pm
Contact:

Mesajde preda.vlad » Sâm Aug 13, 2011 5:48 pm

Ce browser folosesti ?

Si ... am o curiozitate. De ce incluzi fisierul judete1.php dar il folosesti si pentru ajax ?

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 5:55 pm

folosesc IE

in judete1.php o sa fac un alt drop down button cu filtru pt orasele aferente judetului selectat, o sa introduc asa

Cod: Selectaţi tot

$res=mysql_query("select oras from judete where judet='".$w_Result."' order by oras ASC");

if(mysql_num_rows($res)==0)
{
   echo "nu exista date ..";
} else {
echo '<select>';
   while ($row=mysql_fetch_assoc($res)){
      echo "<option value='$row[oras]>".$row['oras']."</option>";
   }
}
echo '</select>';

preda.vlad
Average Member
Mesaje: 192
Membru din: Vin Mar 20, 2009 4:56 pm
Contact:

Mesajde preda.vlad » Sâm Aug 13, 2011 6:00 pm

Pentru development foloseste Firefox cu FIrebug, merge si Chrome, dar niciodata IE.

Si ... ce IE ? ca sunt multe versiuni de IE ...

Daca tot ai probleme foloseste jquery, eu am testat ce ti-am scris si mi-a mers perfect.


Pune codul asta intr-un fisier html si deschidel in browser, si vezi daca merge.

Cod: Selectaţi tot

<select id="categorie" onchange="fct();">
<option value="1">unu</option>
<option value="2">doi</option>
<option value="3">trei</option>
</select>
<Script type="text/javascript">
function fct() {
alert( document.getElementById('categorie').value);
}
</script>


Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 6:02 pm

Idea e ca am facut ieri un cod functional, dar am folosit location.href care imi deschide o alta pagina pt selectia oraselor ori eu vreau in aceeasi pagina imediat sub butonul de judete (sa apara si selectia pt orasele din judetul respectiv).

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 6:06 pm

preda.vlad scrie:Pentru development foloseste Firefox cu FIrebug, merge si Chrome, dar niciodata IE.

Si ... ce IE ? ca sunt multe versiuni de IE ...

Daca tot ai probleme foloseste jquery, eu am testat ce ti-am scris si mi-a mers perfect.


Pune codul asta intr-un fisier html si deschidel in browser, si vezi daca merge.

Cod: Selectaţi tot

<select id="categorie" onchange="fct();">
<option value="1">unu</option>
<option value="2">doi</option>
<option value="3">trei</option>
</select>
<Script type="text/javascript">
function fct() {
alert( document.getElementById('categorie').value);
}
</script>


am testat ... merge.

cipcip
Senior Member
Mesaje: 201
Membru din: Dum Feb 22, 2009 1:26 pm

Mesajde cipcip » Sâm Aug 13, 2011 6:11 pm

Codul care imi merge mie de iti da ca rezultat asa ceva:

Cod: Selectaţi tot

<option value=Bucuresti>Bucuresti</option><option value=Constanta>Constanta</option><option value=Craiova>Craiova</option>Craiova


Ca fisiere...

judete1.php

Cod: Selectaţi tot

<?php

$host = "localhost";
$utilizator = "root";
$parola = "";
$numebd = "proiect";

$con = mysql_connect($host, $utilizator, $parola);
if (!$con) {
   echo 'Eroare la conectarea bazei de date.';
    exit;
}
mysql_select_db($numebd, $con);

$res=mysql_query("select DISTINCTROW judet from judete order by judet ASC");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
   $row=mysql_fetch_assoc($res);
   echo"<option value=".$row['judet'].">".$row['judet']."</option>";
}
if(isset($_GET["Result"])){$w_Result=$_GET["Result"];}
else $w_Result="";

echo $w_Result;
?>


judetselect.php

Cod: Selectaţi tot

<html>
<body>
<SCRIPT language="JavaScript">
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function fjudet(form)
{
var req = Inint_AJAX();

//Item = form1.categorie.selectedIndex;;
//Result = form1.categorie.options[Item].text;
var Result = document.getElementById('categorie').value;

req.open("GET", "judete1.php?Result=" + Result); //make connection

req.onreadystatechange = function () {
   if (req.readyState==4) {
      alert(this.responseText);
    }
}
req.send(null);

}
</SCRIPT>

<form name="form1">
<select id="categorie" name="categorie" "style="width: 202px" style="width: 170px" onchange="fjudet(this.form);">';
         <?php include("judete1.php");?>
</select>
</form>
</body>

</html>

Avatar utilizator
andra2103
Junior Member
Mesaje: 35
Membru din: Mie Iul 02, 2008 9:17 pm

Mesajde andra2103 » Sâm Aug 13, 2011 6:11 pm

iupiiii ... gata merge :D
trec mai departe, daca e ceva revin :wink:

@preda.vlad ... exemplu tau ma ajutat sa inteleg unde am gresit, e la fereastra de alert :wink:


Înapoi la “PHP Incepători”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Google [Bot] și 44 vizitatori