ajax check mysql connection
Posted: Wed Jul 18, 2007 6:45 pm
i need help using javascript and php to make a script that allows you to check your database connection. this is my php/html:
and this is my javascript:
and conn.php:
conn.php works , but i dont think the javascript is working, and i not that good with javascript so i need help getting the conn.php result to that html page in the div box
Code: Select all
echo "<table class='content'><th>Database Configuration</th><tr><td>";
echo "<table><tr><td>";
echo "Database Host<br />";
echo "Database User<br />";
echo "Database Password<br />";
echo "Database<br />";
echo "Database Prefix<br />";
echo "</td><td>";
echo "<input type='text' id='host' value='localhost' /><br />";
echo "<input type='text' id='user' /><br />";
echo "<input type='password' id='pass' /><br />";
echo "<input type='text' id='name' /><br />";
echo "<input type='text' /><br />\n";
echo "<input type='button' onclick='testconn( document.getElementById(\"host\"), document.getElementById(\"user\"), document.getElementById(\"pass\"), document.getElementById(\"name\"));' value='Test Connection' /><br />";
echo "<div id='connresult'></div>";
echo "</td></tr></table>";Code: Select all
var xmlHttp // xmlHttp variable
function GetXmlHttpObject(){ // This function we will use to call our xmlhttpobject.
var objXMLHttp=null // Sets objXMLHttp to null as default.
if (window.XMLHttpRequest){ // If we are using Netscape or any other browser than IE lets use xmlhttp.
objXMLHttp=new XMLHttpRequest() // Creates a xmlhttp request.
}else if (window.ActiveXObject){ // ElseIf we are using IE lets use Active X.
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") // Creates a new Active X Object.
} // End ElseIf.
return objXMLHttp // Returns the xhttp object.
} // Close Function
function testconn(host, user, pass, name){
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null){
alert ("Browser does not support HTTP Request")
return
}
var url="conn.php;
url = url+"?host="+host;
url = url+"&user="+user;
url = url+"&pass="+pass;
url = url+"&name="+name;
xmlHttp.open("GET",url,true)
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
document.getElementById("connresult").innerHTML = xmlHttp.responseText;
}
};
xmlHttp.send(null);
}
Code: Select all
<?php
//
// Conn.php
// Desc: Allows ajax connection testing
//
$host = $_GET['host'];
$user = $_GET['user'];
$pass = $_GET['pass'];
$name = $_GET['name'];
$conn = mysql_connect($host, $user, $pass);
if(!$conn){
DEFINE("conn", false);
$output = "<span style='color: red;'>Could not Connect to Database!</span>";
}
$con = mysql_select_db($name);
if(!$con){
DEFINE("conn", true);
DEFINE("con", false);
$output = "<span style='color: red;'>Could not select database!</span>";
}
else{
$output = "<span style='color: green;'>Succesful Connection!</span>";
}
echo $output;
?>