Digital Brent
Digital Brent

Reputation: 1285

What is ajax returning to me? (ajax call to php file)

I am pretty much brand new to ajax. This is an ajax call which I am using to retrieve information from a sql database using a php document called "bridge.php". I just cannot seem to understand what I am getting back from this, if anything at all. Is it an array? an object? Am I not getting anything back because I'm using a get when I should be using a post? The amount of info that I'm trying to get back would not fit in a get, but the amount I'm sending through the call is small enough to fit.

<script type="text/javascript">
    function refreshPro(namex){
        alert(namex);
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("port_work").innerHTML=xmlhttp.responseText;   //what is xmlhttp.responseText?
            }
        }
        xmlhttp.open("GET","../bridge.php?pro="+namex,true);
        xmlhttp.send();
    }
</script>

and now for the php(bridge.php) which takes the get var from the url and queries the database, which I know this part works okay by itself...

<?php
$pro = $_GET["pro"];
$sql = "SELECT * FROM portfolio WHERE title LIKE \"%$pro%\"";

$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {

?>

    <div id="portfolio_item">
        <h2><?php echo $row['title']; ?></h2>
        <img src="<?php echo $row['image_paths']; ?>" alt="<?php echo $row['title']; ?>" />
        <p id="portfolio_desc"><?php echo $row['description']; ?></p>
    </div>

<?php
}
?>

And yes, I've done my homework. I've studied these two other posts but one starts talking about JSON which I know nothing about, and the other didn't quite seem to match my problem, but I still found the answer kinda useful. Returning AJAX responseText from a seperate file and Ajax call not returning data from php file

Upvotes: 0

Views: 134

Answers (4)

Ezequiel Muns
Ezequiel Muns

Reputation: 7752

You are getting back plain HTML from the server. The responseText variable is a string containing the plaintext response to the GET request.

A GET request is like any other browser request you normally perform when visiting a given URL. Thus you can test what the bridge.php script sends for a given input by visiting http://url/to/bridge.php?pro=<someValue> using your browser of choice.

It doesn't matter the amount of data you get back, what matterns WRT using a POST or a GET is how much data you want to put in (GET request can only take 255 chars of data, POST is technically unlimited).

If you get nothing visiting the bridge.php script directly, this indicates that this script may be failing resulting in a 500 Internal Server Error code. This doesn't get caught by your javascript code since you're explicitly checking that the code is 200 (success) before doing anything with the response.

I would add:

    ... snip ...
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("port_work").innerHTML=xmlhttp.responseText;   
        }
        else 
        {
           alert('There was a ' + xmlhttp.status + ' error :(');
        }
    }
    ... snip ...

Upvotes: 2

Rajan Rawal
Rajan Rawal

Reputation: 6313

First of all just echo 0 or 1 or any damn keyword 'Hello world' from bridge.php.

the code

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
 alert(xmlhttp.responseText);
 }

says that if everything is goes fine. I.E if the path to the bridge.php then it would go inside this if condition. and will alert response.

If this works perfectly then your code shold work too.

also see the answer mentioned by @ccKep. It is easy to use.

In addition use the tool like Mozilla firebug. So that you can understand that what is going on. Hope this helps.

Upvotes: 0

Kep
Kep

Reputation: 5857

Using jQuery for this makes it rather simple:

<script type="text/javascript" src="path/to/jquery.js">
<script type="text/javascript">
function refreshPro(namex)
{
    $.ajax({
      type: "GET",
      url: "../bridge.php",
      data: {
        pro: namex
      }
    }).done(function( msg )
    {
      $("#port_work").html(msg);
    });
}
</script>

Upvotes: 0

gview
gview

Reputation: 15371

The Ajax runs on the client not on the server, so you need to provide the url of your server. This is not going to work:

xmlhttp.open("GET","../bridge.php?pro="+namex,true);

What might work is:

xmlhttp.open("GET","http://yourhost.com/bridge.php?pro="+namex,true);

Upvotes: -1

Related Questions