Hoi

Ik heb op allerhande sites gezeten om erachter te komen wat ik fout heb gedaan, maar allemaal zonder vruchten af te werpen... Het probleem is het dat de eerste dropdown geen gegevens uit de DB opneemt en daadoor de tweede dropdown niet juist filterd (moet ie dus doen a.h.v. de eerste). Wat momenteel het resultaat is, ts dat dropdown 2 ALLE gegevens laat zien (ongefilter, wel de correcty array) maar dropdown 1 helemaal niets, dus filteren gaat niet.

Eens stukje uit de SQLdump:

CREATE TABLE `re2_listings` (
`ListingID` int(10) NOT NULL auto_increment,
`AgentID` int(10) NOT NULL default '0',
`CategoryID` int(10) NOT NULL default '0',
`SubcategoryID` int(10) NOT NULL default '0',
`address` text NOT NULL,
`city` varchar(100) NOT NULL default '',
`state` varchar(100) NOT NULL default '',
`country` varchar(150) NOT NULL default '',

en de gebruikte code:

<?
require_once("conn.php");

connecttodb($db_host,$db_name,$db_username,$db_password);
function connecttodb($db_host,$db_name,$db_username,$db_password)
{
global $link;
$link=mysql_connect ("$db_host","$db_username","$db_password");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$db_name",$link) or die ("could not open db".mysql_error());
}
//////// End of connecting to database ////////
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.search_country.options[form.search_country.options.selectedIndex].value;
self.location='mainsearch.php?search_country=' + val ;
}

</script>
</head>

<body>
<?

@$search_country=$_GET['search_country']; // Use this line or below line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT country FROM re2_listings order by country");


/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($search_country) and strlen($search_country) > 0){
$quer=mysql_query("SELECT DISTINCT city FROM re2_listings where search_country=$search_country order by city");
}else{$quer=mysql_query("SELECT DISTINCT city FROM re2_listings order by city"); }




echo "<form method=post name=f1 action='search.php'>";

////////// Starting of first drop downlist /////////
echo "<select name='search_country' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['search_country']==@$search_country){echo "<option selected value='$noticia2[search_country]'>$noticia2[search_country]</option>"."<BR>";}
else{echo "<option value='$noticia2[search_country]'>$noticia2[search_country]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////

////////// Starting of second drop downlist /////////
echo "<select name='city'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[city]'>$noticia[city]</option>";
}
echo "</select>";
////////////////// This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center>
</body>

</html>

$noticia2['search_country'], bestaat deze variabele wel? Je haalt met $quer2 alleen de kolom 'country' op, dus 'search_country' zal dan geen onderdeel zijn van de resultaatset.

Off-topic: zet ook eens dit bovenaan je script:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
en haal alsjeblieft die foutonderdrukkingen (@) uit je script. Als je aan het scripten bent wil je namelijk alle fouten kunnen zien. Of een variabele bestaat controleer je gewoon met isset() en je gaat hem niet eerst proberen te declareren waarbij je de fout onderdrukt als dat niet lukt.
Tip download prototype -> http://www.prototypejs.org

Include het in je header.
Doe dan het volgende

index.php

<?php
<script type="text/javascript">
funtcion get_data()
{
    new Ajax.Request("getdata.php",{
        postBody : "selected="+$("select1") ,
        onComplete : function(t) {
                $("select2").innerHTML = t.responseText
        }
    })
}
</script>
<select id="select1">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
</select>
<div id="select2">
</div>
?>


getdata.php

<?php
    $return = "<select>"
    $query = "SELECT * FROM tabel WHERE id=".$_POST["selected"] ;
    $result = mysql_query($query) ;
    while ($rij = mysql_fetch_array($result))
    {
         $return .= "<option>".$rij["naam"]."</option>"
    }
    $return .= "</select>"
?>


Hoop dat je hier iets aan hebt. Deze code zal natuurlijk niet direct werken maar moet je aanpassen naar eigen omgeving

..::Typfouten voorbehouden::..
@Blanche

De error die ik op de pagina krijg is

Notice: Undefined index: search_country in /home/.fitz/snewpers/domain/mainsearch.php on line 38

en op 38 staat:

$search_country=$_GET['search_country'];

ik heb $noticia2['search_country'] veranderd in $noticia2['country'], maar dat veranderde niets, of doelde je op $noticia2?

bedankt!

Je moet natuurlijk wel overal $noticia2['search_country'] veranderen in $noticia2['country'].

En ja het klopt dat je die error (kan) krijgen. Dit is namelijk het geval als $_GET['search_country'] niet bestaat. Controleer dus altijd eerst met isset() of een variabele wel bestaat. Daarnaast is regel 3 in jouw script op dit moment erg overbodig tenzij je er bijvoorbeeld dit van maakt:
<?php
if(isset($_GET['search_country']))
{
$search_country = mysql_real_escape_string($_GET['search_country']);
}
?>
En vervolgens is je check op regel 10 ook overbodig, aangezien je dat op regel 3 al doet. Ga dus regel 10 e.v. combineren met regel 3.
brrrr

de error die er kwam is nu wel weg, maar nu gaat ie direct zoeken zonder de tweede dropdown te raadplegen. Die staat er wel, en alles is selecteerdbaar (zonder filter) totdat er een land wordt gekozen.

ben bang dat ik dit niet zelf klaar krijg :/

Bedankt!

JP
JP als je me het volledige script stuur INCLUSIEF uitleg wat het moet doen. zal ik kijken of ik vanavond tijd heb om het voor je na te kijken.

Pm me maar een downloadlink en je email adres.


-- onzinnige post verwijderd--

Reageren