Jump to content

Use a selected drop down value in a second query, and then populate check boxes


jamey8420

Recommended Posts

I have a drop down that is being populated with a SQL query:

 

echo "<html>";
echo "<body class='body' onload='init();'>";
echo "<form name='form1' method='post'>";
echo "</br>";
echo "<table border=2 bordercolor=#5892AD align=center><tr><td>";
echo "<div style='text-align:left;'>";
echo "<div id='div_1' class='table-with-thin-border' style='margin:auto;width:800px;background-color:#FFFFFF;'>";
echo "<center><span class=f2><b>Rights Administration</b></span></center><br>";
echo "<form id='form2' name='user_rights' method='post' action='' onsubmit='' style='margin:auto;'>";
	echo "<div id='div_2' class='table-with-no-border'> ";
	echo "<span class=f3>";
	$user_res = mssql_query("SELECT UR.net_id, el.lname + ', ' + el.fname as name FROM cb_user_rights UR join employeelists el on ur.net_id = el.net_id order by name");
						echo "<a><b>User:</b>  </a><select name='user' value='-1' onchange='getUserId()'>User Name</option>";
		echo "<option value=-1>Select One</option>";
		while($row=mssql_fetch_array($user_res)){
			$net_id = $row['net_id'];
			$user = $row['name'];
			echo "<option value=".$net_id.">".$user."</option>";

		}

		echo "</select>";
echo "</span>";


	$rights_res = mssql_query("SELECT super_admin, rpt_admin, budg_admin FROM cb_user_rights WHERE net_id = '".$ValueFromDropDown."'");
	while ($row=mssql_fetch_array($rights_res)){
		$super_admin = $row['super_admin'];
		$rpt_admin = $row['rpt_admin'];
		$budg_admin = $row['budg_admin'];
	}


echo "<span class='f3'>  &nbsp &nbsp &nbsp &nbsp";
	echo "<input type='checkbox' name='cb0' value='".$super_admin."'>Super Admin";
	echo "</span>";
	echo "<span class='f3'>  &nbsp &nbsp &nbsp &nbsp";
	echo "<input type='checkbox' name='cb1' value='".$rpt_admin."'>Report";
	echo "</span>";
	echo "<span class='f3'>  &nbsp &nbsp &nbsp &nbsp";
	echo "<input type='checkbox' name='cb2' value='".$budg_admin."'>Budget";
	echo "</span>";
	echo "<span>      </span>";




echo "</br></br>";


echo "</form>";
echo "</body>";
echo "</html>";

 

What I am hoping to do is to use the selected value from the drop down to then run a second query, and to populate the values into the three checkboxes.  I've seen some examples of posting to new pages, but I want to use the same page is possible.

 

Any thoughts on how I can accomplish this?  I have also seen some examples incorporating AJAX, but I haven't been able to get that to work for me.  Maybe am I making this too difficult, and I'm missing an easier way to do this?  I thank you very much in advance.

 

Thanks,

jamey8420

Link to comment
Share on other sites

Funny you would ask that. I'm right now building a new addition to my weather widget and using ajax via the prototypejs.org prototype file. This uses onClick but its the same principle. The include is simply a select for all 50 states.

<select id="state" name="state">

<option value="al">Alabama</option>

...

</select>

 

 

the index.php file

<html>
<head>
	<script type="text/javascript" src="prototype.js"></script>
	<script>

		function sendRequest() {
			new Ajax.Request("parse.php",
				{
				method: 'post',
				postBody: 'state='+ $F('state'),
				onComplete: showResponse
				});
			}

		function showResponse(req){
			$('show').innerHTML= req.responseText;
		}

                  function sendCityRequest() {
			new Ajax.Request("parse.php",
				{
				method: 'post',
				postBody: 'city='+ $F('city'),
				onComplete: showCityResponse
				});
			}

		function showCityResponse(req){
			$('show').innerHTML= req.responseText;
		}

	</script>
</head>

<body>
	<form id="test" onsubmit="return false;">
		<?php include("./states_select.txt"); ?>
		<input type="submit" value="state" onClick="sendRequest()">
	</form>

	<div id="show"></div>
</body>

</html>

 

the parse.php file

<?php
if(isset($_POST['state']))
{
  $st = $_POST['state'];
  $dir="./zips/$st/";
  $cities = scandir($dir);
echo "<select id=\"city\" name=\"city\">\n";
  	foreach($cities as $city)
  	{
  		if ($city != '.' && $city != '..')
	{
		//$Dcity=$city;
		$Dcity = str_replace("_"," ",$city);
		$Dcity=ucwords($Dcity);
		echo "<option value=\"zips/$st/$city\">$Dcity</option>\n";

	}	}
echo '</select><input type="submit" value="city" onClick="sendCityRequest()">';

}
if(isset($_POST['city']))
{
$splitup=$_POST['city'];
list($zips,$state,$city)=explode("/",$splitup);

$cities = scandir("$zips/$state/");
echo "<select id=\"city\" name=\"city\">\n";
  	foreach($cities as $ci)
  	{
  		if ($ci != '.' && $ci != '..')
	{
		//$Dcity=$city;
		$Dcity = str_replace("_"," ",$ci);
		$Dcity=ucwords($Dcity);
		echo "<option value=\"zips/$state/$ci\">$Dcity</option>\n";

	}	}
echo '</select><input type="submit" value="city" onClick="sendCityRequest()">';
//echo "<br>::$zips::$state::$city::";
$city_data=file_get_contents("$zips/$state/$city/info.txt");
list($town,$county,$long,$latitude)=explode("|",$city_data);
$longitude = ($long > 0) ? $long * -1 : $long;
echo "<table><tr>";
echo "<td><b>City</b></td><td><b>County</b></td><td><b>Latitude</b></td><td><b>Longitude</b></td></tr>";
echo "<tr><td>$town</td><td>$county</td><td>$latitude</td><td>$longitude</td></tr>";
echo "<tr><td colspan=4>";
$time=date("U");

$sun_info = date_sun_info(strtotime(date("Y-m-d")), $latitude, $longitude);
foreach ($sun_info as $key => $val) {
     $str = str_replace("_"," ",$key);
		$str=ucwords($str);
    echo "<b>$str:</b> " . date("h:i A", $val) . "<br>\n";
}
echo "</td></tr><table>";
}
?>

 

I have not included the state/city/info.txt as from this you can get a feel for how it works and what it takes to return to the same page multiple times and hold the same place. When you are building I suggest putting initial output, like the simple return from the form widgets directly to the screen to make sure you are getting what you think you should as only the initial page shows with view source. But if you use firefox and the web developer plugin you can use the form widget to view form values and it will show the JS returns.

 

Feel free to modify and use as you wish.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.