...

View Full Version : Muliple Select Boxes



CoolAsCarlito
10-07-2008, 12:10 AM
One a user selects an option out of the first select box I want it to take that type back to the database and match it with all the show names with that type and then place all show names in that second select box. How is this done? And is my coding right so far?


<?php

/* setupshow.php */

/* This form after submission takes the results of the form and makes a new show ready for adding matches. */

require ('database.php');

echo '<form action="setupshow.php" method="post">';

echo '<fieldset>';
echo '<legend>Enter the following information to setup a show:</legend>';
echo '<p>Weekly Show or Pay-Per View:<select name="type"><option value="">Select a Show Type</option>';
$query = 'SELECT type FROM shows';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
echo "<option value=\"{$row['type']}\">{$row['type']}</option>\r";
}
echo '</select></p>';
echo '<p>Show Name:<select name="showname"><option value="">Select a Show Name</option>';
$query2 = 'SELECT showname FROM shows';
$result2 = mysql_query($query2);
while ($row = mysql_fetch_assoc($result)){
echo "<option value=\"{$row['showname']}\">{$row['showname']}</option>\r";
}
echo '</select></p>';
echo '<p>Show Label:<input name="showlabel" type="text" readonly="true" size="5"></p>';
echo '<p>Location:<input name="location" type="text"></p>';
echo '<p>Arena:<input name="arena" type="text"></p>';
echo '<div align="center"><input name="submit" type="submit"><input name="sumbitted" type="hidden" value="TRUE"></div>';
echo '</fieldset>';

echo '</form>';

?>

ohgod
10-07-2008, 03:50 PM
in your first dropdown you'll want an onChange event to fire an ajax request to take the result from the first dropdown and query for the second, returning with it.


i don't see anything in your code to even start with that.

CoolAsCarlito
10-07-2008, 08:59 PM
What do you mean you dn't see anything in my code to even start with that?


in your first dropdown you'll want an onChange event to fire an ajax request to take the result from the first dropdown and query for the second, returning with it.


i don't see anything in your code to even start with that.

ohgod
10-07-2008, 09:08 PM
show me an onChange in that code or the base for an ajax request


unless somehow i've misinterpreted what you're actually looking for i'm saying all you have right there is a php page.

CoolAsCarlito
10-07-2008, 09:24 PM
Oh I"m sorry I don't have it on there I didn't know what I needed to do to my page to achieve the results I wanted.


show me an onChange in that code or the base for an ajax request


unless somehow i've misinterpreted what you're actually looking for i'm saying all you have right there is a php page.

CoolAsCarlito
10-08-2008, 01:01 AM
Okay so I've done some modification. Can you tell me why its not loading the show names.

This is my updated php script.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
try
{
if (window.XMLHttpRequest) // firefox
reqsend = new XMLHttpRequest(); // create object for ff and other browsers
else // IE 5.0+
reqsend = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){};

function getNames(typed)
{
var created = "option";
var i = 0;
try
{
reqsend.open("POST", "getNames.php", true);
var stuff = "type=" + typed;
reqsend.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // need for POST
reqsend.setRequestHeader("Content-Length", stuff.length);
reqsend.send(stuff); // actual send

reqsend.onreadystatechange = function()
{
if (reqsend.readyState == 4 && reqsend.status == 200)
{
var name = reqsend.responseXML.getElementsByTagName("name");
for(i=0; i<name.length; i++)
{
var opts = document.getElementById('names');
var newopt = document.createElement(created);
opts.appendChild(newopt);
newopt.innerHTML = name[i].childNodes[0].nodeValue;
}
}
};
}
catch(e){};
}
</script>
</head>

<body>
<?php

/* setupshow.php */

/* This form after submission takes the results of the form and makes a new show ready for adding matches. */

require ('database.php');

echo '<form action="setupshow.php" method="post">';

echo '<fieldset>';
echo '<legend>Enter the following information to setup a show:</legend>';
echo '<p>Weekly Show or Pay-Per View:<select name="type"><option value="">Select a Show Type</option>';
$query = 'SELECT type FROM shows';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
echo "<option value=\"{$row['type']}\">{$row['type']}</option>\r";
}
echo '</select></p>';
echo "<select id=\"names\"</select>";
echo '<p>Show Label:<input name="showlabel" type="text" readonly="true" size="5"></p>';
echo '<p>Location:<input name="location" type="text"></p>';
echo '<p>Arena:<input name="arena" type="text"></p>';
echo '<div align="center"><input name="submit" type="submit" value="Submit"><input name="sumbitted" type="hidden" value="TRUE"></div>';
echo '</fieldset>';

echo '</form>';

?>
</body>
</html>


This is the getNames.php script.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
if(isset($_POST['type']) && !empty($_POST['type']))
{
require ('database.php');

$type = mysql_real_escape_string($_POST['type']);

$sql = "SELECT * FROM shows WHERE type='$type'";
$result = mysql_query($sql);


header("Content-Type: text/xml");
echo "<all>";
while( $data = mysql_fetch_assoc($result))
{
echo $data['showname'];
}
echo "</all>";

mysql_close();
}
?>
</body>
</html>

ohgod
10-08-2008, 04:31 PM
let's back up a bit and look at the flow you'll want.

you're going to end up with 2 pages, a display php page and a processor php page. on the display page your dropdowns and all other information will live. on the proc page there will be nothing but a database call and an echo or return of some sort.

so, your first dropdown will have a javascript onchange event in it. it will call your ajax function on your display page. upon it being called you should basically set it up to pull the selected value and send it to the proc page. the proc page will take that value, run a query based on that condition, build the contents of the next dropdown and send it back. the ajax function on your display page will catch what the proc sends. from there it's a matter of inserting that code into your page with js.

it sounds like you need to get a better feel for the flow of ajax before trying to code it. i'm not trying to sound condescending, lord knows i'm no pro. heck, as often as not i go the lazy route and use a framework because it has updater functions pre built.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum