Jump to content

Using Switch with a GET Request


agr8lemon

Recommended Posts

Hello everyone.

 

I have a page with 3 separate drop down choices.

 

Search SHEC Departments

Search SJCF Departments

Search SHEC Employees

 

I'd like to show the results of the search on the same page. I'm trying to determine what SQL query to run based on what SELECT button they push.

 

I'm having a issue trying to use a switch with $_GET. Here is an example of what I have.

 

Sorry if the code is sloppy, I've been trying a bunch of things and have not organized it yet.

 

<?php
  require"../includes/dept_mssql.php";  //Connect to the Department database
  require"../includes/emp_mssql.php";   //Conect to the Employee database
//show all errors
error_reporting(E_ALL);
ini_set('display_errors', '3') 

?>

<html>
<head>
</head>
<body>
<div align="center">
<h1> Hospital Phone Directory </h1>
<hr />
</div>
<div align=center>
<h2>
SHEC Department Search
</h2>
<form name=shec_depart action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<?php
$sql_depart_shec="SELECT Name, Depart_ID, Facility FROM dbo.Depart_tbl WHERE Facility = 'SHEC' OR Facility = 'BOTH' ORDER BY Name ASC";
$shecResult=odbc_exec($dept_conn, $sql_depart_shec);
$shecOptions="";

while($shecRows=odbc_fetch_array($shecResult)){
$shecId=$shecRows["Depart_ID"];
$shecDepartments=$shecRows['Name'];
$shecOptions.="<OPTION VAULE=\"$shecId\">".$shecDepartments;
} 
?>
<select name="shec-depart">
<OPTION VALUE=>SHEC Departments<?php echo $shecOptions ?>
</SELECT> 
<input type="submit" name="Submit" value="Search" />
<input type="hidden" name="submitted" id="submitted" value="true" />

</form>
</div>


<div align=center>
<h2>SJCF Department Search</h2>
<form name=sjcf_depart action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<?php
$sql_depart_sjcf="SELECT Name, Depart_ID, Facility FROM dbo.Depart_tbl WHERE Facility = 'SJCF' OR Facility = 'BOTH' ORDER BY Name ASC";
$sjcfResult=odbc_exec($dept_conn, $sql_depart_sjcf);
$sjcfOptions="";

while($sjcfRows=odbc_fetch_array($sjcfResult)){
$sjcfId=$sjcfRows["Depart_ID"];
$sjcfDepartments=$sjcfRows['Name'];
$sjcfOptions.="<OPTION VAULE=\"$sjcfId\">".$sjcfDepartments;
} 
?>
<select name="sjcf-dept" STYLE="width: 275px">
<OPTION VALUE=>SJCF Departments<?php echo $sjcfOptions ?>
</SELECT>
<input type="submit" name="Submit" value="Search" />
<input type="hidden" name="submitted" id="submitted" value="true" />
</form> 
</div>


<div align=center>
<h2> SHEC Employee Search</h2>
<form name=shec_emp action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">


<?php

$sql_emp_shec="SELECT * FROM dbo.Emp_tbl WHERE Facility = 'SHEC' OR Facility = 'BOTH' ORDER BY FirstName ASC";
$result=odbc_exec($emp_conn, $sql_emp_shec);

?>

<select name="shec-name" STYLE="width:  275px">
<OPTION VALUE=A>A</option>
<OPTION VALUE=B>B</option>

</SELECT> 
<input type="submit" name="Submit" value="Search" />
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>



<?php
if($_GET['submitted'] == true)
{
switch ($_SERVER['QUERY_STRING'])
{
case "shec-dept";
  echo "do some query";
  break;
case "sjcf-dept";
  echo "Do another query";
  break;
case "shec-name";
  echo "Do yet a different query";
  break;
defalut;
  echo "error";
}
}
else
{
echo 'Get is not set';
}
odbc_close($dept_conn); 
odbc_close($emp_conn);
?> 

 

At the moment the page displays, however making a selection from the dropdowns and hitting the submit button does not display any of the switch echos.

 

 

Any help would be wonderful!! Thank you!

 

 

Link to comment
Share on other sites

At the moment the page displays, however making a selection from the dropdowns and hitting the submit button does not display any of the switch echos.

 

The syntax for switch-case statements in php requires a colon rather than a semicolon

 

case "shec-dept":
  echo "do some query";
  break;

Link to comment
Share on other sites

The $_SERVER['QUERY_STRING'] is going to contain the entire query string:

?shec-name=A&Submit=Search&submitted=true

so you are not going to be able to use it in the switch like you have it.  You may want to change each of your "hidden" fields (named "submitted") to return a different value.  For instance, the last one could be:

<input type="hidden" name="submitted" id="submitted" value="shec-name" />

 

Then use that in the switch:

switch ($_GET['submitted']) {

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.