ajax check mysql connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

ajax check mysql connection

Post 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
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post 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;
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

still doesn't work the html isn't changing :(
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Have you tested to see if your conn.php file is being requested by the javascript?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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 :) )
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Have it send you an email or write to a log file whenever it is called.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

i have SMTP thing disabled since i am working on a local server
and i got most of my code from here
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

So do you know if it's being called by the javascript yet?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

can u give me code to test? i dont really get what you are saying.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You can research file_put_contents or the mail() functions in order to create a small debugger.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

AOL Speak

Post 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.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

well, since the js is not working, i have decided not to use ajax, and just use php... and it works
Post Reply