0
votes

What am I doing wrong? I'm a newbie and my code isn't pulling the cities from the mysql database.

function getXMLHTTP() { //fuction 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(state) {       
    
var strURL="get_city.php?state="+state;
var req = getXMLHTTP();
    
if (req) {
    req.onreadystatechange = function() {
    if (req.readyState == 4 && req.status==200) {
        document.getElementById("citydiv").innerHTML=req.responseText;
            }
        }
    req.open("GET","get_city.php?state="+stateL,true);
    req.send(null);
}
}
<div class="row">
        <span class="small-6 columns" style="inline">
            <label>Location</label>
            <select name="state" method="GET" id="state"     onChange="getCity(this.value)">
                        <option value="0">Select State</option>
                        <option value=1>Alabama</option>
                        <option value=2>Alaska</option>
                        <option value="3">Arizona</option>
                        <option value=AR>Arkansas</option>
                        <option value=CA>California</option>

                        <option value="CO">Colorado</option>
                        <option value="CT">Connecticut</option>
                        <option value="DE">Delaware</option>
                        <option value="DC">District of Columbia</option>
                        <option value="FL">Florida</option>

                        <option value="GA">Georgia</option>
                        <option value="HI">Hawaii</option>
                        <option value="ID">Idaho</option>
                        <option value="IL">Illinois</option>
                        <option value="IN">Indiana</option>

                        <option value="IA">Iowa</option>
                        <option value="KS">Kansas</option>
                        <option value="KY">Kentucky</option>
                        <option value="LA">Louisiana</option>
                        <option value="ME">Maine</option>

                        <option value="MD">Maryland</option>
                        <option value="MA">Massachusetts</option>
                        <option value="MI">Michigan</option>
                        <option value="MN">Minnesota</option>
                        <option value="MS">Mississippi</option>

                        <option value="MO">Missouri</option>
                        <option value="MT">Montana</option>
                        <option value="NE">Nebraska</option>
                        <option value="NV">Nevada</option>
                        <option value="NH">New Hampshire</option>

                        <option value="NJ">New Jersey</option>
                        <option value="NM">New Mexico</option>
                        <option value="NY">New York</option>
                        <option value="NC">North Carolina</option>
                        <option value="ND">North Dakota</option>

                        <option value="OH">Ohio</option>
                        <option value="OK">Oklahoma</option>
                        <option value="OR">Oregon</option>
                        <option value="PA">Pennsylvania</option>
                        <option value="RI">Rhode Island</option>

                        <option value="SC">South Carolina</option>
                        <option value="SD">South Dakota</option>
                        <option value="TN">Tennessee</option>
                        <option value="TX">Texas</option>
                        <option value="UT">Utah</option>

                        <option value="VT">Vermont</option>
                        <option value="VA">Virginia</option>
                        <option value="WA">Washington</option>
                        <option value="WV">West Virginia</option>
                        <option value="WI">Wisconsin</option>

                        <option value="WY">Wyoming</option>
                </select></span>

        <span class="small-6 columns" style="inline">
            <select name="city" id="citydiv" >
                    <option value="">Select City</option>
        </select></span>
        </div>

My php file get_city:

<?php

$state_selected=strval($_GET['state']);
echo $state_selected;

$link=mysqli_connect("127.0.0.1","root","cw","locations");
if(mysqli_connect_errno()) {
    printf("connect failed: %s\n",mysqli_connect_error());
    exit();
}

$query="select city from cities where state_code= '".$state_selected."'";

$rs=mysqli_query($link,$query);
?>

<select name="city" id="citydiv" onchange="populate_utility_menu(this.value)">

<?php

while($row=mysqli_fetch_array($rs)) {
    echo "<option value=>".$row['city'];    
    echo "</option>";
}
?>
</select>

I realize some of my option values are different then the two char string I want to pass but I was just checking if that was the problem. I'm not even sure how to research more into whats going on besides the google inspector and the apache2 error.log file which is below and looks like maybe php isn't loading a mysql file but I'm clueless how to check or direct it to the correct path. Please help and remember I am very new at this.

apache2 error.log

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20121212/msql.so' - /usr/lib/php5/20121212/msql.so: cannot open shared object file: No such file or directory in Unknown on line 0

[Tue Jun 14 17:48:47.600742 2016] [mpm_prefork:notice] [pid 13981] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.14 configured -- resuming normal operations

[Tue Jun 14 17:48:47.600928 2016] [core:notice] [pid 13981] AH00094: Command line: '/usr/sbin/apache2'

1
Looks like you're missing a library - user2182349
You are going to be open to SQL injections when you get that query working. Use parameterized queries. Also the option you are going to generate is going to be invalid, there will be no value. - chris85
I was trying to generate a 2 char string for the state code in my database. Could you please elaborate as to why my option values are incorrect? And by sql injections, you mean people can complete the form with malicious input to corrupt the database? How do I prevent this, by just adding input authenication (which was my next step after I got this other stuff working)? - user3499087

1 Answers

0
votes

Did you upgrade your PHP version? It could be that your configuration files are still pointing to old location paths. Try this:

sudo apt-get install php5-mcrypt
sudo apt-get install php5-mysql