How to use $_post and switch() on target page to return database data

I have two select drop-downs and a button in a form. One select is a state chooser, the other is a “carrier” chooser (which is populated using PHP). When a user clicks the “display” button, after a state and carrier are selected it should redirect them to the target “company.php”.

WHAT I NEED:
Here’s the tricky part, “company.php” needs to be able to identify which state was chosen in order to grab its respective carrier and display the table data. Each “carrier” across all 4 states has a unique ID, so “company.php” can be EX:/company.php?ID=””. Please Help!!

HERE IS THE PHP AT THE START OF THE PAGE:

<?
include($_SERVER['DOCUMENT_ROOT']."/include/template2014.php");
include($_SERVER['DOCUMENT_ROOT']."/include/all_include.php");
include($_SERVER['DOCUMENT_ROOT']."/GIA/cps/include.php");

//For Connecticut
$sqlCT = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='CT' and `show`='Y' order by Name");
//For New York
$sql = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='NY' and `show`='Y' order by Name");
//For New Jersey
$sqlNJ = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='NJ' and `show`='Y' order by Name");
//For New Hampshire
$sqlNH = mysql_query("SELECT * FROM `gia_survey_allyears_company` WHERE `State`='NH' and `show`='Y' order by Name");

$row = mysql_fetch_assoc($sql);

//For New York
do {
    $company_arry[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sql));

//For New Jersey
do {
    $company_arryNJ[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sqlNJ));

//For New Jersey
do {
    $company_arryNH[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sqlNH));

//For Connecticut
do {
    $company_arryCT[]=array('Name'=>$row['Name'],'Id'=>$row['ID']);
} while ($row = mysql_fetch_assoc($sqlCT));

$total = count($company_arry);
$col1 = round($total/2);
$col2 = $total-1;
?>

HERE IS THE FORM:


        <br />
        <br />
        <br />

Select a carrier - Conn " . $company['Name'] . ""; } echo $options; ?>
Select a carrier - NH " . $company['Name'] . ""; } echo $options; ?>
Select a carrier - NJ " . $company['Name'] . ""; } echo $options; ?>
Select a carrier - NY " . $company['Name'] . ""; } echo $options; ?>
</form>

HERE IS THE PHP INCLUDE FILE:

<?
$sql = mysql_query("SELECT gia_survey_allyears_company.Name, gia_survey_allyears_matrix.* FROM `gia_survey_allyears_matrix` 
                    left join gia_survey_allyears_company on gia_survey_allyears_matrix.CompanyID = gia_survey_allyears_company.ID
                    WHERE gia_survey_allyears_matrix.CompanyID=".mysql_real_escape_string(trim($_GET['id']))."");

$row = mysql_fetch_assoc($sql);

$qu_arry = array(   'Q1'=>'N/A','Q2'=>'N/A','Q3'=>'N/A','Q4'=>'N/A',
                    'Q5'=>'N/A','Q6'=>'N/A','Q7'=>'N/A','Q8'=>'N/A',
                    'Q9'=>'N/A','Q10'=>'N/A','Q11'=>'N/A','Q12'=>'N/A',
                    'Q13'=>'N/A','Q14'=>'N/A','Q15'=>'N/A','Q16'=>'N/A');

$quavg_arry = array(    'Q1'=>'N/A','Q2'=>'N/A','Q3'=>'N/A','Q4'=>'N/A',
                    'Q5'=>'N/A','Q6'=>'N/A','Q7'=>'N/A','Q8'=>'N/A',
                    'Q9'=>'N/A','Q10'=>'N/A','Q11'=>'N/A','Q12'=>'N/A',
                    'Q13'=>'N/A','Q14'=>'N/A','Q15'=>'N/A','Q16'=>'N/A');

$holdarry = array(  'Agents'=>'N/A','RankOutOf'=>'N/A','Rank'=>'N/A','Score'=>'N/A',
                    'Avg'=>'N/A','AvgAllScore'=>'N/A','AvgAllQuestion'=>'N/A',
                    'ComAvg'=>'N/A','Q1-16'=>$qu_arry,'Q1-16Avg'=>$quavg_arry);

$company['2003'] = $holdarry;
$company['2004'] = $holdarry;
$company['2005'] = $holdarry;
$company['2006'] = $holdarry;
$company['2007'] = $holdarry;
$company['2008'] = $holdarry;
$company['Name']= $row['Name'];
$company['State']= $row['State'];

do {
// Show Scores for question
$qu_arry = array(   'Q1'=>round($row['Q2-0'],1),'Q2'=>round($row['Q2-1'],1),'Q3'=>round($row['Q3-0'],1),'Q4'=>round($row['Q3-1'],1),
                    'Q5'=>round($row['Q3-2'],1),'Q6'=>round($row['Q4-0'],1),'Q7'=>round($row['Q4-1'],1),'Q8'=>round($row['Q5-0'],1),
                    'Q9'=>round($row['Q5-1'],1),'Q10'=>round($row['Q5-2'],1),'Q11'=>round($row['Q6-0'],1),'Q12'=>round($row['Q6-1'],1),
                    'Q13'=>round($row['Q7-0'],1),'Q14'=>round($row['Q7-1'],1),'Q15'=>round($row['Q8-0'],1),'Q16'=>round($row['Q8-1'],1));

    $company[$row['Year']]= array(  'Agents'=>$row['total'],
                                    'RankOutOf'=>'',
                                    'Rank'=>$row['Rank'],
                                    'Score'=>round($row['QAvg'],1),
                                    'Q1-16'=>$qu_arry,
                                    'Q1-16Avg'=>'');

    $sqlStateYear = mysql_query("SELECT * FROM `gia_survey_allyears_matrix` WHERE CompanyID=0 and `State`='".$row['State']."' and `Year`='".$row['Year']."'");
    $rowStateYear = mysql_fetch_assoc($sqlStateYear);

    do {

$company[$row['Year']]['Q1-16Avg'] = 
              array('Q1'=>round($rowStateYear['Q2-0'],1),'Q2'=>round($rowStateYear['Q2-1'],1),
                    'Q3'=>round($rowStateYear['Q3-0'],1),'Q4'=>round($rowStateYear['Q3-1'],1),
                    'Q5'=>round($rowStateYear['Q3-2'],1),'Q6'=>round($rowStateYear['Q4-0'],1),
                    'Q7'=>round($rowStateYear['Q4-1'],1),'Q8'=>round($rowStateYear['Q5-0'],1),
                    'Q9'=>round($rowStateYear['Q5-1'],1),'Q10'=>round($rowStateYear['Q5-2'],1),
                    'Q11'=>round($rowStateYear['Q6-0'],1),'Q12'=>round($rowStateYear['Q6-1'],1),
                    'Q13'=>round($rowStateYear['Q7-0'],1),'Q14'=>round($rowStateYear['Q7-1'],1),
                    'Q15'=>round($rowStateYear['Q8-0'],1),'Q16'=>round($rowStateYear['Q8-1'],1));
$company[$row['Year']]['AvgAllScore'] = round($rowStateYear['QAvg'],1);
$company[$row['Year']]['RankOutOf'] = $rowStateYear['total'];
    } while ($rowStateYear = mysql_fetch_assoc($sqlStateYear));

// END
} while ($row = mysql_fetch_assoc($sql));
function fnRake($rake) {
    if ($rake==11 || $rake==12 || $rake==13) {
        $i=$rake;
    }else {
        $i = substr($rake,strlen($rake)-1,1);
    }
    switch ($i) {
        case 1:
            $add =  "st";
        break;
        case 2:
            $add =  "nd";
        break;
        case 3:
            $add =  "rd";
        break;
        case 11:
            $add =  "th";
        break;
        case 12:
            $add =  "th";
        break;
        case 13:
            $add =  "th";
        break;
        default:
            $add =  "th";
        break;
    }
    if ($rake=="N/A") {
        return $rake;
    }else{
        return $rake."<sup>$add</sup>";
    }
}
?>

JS AND JQUERY IF NEEDED:

<script>
////// DROPDOWN FUNCTIONALITY ///////
function optionCheck() {
    var i, len, optionVal, helpDiv,
        selectOptions = document.getElementById("state_select");
        carrOptions = document.getElementById('carrier_select1');

    // loop through the options in case there
    // are multiple selected values
    for (i = 0, len = selectOptions.options.length; i < len; i++) {

        // get the selected option value
        optionVal = selectOptions.options[i].value;

        // find the corresponding help div
        helpDiv = document.getElementById("carrier_select" + optionVal);
        displayButton = document.getElementById('display-btn');
        //selectDiv = document.getElementsByClassName("select-div");

        // move on if we didn't find one
        if (!helpDiv) { continue; }

        // set CSS classes to show/hide help div
        if (selectOptions.options[i].selected) {
            helpDiv.className = "conn_select nh_select nj_select ny_select";
            $(helpDiv).addClass("dropdown-box");
        } else {
            //Hide carrier select on page load
            helpDiv.className = "carr-hide";
            //Hide display button when changing states
            $('#display').hide();
        }
    }    
}

// alternative method of binding the onchange handler
document.getElementById("state_select").onchange = optionCheck;

//Remove emtpy 'options' generated by PHP
$('[class=carr-hide] option').filter(function(){
    return this.value == ''
}).remove();

//Show display button when acrrier is selected
document.getElementById('carrier_select1').addEventListener('change', function () {
    var style = this.value > 0 ? 'block' : 'none';
    document.getElementById('display').style.display = style;
});

document.getElementById('carrier_select2').addEventListener('change', function () {
    var style = this.value > 0 ? 'block' : 'none';
    document.getElementById('display').style.display = style;
});

document.getElementById('carrier_select3').addEventListener('change', function () {
    var style = this.value > 0 ? 'block' : 'none';
    document.getElementById('display').style.display = style;
});

document.getElementById('carrier_select4').addEventListener('change', function () {
    var style = this.value > 0 ? 'block' : 'none';
    document.getElementById('display').style.display = style;
});
</script
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s