Getting error I can't figure out


#1

OK, I have a php page with an ajax piece in it that is giving me an error. The error is triggered on line 61 of the code and displays as "There was a problem while using XMLHTTP: Internal Server Error".

Now, I must admit that I am trying to learn ajax so this is actually something that I found and modified off the internet.

Any help would be greatly appreciated.

Link as I cannot get the whole PHP code into this area and show properly (must be doing something wrong).

E


Help me figure out what is wrong
#2

Without seeing the code, we can only guess. But it sounds (to me) like whatever server you're trying to use via AJaX is not giving a 200 response code.

^_^


#3

Copy and past the code in the response chatbox. Then select all the code and right above the textbox, there is a </> button. Hit that to format your code.


#4

You're passing a year value to a script that looks like it's expecting a COUNTRY value. Maybe that's the issue.

Sorry.. that came off as a bit snarky. It does appear as though the receiving script is asking for something other than a year value.

HTH,

^_^


#5

As Ryan says, you'll need to format it.

If you check the network tab of your browser's dev tools, you can see the AJAX request is firing off as expected and the server is returning a 500 error:

Drilling down a little further the response seems to be:

Show year passed to me: <br>

To help you more, we'll need to see the PHP


#6

Here is the code (I hope)

<?php
    session_start();  // always use this, just in case

    include_once (getRootPath() . 'inc/db_connect_PDO2.php');

    // connect to the db
    $db = get_db_connection();

    // $mPixResult = $db->prepare('SELECT * FROM Show_Pix WHERE Show_Year = :PixYear AND Show_Name = :PixShowName');
    $mPixResult = $db->prepare('SELECT DISTINCT(Show_Year) FROM `Show_Pix` order by Show_Year DESC');
    $mPixResult->execute();
?>

<html>
<head>
<title>SACCC Caption Edit Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<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">Show Year</td>
    <td  width="150"><select name="Show_Year" onChange="getCity('getCity.php?show_year='+this.value)">
    <option value="">Select year</option>

    <?php while ($row = $mPixResult->fetch(PDO::FETCH_ASSOC)) 
    { 
?>
    <option value=<?=$row['Show_Year']?>><?=$row['Show_Year']?></option>
<?php
    }
?>
        </select></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td ><div id="citydiv"><select name="city">
    <option>Select Name</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>
<?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;
}
?>

#7

This appears to be the code for the page you linked to previously.
We would need to see getCity.php, as this is the page you are trying to load via AJAX and which is returning a 500 error.

If I navigate to view-source:http://saccc567.com/Test/getCity.php, I see:

Show year passed to me: <br>

As per the screen shot I posted yesterday.


#8

here is the getCity.php code:

<? $show_year=$_Post['Show_Year'];
    echo 'Show year passed to me: ' . $show_year . '<br>';

    $db = get_db_connection();
    if (!$db) {
        die('Could not connect: ' . mysql_error());
    }

    $Result = $db->prepare('SELECT DISTINCT(Show_Name)  FROM Show_Pix WHERE Show_Year = :PixYear');
    $Result->bindValue(':PixYear', $show_year);
    $Result->execute();

    // mysql_select_db('sacccco1_MainDB');
    // $query="select DISTINCT(Show_Name) from Show_Pix where Show_Year=$showyear";
    // $result=mysql_query($query);

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

#9

Also, I need to understand what the best tools are for this debugging and how to use them. Any suggestions on tools and tutorials on debugging both php and javascript?

E


#10

This is probably what is causing the problem.
Try removing that, then making sure that the <select> element is the only thing output to the page.

As regards debugging, I use Chrome's Dev tools.
Here is a great overview.


#11

You are setting the variable "show_year" to a POST value, but the AJaX is sending via GET.

Didn't notice until just now the PixYear being set.

<?=$row['city']?>

^_^


#12

OK, here is my updated getCity code that this still gives me the same error.

<? $show_year=$_GET['Show_Year'];
    // echo 'Show year passed to me: ' . $show_year . '<br>';

    $db = get_db_connection();
    if (!$db) {
        die('Could not connect: ' . mysql_error());
    }

    $Result = $db->prepare('SELECT DISTINCT(Show_Name)  FROM Show_Pix WHERE Show_Year = :PixYear');
    $Result->bindValue(':PixYear', $show_year);
    $Result->execute();

    // mysql_select_db('sacccco1_MainDB');
    // $query="select DISTINCT(Show_Name) from Show_Pix where Show_Year=$showyear";
    // $result=mysql_query($query);

    ?>
    <select name="showName">
    <option>Select Name</option>
    <? while($row=mysql_fetch_array($result)) 
    { 
?>
<option value><?=$row['Show_Name']?></option>
<? 
    } 
?>
</select>

#13

Network admins are now blocking the link to your site, so I can't View Source, anymore.

Is the getCity.php?{var} called "Show_Year"? If not, then the code above needs to change to be what is being passed.

^_^


#14

Might I also ask what you are trying to accomplish?
Is this a learning exercise or is it for a project?


#15

Pullo,

Both. I want to try to understand ajax better and am using it for the basis of a project - namely to get a list of pictures from my database using the year they were taken and the venue name (we have more than one venue per year).

So, hope that helps. I need to understand what I have done incorrect to cause this to error out.

Thanks
E


#16

Would you consider using the jQuery library (or at least part of it)?
It adds a great set of utility functions for dealing with AJAX requests in a cross-browser way.


#17

jQuery and Mootools are great libraries. I'm of the opinion that one should learn vanilla JS before delving into libraries. I think it better helps the coder to understand.

Just my $0.032761

^_^


#18

I would entertain using anything that will help with my projects - especially this one which, as stated to Pullo before, is to get the year of the venue, then, from that (all this is in the DB), get the venue name so that I can then get a listing of the images (actually the location of the image) from the DB for the user to review/edit.

Thanks.
E


#19

One way you could do it, would be like this:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>AJAX test</title>
  </head>
  <body>
    <p>
      <label for="show_year">Show Year:
        <select id="show_year">
          <option value="">Select year</option>
          <option value=2014>2014</option>
          <option value=2013>2013</option>
          <option value=2012>2012</option>
          <option value=2011>2011</option>
          <option value=2010>2010</option>
          <option value=2009>2009</option>
          <option value=2008>2008</option>
          <option value=2007>2007</option>
          <option value=2006>2006</option>
          <option value=2000>2000</option>
        </select>
      <label>
    </p>
    <p>You selected: <span id="result">nothing, yet!</span></p>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $("#show_year").on("change", function(){
        $.ajax({
          type: "POST",
          url: "submit.php",
          cache: false,
          data: { "show_year": $("#show_year").val() },
          success: function (response){
            console.log(response);
            $("#result").text(response);
          }
        });
      });
    </script>
  </body>
</html>

submit.php

<?php
$show_year = $_POST["show_year"];

// You now have the value submitted via AJAX stored in the $show_year variable.
// You would normally use it to do some database stuff
// Then echo the result
// In this case, I'll just echo the year back to the page
//
echo $show_year;

Demo

I know this doesn't do more than echo the value selected back to the page, but hopefully it demonstrates the principle reasonably well.


#20

Pullo,

This is fine but how would I get the venue NAME (contained in the db) using the year? That is what I really need to do.

Thanks