Help me figure out what is wrong

OK, I have downloaded a couple of scripts that should, when working correctly, populate a second dropdown based on what the first choice is. These work using the OLD mysql stuff but now I am trying to update it for PDO. The PDO connect routine works since I use it on other projects.

So, here is the INDEX.php file that populates the first dropdown, the javascript ajax routine should call findcity.php and populate the city dropdown based on the country (first) dropdown.

Roshan's Ajax dropdown code
<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>
<script>
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(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('findcity.php?country='+this.value)">
    <option value="">Select Country</option>
    <option value="1">USA</option>
    <option value="2">Canada</option>
        </select></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td ><div id="citydiv"><select name="city">
    <option>Select City</option>
        </select></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

and now for the findcity.php code:

<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>

<? $country=$_REQUEST['country'];

    include_once(getRootPath(). 'inc/db_connect_PDO2.php');
    
    $db = get_db_connection();
    
    $result = $db->prepare('SELECT * FROM city WHERE contryid = :country');
    $result = $db->bindParam(':country',$country);
    $result->execute();
    
// $link = mysql_connect('localhost', 'sacccco1_dbuser', 'dbuser'); //changet the configuration in required
// if (!$link) {
    // die('Could not connect: ' . mysql_error());
// }
// mysql_select_db('sacccco1_MainDB');
// $query="select city from city where countryid=$country";
// $result=mysql_query($query);
print_r($result->fetchALL());

?>
<select name="city">
<option>Select City</option>
// <? while($row=mysql_fetch_array($result)) { ?>
<? while($row = $result->fetch(PDO::FETCH_NUM)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
<?php
// returns the relative path from current folder to Web Site Root directory
function getRootPath() {
  $current_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
  $current_host = pathinfo($_SERVER['REMOTE_ADDR'], PATHINFO_BASENAME);
  $the_depth = substr_count( $current_path , '/');

  // Set path to root for includes to access from anywhere
  if($current_host == '127.0.0.1') $pathtoroot = str_repeat('../' , $the_depth-1);
  else $pathtoroot = str_repeat ('../' , $the_depth);

  return $pathtoroot;
}
?>

Please let me know what I did wrong.

E

Define ‘what i did wrong’. Is the script producing an error? Is it doing something it shouldnt? Not doing something it should? Can’t diagnose a problem unless you tell us what it is.

Sorry, the script is producing the error from the ajax part that says “Internal Server Error”. I can see that the index.php is passing the country variable properly and it is assumed that the findcity.php routine is the problem. Everything points to the PDO part since using the original mysql calls to the db work fine.

Discussion continued from

Hi,

If I understand the problem correctly, you have a database query that worked using the old mysql extension and you are trying to convert it to use PDO.

If this is indeed the case, it would be better to write:


I tried to change this old code:

Old mysql code

to use PDO. Here’s what I got.

New PDO code

When I run it, it produces the following error:

Error message

What am I doing wrong?


As an aside, why are you including all of the AJAX code in your question, as well as a bunch of commented out code? This just makes the question more difficult to read.

When you changed from mysql to PDO, did you also change the column names in your database, or is this a typo?

$result = $db->prepare('SELECT * FROM city WHERE contryid = :country');
..
... and further down in the commented code ...
..
// $query="select city from city where countryid=$country";

Old version uses a column called ‘countryid’, new uses ‘contryid’.

^ this would be my assumption as to what’s gone wrong.

If fixing that does not correct the problem, what happens when you go directly to findcity.php?country=SomeActualCountryName ?

1 Like

OK, since this is still a problem even after trying all the above (StarLion, calling the findcity.php?country=1 does not show anything and not sure why), I am putting in more info I hope helps to narrow down where the mistake is being made.

I have put two sites up, [Test4][1] which uses the new PDO stuff and is giving me the error from the ajax error handler and [Test5][2] which uses the old, deprecated mysql calls. BOth these sites use the same index.php file (above) but the findcity.php files called are different.

Code for findcity.php in Test4:

<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>

<? $country=$_REQUEST['country'];

    include_once(getRootPath(). 'inc/db_connect_PDO2.php');
    
    $db = get_db_connection();
    
    $result = $db->prepare('SELECT * FROM city WHERE countryid = :country');
    $result = $db->bindParam(':country',$country);
    $result->execute();


?>
<select name="city">
<option>Select City</option>
// <? while($row=mysql_fetch_array($result)) { ?>
<? while($row = $result->fetch(PDO::FETCH_NUM)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
<?php
// returns the relative path from current folder to Web Site Root directory
function getRootPath() {
  $current_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
  $current_host = pathinfo($_SERVER['REMOTE_ADDR'], PATHINFO_BASENAME);
  $the_depth = substr_count( $current_path , '/');

  // Set path to root for includes to access from anywhere
  if($current_host == '127.0.0.1') $pathtoroot = str_repeat('../' , $the_depth-1);
  else $pathtoroot = str_repeat ('../' , $the_depth);

  return $pathtoroot;
}
?>

Code for findcity.php in Test5:

<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>

<? $country=$_REQUEST['country'];
include_once(getRootPath() .  "inc/db_connect.php");

mysql_select_db('sacccco1_MainDB');
$query="select city from city where countryid=$country";
$result=mysql_query($query);

?>
<select name="city">
<option>Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
<?php

// returns the relative path from current folder to Web Site Root directory
function getRootPath() {
  $current_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
  $current_host = pathinfo($_SERVER['REMOTE_ADDR'], PATHINFO_BASENAME);
  $the_depth = substr_count( $current_path , '/');

  // Set path to root for includes to access from anywhere
  if($current_host == '127.0.0.1') $pathtoroot = str_repeat('../' , $the_depth-1);
  else $pathtoroot = str_repeat ('../' , $the_depth);

  return $pathtoroot;
}

?>

Hope this helps to give me more insight as to what is wrong.

Also, in the error log for the Test4 PDO stuff, it says “PHP Parse error: syntax error, unexpected end of file in /Test4/findcity.php on line 40” but, as you can see, it does not have 40 lines in there. Just another anomaly I can see and maybe points more to what is mistaken.

E
[1]: http://saccc567.com/Test4/
[2]: http://saccc567/Test5/

Unexpected EOF usually means you’ve forgotten to close one of your statement blocks. Specifically…

// <? while($row=mysql_fetch_array($result)) { ?>

This line is NOT commented out - because the // is outside of the PHP code, it gets treated though the “//” is regular HTML. So the PHP engine opens two while loops, and then only closes 1, and it gets confused.

1 Like

StarLion, thank you. You pointed out something I had forgotten about - commenting out php has to be done inside the tags not outside.

That pointed me to another error that I have since fixed. This is now working as I thought it should.

Now, on to learning how to use this for my project(s).

Thanks all
E

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.