0

I have a simple AJAX function bound to a button that should execute a PostgreSQL query. However, when I click the button that I bound the ajax query to, all I get is the confirmation that the database connection was successful. Nothing seems to happen withe the ajax result (should be printing to console in the handleAjax() function. What am I doing wrong?

This is the javascript code (with jquery):

$(document).ready(function() {

    function sendAjax() {

        $.ajax({
            url: "db/database.php",
            success: function (result) {
                handleAjax(result);
            }
        });
    }

    function handleAjax(result) {
        console.log(result);
    }

    $("#submit-button").on("click", sendAjax);

});

And this it the contents of database.php:

    <?php

    function dbconn(){

        ini_set('display_errors', 1); // Displays errors

        //database login info
        $host = 'localhost';
        $port = 5432;
        $dbname = 'sms';
        $user = 'postgres';
        $password = 'postgres';
        // establish connection
        $conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
        if (!$conn) {
            echo "Not connected : " . pg_error();
            exit;
        } else {
            echo "Connected.";
        }
    }

    $conn = dbconn();
    $sql = "SELECT * FROM numbers;";

    $result = pg_query( $sql ) or die('Query Failed: ' .pg_last_error());
    $count = 0;
    $text = 'error';
    while( $row = pg_fetch_array( $result, null, PGSQL_ASSOC ) ) {
        $text = $row['message'];
        //echo $text;
    }
    pg_free_result( $result );

?>

3 Answers 3

1

The problem is in the database.php file, all you get is "Connected." because you don't print your result at the end. Ajax only receive the output of the php file.

So at the end of your php file you should add :

echo $text;

And you also should remove the echo "Connected.";

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

Comments

1

AJAX is not a magic wand that in magic way reads PHP code. Let's say AJAX is a user. So what does user do.

  1. Open web page
  2. Wait until PHP execute code and display data
  3. Tells you what he sees

If you don't display anything, ajax can't tell you what he saw.

In thi's place is worth to say that the best way to communicate between PHP and AJAX is using JSON format.


Your code generally is good. All you have to do is to display your data. All your data is in your $text var. So let's convert your array ($text) to JSON.

header('Content-Type: application/json');
echo json_encode($text);

First you set content-type to json, so ajax knows that he reads json. Then you encode (convert) your PHP array to js-friendly format (JSON). Also delete unnecessary echoes like 'Conntected' because as I said, AJAX reads everything what he sees.

Comments

0

You should return $conn from dbconn()

 if (!$conn) {
        echo "Not connected : " . pg_error();
        exit;
    } else {
        echo "Connected.";
        return $conn;
    }

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.