Here is the code as it is now:
(Code is for an homepage registration form and functionality in index.php)
It sends to register.php through AJAX, the username, password and email variables, which are meant to be input into a mySQL database and if all goes well it should echo true(PHP) in the register.php file, this is then read as the response in the index.php and the display is changed to a logged in display, so to speak. It makes perfect sense but the implementation is lacking.
Here is the snippet of the index.php javascript code.
[CODE]<script type=”text/javascript”>
loggedin = false;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
function askimet()
{
if(loggedin != true)
{
xmlhttp.open(“GET”,”disp_loggedout.php”,false);
xmlhttp.send();
txt = xmlhttp.responseText;
document.getElementById(‘inserttopage’).innerHTML=txt;
}
else
{
xmlhttp.open(“GET”,”disp_loggedin.php”,false);
xmlhttp.send();
txt = xmlhttp.responseText;
document.getElementById(‘inserttopage’).innerHTML=txt;
}
}
function XHRreg()
{
$(“#signupform”).css(“opacity”,”0.5″);
var vusername = document.getElementsByTagName(“input”)[0].value;
var vpassword = document.getElementsByTagName(“input”)[1].value;
var vemail = document.getElementsByTagName(“input”)[2].value;
fname = “Muna”;
xmlhttp.open(‘POST’, “register.php”, true);
xmlhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xmlhttp.setRequestHeader(“Content-length”, fname.length);
xmlhttp.setRequestHeader(“Connection”, “close”);
xmlhttp.send(vusername&vpassword&vemail);
xmlhttp.onreadystatechange = alertChange;
function alertChange()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText == “true”)
{
loggedin = true;
formclose();
}
else
{loggedin=false;}
}
}
function formclose()
{
$(document).ready(function()
{
$(“#page-wrap”).fadeOut(500,function(){
askimet()});
}
);
}
}
</script>
I believe it must be a stupid mistake somewhere that is causing this and would really appreciate it if you could set me straight on this one.
and here is the register.php code:
[CODE]<?php
include(“db.php”);
if (isset($_POST[‘vusername’]) && isset($_POST[‘vpassword’]) && isset($_POST[‘vemail’]))
{
//Prevent SQL injections
$username = $_POST[‘vusername’];
$email = $_POST[‘vemail’];
//Get MD5 hash of password
$password = md5($_POST[‘vpassword’]);
//Check to see if username exists
$sql = mysql_query(“SELECT username FROM usersystem WHERE username = ‘$username'”);
If (mysql_num_rows($sql>0))
{
echo(“false”);
//username is already taken
}
else
{
mysql_query(“INSERT INTO usersystem (username, password, email) VALUES ( ‘$username’, ‘$password’, ‘$email’)”);
echo(“true”);
//registration is complete
}
}
?>
[B]P.S: Please be aware that I am still yet to add proper javascript form validation to the index page – before the PHP form validation that is done through AJAX.