Page 1 of 1

ajax check mysql connection

Posted: Wed Jul 18, 2007 6:45 pm
by ziggy3000
i need help using javascript and php to make a script that allows you to check your database connection. this is my php/html:

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>";
and this is my javascript:

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);
}
and conn.php:

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;
?>
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

Posted: Wed Jul 18, 2007 7:09 pm
by nathanr
this bit here is all wrong mate:

Code: Select all

var url="conn.php;
url = url+"?host="+host;
url = url+"&user="+user;
url = url+"&pass="+pass;
url = url+"&name="+name;
should be

Code: Select all

var url="conn.php?host="+host+"&user="+user+"&pass="+pass+"&name="+name;

Posted: Wed Jul 18, 2007 7:17 pm
by ziggy3000
still doesn't work the html isn't changing :(

Posted: Wed Jul 18, 2007 10:51 pm
by Phoenixheart
I think your problem comes from the javascript, not php. If you want to go Ajax, then I recommend trying some open libraries to make your life easier. IMHO, Dojo is very strong, but jQuery is much more simplier.

Posted: Wed Jul 18, 2007 11:27 pm
by Benjamin
Have you tested to see if your conn.php file is being requested by the javascript?

Posted: Thu Jul 19, 2007 1:05 pm
by ziggy3000
well, when i go to conn.php? and type in my details in the address bar, it outputs what it is supposed to,
how do you test if conn.php is being requested by javascript? i just started learning when i wanted to create a wysiwyg editor(that's coming along very good :) )

Posted: Thu Jul 19, 2007 1:17 pm
by Benjamin
Have it send you an email or write to a log file whenever it is called.

Posted: Thu Jul 19, 2007 1:30 pm
by ziggy3000
i have SMTP thing disabled since i am working on a local server
and i got most of my code from here

Posted: Thu Jul 19, 2007 2:40 pm
by Benjamin
So do you know if it's being called by the javascript yet?

Posted: Thu Jul 19, 2007 3:57 pm
by ziggy3000
can u give me code to test? i dont really get what you are saying.

Posted: Thu Jul 19, 2007 4:01 pm
by Benjamin
You can research file_put_contents or the mail() functions in order to create a small debugger.

Posted: Thu Jul 19, 2007 5:23 pm
by ziggy3000
so i put a textarea with the id 'test' and i just the following jscode

Code: Select all

var url="conn.php?host="+host+"&user="+user+"&pass="+pass+"&name="+name;
xmlHttp.open("GET",url,true)
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) { 
//document.getElementById("connresult").innerHTML = xmlHttp.responseText;
document.getElementById('test').value = xmlhttp.responseText;
}
};
xmlHttp.send(null);
}
and the value of the textarea doesn't change

maybe because it's in a seperate js file?

AOL Speak

Posted: Thu Jul 19, 2007 11:51 pm
by feyd
ziggy3000, I think I've ask you enough via private message. Not anymore.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.

Posted: Fri Jul 20, 2007 3:25 pm
by ziggy3000
well, since the js is not working, i have decided not to use ajax, and just use php... and it works