I work on a web interface which receives and send informations to a distant PostGreSQL database.
From what I found on the internet it seems that the best way is to create a small php script in an external .php file.
I followed that idea,
so I have that script pg_connection.php which tests the connectivity to my database (with a pg_connect) in that style :
$db = pg_connect("host=localhost port=5432 dbname=**** user=**** password=****")
or die("Not Connected");
if ($db) {echo (Connected);} else {echo (Not Connected);}
If I launch only that pg_connection.php in my webbrowser, it works fine (I just have wrotten Connected with the correct login infos entered in my script or Not connected if I put a random ip address).
Then in my .js(+jquery) external script I use :
$(document).ready(function(){
$("#dbstatus").load("php-services/pg_connection.php");
var dbstatus = new String();
dbstatus = $("#dbstatus").val();
if (dbstatus == "Connected")
{ /*jquery code to show a green light on a section in my html page*/}
else { /*jquery code to show a red light*/}
}
And that works partially :
In my $(“#dbstatus”) object it will replace the default text by Connected or Not Connected,
But it doesn’t produce any effect on the green/red light in my conditionnal
Then I went in my Chrome console and type dbstatus, and I realized that the content of my var is
<div id="dbstatus" class="span3">Connected</div>
when I expected it to be just “Connected”.
Any idea on how to clean that var from all these extra html stuffs ?
Is there more simple method implemented in js or Jquery to check a postgreSQL database status ?
Try to modify your code to:
And in JS: