Hi.
This java script dont work on the second dropdown.

The first dropdown rewrights the URL with param. cat.

The problem is that the second time it dos not pick up the town param.

Please help me im not a javascript persson but trying.....

//Mikael

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

<html>

<head>

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


function reload2(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;

txt1=val;
txt2="&town=";
txt3=txt1+txt2;
txt4=txt3;

self.location='image_upload.php?cat=' + txt4 ;
}
</script>

</head>


<?php

	
	include('configs/dbconfig.php');
	//Connect to  your rating database
	$servername        = $db_host;
	$dbuser        = $db_user;
	$dbpassword        = $db_pass;
	$dbname        = $db_name;

connecttodb($servername,$dbname,$dbuser,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}

$cat2=$_GET['cat'];

@$cat=$_GET['cat'];

@$town=$_GET['town'];

$quer2=mysql_query("SELECT DISTINCT name,cid FROM categories order by name"); 



if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT town FROM files_video where vcategs='0,$cat,0' and town >'' union 
SELECT DISTINCT town FROM files_image where vcategs='0,$cat,0' and town >'' order by town"); 
}else{$quer=mysql_query("SELECT DISTINCT town FROM files_video order by town"); } 


echo "<td align='left' valign='top'></td>
					<td class='nopad'>
					    <table cellpadding=1 cellspacing=0 border=0>
						<tr><td width='20%' align='left'>Ett land:</td><td>
<select class='ff'  name='cat' onchange=\"reload(this.form)\"><option value=''>V&auml;lj ett land</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cid']==@$cat){echo "<option selected value='$noticia2[cid]'>$noticia2[name]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cid]'>$noticia2[name]</option>";}
}
echo "</select></td></tr>";

$cat= $_POST['cat'];

echo "$cat2 cat2";

echo "<tr><td width='20%' align='left'>En stad:</td><td>
<select class='ff'  name='town' onchange=\"reload2(this.form)\"><option value='$cid'>V&auml;lj en stad</option>";
if ($cat2 !=''){
while($noticia = mysql_fetch_array($quer)) { 
if($noticia['town']==@$town){echo "<option selected value='$noticia[town]'>$noticia[town]</option>"."<BR>";}
else{echo  "<option value='$noticia[town]'>$noticia[town]</option>";}
}
}
echo "</select></td></tr>";
$town=$_POST['town'];

echo "$town town";
echo "<tr>
					<td width='20%' align='left'>Ny stad: :</td><td><input class='ff' type='text' name='town2'></td></tr>";

?>

Coolkille,

As this is a JavaScript question, can you post the server's response HTML rather than the php source.

Use "view source" in your browser?

That way people can paste it straight into a file, browse it and play around with the code.

Airshow

I think that your problem is with this code.

txt1=val;
txt2="&town=";
txt3=txt1+txt2;
txt4=txt3;

You have set the VAL var but then you have txt2 = "&town="; Then txt3 = txt1 + txt2. I think that you need to add the value of TOWN at the end. Therefore I would change txt2="&town=" + (the variable for town value);

Coolkille,

If you're still out there then try this. It is a much neater solution than building url search strings "manually". The browser does that for you automatically when an HTML form is submitted.

  1. Use this version of function reload :
    function reload(form, action){
    	form.action.value = action;
    	form.submit();
    }

    and delete function reload2 .

  2. Use the following form tag:
    <form method="post" action="image_upload.php">
  3. Add the following tag anywhere inside your form (immediately after the form tag is a good place):
    <input type="hidden" name="action" value="">
  4. Change your <select> tags as follows:
    <select class="ff" name="cat" onchange="reload(this.form, 'changeCat')">
    <select class="ff" name="town" onchange="reload(this.form, 'changeTown')">

    Now in image_upload.php you will pick up three values; $_POST['cat'], $_POST['town'], $_POST['action'] .
    The last one, $_POST, will allow your php to know which select menu was changed and to branch accordingly, if necessary.

You might also like to delete the spurious <BR> from where you build the second <select> menu in a php loop.

Airshow

This article has been dead for over six months. Start a new discussion instead.