简体   繁体   中英

Having problems getting a $_GET or $_POST to work with ajax

I have this code in my find.php file:

<select name="EmpName" required="required" id='empn'>
    <option value="" style="display:none;"></option>    
    <?php

    //get the employee id 
        $employeeId = $_POST['country'];

    //make connection to database, bail if no connection
    $connection = odbc_pconnect('db','','');
    if (!$connection) { exit("Connection Failed: " . $connection); }

    //retrieve usernames and passwords
    $sql = "SELECT EName FROM LoginTable WHERE EmployeeID='$employeeId'";

    $rs = odbc_exec($connection, $sql);

    while(odbc_fetch_row($rs)) {
        $rowJobNum = odbc_result($rs, 'EName');
        printf("<option value='%s'>%s</option>", $rowJobNum, $rowJobNum);
    }

?></select>

I have this code in my index.php file:

<script>
function getXMLHTTP() { //function to return the xml http object
        var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}



function getCity(strURL) {      

    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('citydiv').innerHTML=req.responseText;                      
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}
</script>




</head>

<body>

<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td  width="150"><select name="country" onChange="getCity('find.php?country='+this.value)">
    <option value="">Select Country</option>
    <option value="1">mg05</option>
    <option value="2">Canada</option>
        </select></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td ><div id="citydiv"><select name="city">
        </select></div></td>
  </tr>

</table>  
</form>
</body>
</html>

Here's the issue... I can't access the selection from the first drop down menu, the selection from the drop down menu is never going through to find.php . For reference, mg05 in the drop down list is an employee ID, I am working off an example here that was originally formatted for cities/states etc.

Here's my big issue, if I go to:

$sql = "SELECT EName FROM LoginTable WHERE EmployeeID = '$employeeId'";

and change it to

$sql = "SELECT EName FROM LoginTable WHERE EmployeeID= 'mg05'";

It works perfectly, and returns the correct result. So... obviously the post of data from index.php to find.php isn't working and I can't figure out why. I have $_GET and $_POST working in other areas of my site, but not this one.

Any insight as to where I may have gone wrong? I've tried using both get and post and nothing has worked.

The problem is that you are expecting the displayed text in the select to be post. This will not occur if the selected option has a value attribute, the value attribute will be used

<select name="country" onChange="getCity('find.php?country='+this.value)">
    <option value="">Select Country</option>
    <option value="mg05">mg05</option>
    <option value="Canada">Canada</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM