Hi, I just prepared a basic html code for a login page and another webpage linked to the login page.I need to connnect it with a mysql database.
But I am unable to do so.
Whathave I done is made a html code with a javascript function which is further calling the php code of the database.
When I tried running it, it should check the username and password in the database and then allow to next webpage.but it is not doing so.
Please let me know a simpler way to connect my database.

<html><head><title> My First HTML Webpage </title> <script>
function check(form)
{
if (str==" ")
{
document.getElementByPassword("txtHint").innerHTML=" ";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+ , Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{//code for IE6,IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 "xmlhttp.status==200)
{
document.getElementByPassword("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","php/db.php?q="+str,true);
xmlhttp.send();
}
</script> </head> <body> <center> <h1> <u>LOGIN FORM</u> </h1> </center> <hr /> <form> <u>USERNAME</u> <input type='text' name=username><br /> <u>PASSWORD</u> <input type='password' name=password><br /> <a href="MyWebPage2.html"><input type='button' onclick='check(this.form)' value='login'></a><br /><br /><br /> <p title=WELCOME!>Please login your information before continuing</p> </form> </body> </html>

this is my HTML code..

$host="localhost"; // Host name.
$db_user="root"; // MySQL username.
$db_password=""; // MySQL password.
$database="mydatabase"; // Database name.
$link = mysql_connect($host,$db_user,$db_password);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
else
{
echo "Mysql Connected Successfully";
}
$login = mysql_query(“select * from table_name where (username = ‘” . $_POST[‘username’] . “‘) and (password = ‘” . md5($_POST[‘password’]) . “‘)”,$db);
$rowcount = mysql_num_rows($login);
if ($rowcount == 1) {
$_SESSION[‘username’] = $_POST[‘username’];
header(“Location: MyWebPage2.html”);
}
else
{
header(“Location: index.html”);
}
mysql_close($link);
?>

and this is my php code.thanks.

Recommended Answers

All 2 Replies

Firstly, why use javascript and AJAX to call a PHP script in the first place? You could just have PHP code in your HTML that handles the database stuff on the post back of the form.
Secondly, you could jQuery or similar to do the AJAX stuff much more easily.

But, if you want to figure out what is wrong with the code you have you need to know which part is failing. Either debug your PHP code or alter it to write to a file when called so you can check that the AJAX call to the script is working OK.
Then you need to check your database connection. And if that works then you need to check the values being returned. Once you know where the problem lies you can do something about it.

Actually I am new at this.I found a video which should me this way of connecting the database. But I will try to make a php code in html.
thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.