agr8lemon Posted April 8, 2010 Share Posted April 8, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/198015-using-switch-with-a-get-request/ Share on other sites More sharing options...
leehanken Posted April 8, 2010 Share Posted April 8, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/198015-using-switch-with-a-get-request/#findComment-1039049 Share on other sites More sharing options...
agr8lemon Posted April 8, 2010 Author Share Posted April 8, 2010 case "shec-dept": echo "do some query"; break; Thank you, I guess I got a little semi-colon happy That did not fix the issue however. Quote Link to comment https://forums.phpfreaks.com/topic/198015-using-switch-with-a-get-request/#findComment-1039051 Share on other sites More sharing options...
DavidAM Posted April 8, 2010 Share Posted April 8, 2010 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']) { Quote Link to comment https://forums.phpfreaks.com/topic/198015-using-switch-with-a-get-request/#findComment-1039067 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.