Results 1 to 2 of 2

Thread: Using Switch with a GET Request

  1. #1
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using Switch with a GET Request

    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 Code:
    <?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 them 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!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    1. "default" is spelled wrong. Fix that.
    2. After 'case' and 'default' statements, you use a colon ( : ), not semi-colon ( ; ).
    Example: case "shec-dept":

    There might be more problems, but start there.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •