0

I am busy learning Ajax and need to use Postgres for the backed DB. I found an example for Mysql which works but when I convert it to Postgres, it stops working. I have not made any changes to the HTML code when swapping from Mysql to Postgres.

When I execute the php code at command line (php test.php), it works and outputs the correct number of rows and data.

Here is the php

<?php 

$dbh = pg_connect("host=192.168.0.8 dbname=test user=test password=test");
if (!$dbh) {
    die("Error in connection: " . pg_last_error());
}       

// execute query
$sql = "SELECT * FROM users";
$result = pg_query($dbh, $sql);
if (!$result) {
 die("Error in SQL query: " . pg_last_error());
}       

// iterate over result set
// print each row
while ($row = pg_fetch_array($result)) {
   echo "Name: " . $row[1];
    echo "<BR />";
}       

// free memory
pg_free_result($result);       

// close connection
pg_close($dbh);
?>

And here is the html

<html>
<head>
    <script language="JavaScript" type="text/javascript">
    function ajax_post(){
        // Create our XMLHttpRequest object
        var hr = new XMLHttpRequest();
        // Create some variables we need to send to our PHP file
    var url = "test.php";
    var fn = document.getElementById("name").value;
    var vars = "name="+fn;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
        document.getElementById("name").value = "";
    }
    </script>
    <script type="text/javascript">
        function setFocus() 
        {
          document.getElementById("name").focus();
        }
    </script>
</head>

<body onload="setFocus()">
    <div>
    <form name="input" action="" onsubmit="ajax_post(); return false;"> 
        Barcode : <input type="text" id="name" size="30"><br />       
    </form>
    </div>
    <div style="background-color: lightyellow; border: 1px solid black; width: 400; height: 300;">
        <div id="status"></div>
    </div>
</body>

One thing I have noticed is that when I run the test.php file at command line with postgres, anew-line character is always appended to the end of the data.

Thanks O

4
  • Check if pg extension is loaded. PHP CLI and PHP/Apache can have different php.ini settings Commented May 16, 2012 at 8:20
  • 1
    "it stops working" is not an error message. Commented May 16, 2012 at 9:20
  • 2
    In what way does it not work? Does it emit an error? If so, what? Commented May 16, 2012 at 9:29
  • This could be out-dated but iirc pg_fetch_array and friends require a row specifier and otherwise will repeat the first row over and over. If you are seeing an endless loop, that's why. Commented Oct 14, 2012 at 5:59

1 Answer 1

1

Install Firefox and the Firebug add-on. In Firebug, you can conveniently watch the whole communication between your HTML and PHP page, and there's also a great JS debugger with breakpoints etc. included. You don't seem to have a good idea what the error exactly is, but you should still be able to spot it with these tools.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.