Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing values from php generated drop-down menus

Status
Not open for further replies.

atsea

Technical User
Feb 27, 2005
51
JP
I recently hit a little snag...maybe someone here can advise me on the best way to accomplish this:

A little background...
So far my program generates a table and two drop down menus (ddm) containing values acquired from a mySQL database.
In order to maintain .html extension on my webpage I load this (.php) file in an <iframe>.

What I would like to happen...
1 - Everytime the user changes the value in the drop down menu that value is passed (variable) to the query that generates the table.

2 - the table should automatically refresh. In the same window. (not sure if I should have the table in a separate <iframe>)

My main issue right now is I can't seem to get the variables to pass from the function when a selection is made.

here is some of the code to give you an idea:

Code:
error_reporting(E_ALL); 

//These variables will determine the search parameters
$year = $_POST['year'];
$genre = $_POST['genre'];

include 'db_connect.php';


ddm_genre();
ddm_year();


//not sure if I should make this a function as well it may be easier to pass the varible

//after the connection is made use the INSERT command to enter the values in the db
//$sql_select = "SELECT ID, Genre, Title, Director, Year FROM movies";
$sql_select = "SELECT Genre, Title, Director, Year FROM movies ORDER BY Genre";

//result set
$rs = mysql_query($sql_select);

//creating the table /w headers

	 echo "<html><body>";
	 echo "<table border='1' cellspacing='0'><tr><td>Genre</td><td>Title</td><td>Director</td><td>Year</td></tr>";
	
	
	//row for each record
	 while ($row = mysql_fetch_array($rs)) {
                 echo "<tr><td>" . $row['Genre'] . "</td><td>" . $row['Title'] . "</td><td>" . $row['Director'] . "</td><td>" . $row['Year'] . "</td></tr>";
           }  
	
	 echo "</table>";
	 echo "</body></html>";


//free memory
mysql_free_result($rs);

//close the db
mysql_close();





//functions


function ddm_genre(){

//query to SELECT the dd-menu option values
$select = "SELECT DISTINCT Genre FROM movies ORDER BY Genre";

//Result Set
$rs_genre = mysql_query($select) or die(mysql_error());

//If no rows are returned display default error message
if (!mysql_num_rows($rs_genre)) {
	die(mysql_error());
} 

//Else make drop down box
else {

	echo "<form name='genre' action='db_select.php' method='post'>";
	echo "<label>Genre: ";
	echo "<select name='genre' id='genre'>";


	//Each row in the rs will be an option
	while ($row = mysql_fetch_array($rs_genre)) 
		{
			echo "<option value='".$row['Genre']."'>".$row['Genre']."</option>";
		}
}

echo "</label>";
echo "</select>";
echo "</form>";

//free memory
mysql_free_result($rs_genre);

}



function ddm_year(){

//query to SELECT the dd-menu option values
$select = "SELECT DISTINCT Year FROM movies ORDER BY Year";

//Result Set
$rs_year = mysql_query($select) or die(mysql_error());

//If no rows are returned display default error message
if (!mysql_num_rows($rs_year)) {
	die(mysql_error());
} 

//Else make drop down box
else {

	echo "<form name='year' action='db_select.php' method='post'>";
	echo "<label>Year: ";
	echo "<select name='year' id='year'>";


	//Each row in the rs will be an option
	while ($row = mysql_fetch_array($rs_year)) 
		{
			echo "<option value='".$row['Year']."'>".$row['Year']."</option>";
		}
}

echo "</label>";
echo "</select>";

//free memory
mysql_free_result($rs_year);

}

Any suggestions would be greatly appreciated.

Thanks,

Aaron
 

I get the impression the PHP side of things is working fine. In which case, this is a question for the Javascript forum.

I believe you will want to put an onchange into both the <select> drop downs that triggers the form to submit every time you change the drop down using something like...

Code:
<select name="year" id="year" onchange="this.form.submit()">...</select>

Cheers,
Jeff

 
As far as html extensions, this can be fixed with htaccess!
I have put out examples on this forum before, but I dont have time to go in depth about it now.. Maybe you can search for it?

Also:
PHP is server-side, and therefore can not do anything on client-side, as automatic-refresh when selecting things, etc.

Ask this question on the javascript forum, about onchange property. However: be aware that not everyone can take advantage of javascript functions, as not everyone has them enabled,for different reasons.

Olav Alexander Mjelde
Admin & Webmaster
 

DaButcher...

Just a quick question about what you propose above (since you are putting your hand up as someone with knowledge on .htaccess). Can you do this using .htaccess if you are running the web server under the Windows OS?

I agree with your suggestion for mapping .html as .php, but thought that this was not possible if the web server was running under Windows (using .htaccess).

Maybe I missed something. If so... cool... teach me more!

Jeff

 
.htaccess is for apache web servers so it will not work on IIS (windows).
 

MattNeeley said:
.htaccess is for apache web servers so it will not work on IIS (windows).

I had to take a break to stop the laughing when I read this. Let me take it slower for you young Matt...

IIS is not Windows. IIS runs as a web server on windows.
Apache is a web server. Apache runs on windows. Would you consider Apache to be Windows?

Since I can run Apache on Windows then using your crazy logic, .htaccess should work on Windows.

Look like the solution is to use javascript anyway.

Cheers,
Jeff
 
Thanks for all the replies...

For my example above I had no intention of using .htaccess nor did I want to start messing around with JS.

(however, DaButcher, you did give some new ideas regarding .htaccess.)

I was able to find a solution, and it was really quite simple.

to answer the first part of my question I added:
action='".$_SERVER['PHP_SELF']."' (in the <form>)

the second issue is resolved by adding:
onChange='submit();' (in the <select>) and having the .php file refresh itself in the .html <iframe> (as intended, keeping everything server side)

Again, thanks for help.

atsea
 
Just so you know...

If you want to pass the value itself and perform some
processing on the client side before going up to the
server, you could use something similiar to:

onchange="SomeFunction(options(parseInt(value)).text)"

Darrell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top