/    Sign up×
Community /Pin to ProfileBookmark

Problem with XMLHttpRequest ‘send’ function using ‘POST’ method for registration.

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>[/CODE]

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
}

}
?>[/CODE]

[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.[/B]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@CarthAug 15.2010 — There is a problem in register.php, a bracket is in the wrong place:

[code=php]If (mysql_num_rows($sql>0))[/code]

Change to

[code=php]If (mysql_num_rows($sql)>0)[/code]

Also, not sure if you're aware, but this won't prevent SQL injections:
[code=php]//Prevent SQL injections
$username = $_POST['vusername'];
$email = $_POST['vemail'];
[/code]


You should call mysql_real_escape_string():
[code=php]//Prevent SQL injections
$username = mysql_real_escape_string($_POST['vusername']);
$email = mysql_real_escape_string($_POST['vemail']); [/code]
Copy linkTweet thisAlerts:
@CarthAug 15.2010 — It also doesn't make sense to put $(document).ready() inside the formclose() function, instead put everything inside it. And no need to be using getElementsByTagName() when you have jQuery to do the work for you - this is one of the places where jQuery can really make coding faster and simpler.

You can also use jQuery for the AJAX.

For your example, I would use .get().

Simple example (needs expanding):

[CODE]<script language="javascript">
var loggedin = false;
$(document).ready(function() {
$.get("register.php",
{
vusername: $("input").eq(0).val(),
vpassord: $("input").eq(1).val(),
vemail: $("input".eq(2).val()
}, function(data, textStatus, XMLhttprequst) {
if (data == "true") {
loggedin = true;
// ...
});
});
});
</script>[/CODE]
×

Success!

Help @bmatthews spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...