Jump to content

AJAX + PHP, error?


Chidori Soul

Recommended Posts

Alright, so I'm doing a pure dynamic AJAX site, and i've been on a register script for 2 days now, and for some reason, something about it makes it weird.

 

<?php
session_start();
require('require/connect.php');
require('require/clean_code.php');
$name = safe($_GET['user']);
if(isset($_GET['user'])) {
$pass = safe($_GET['pass']);
$email = safe($_GET['email']);
$gender = safe($_GET['gender']);
$class = safe($_GET['type']);
if($name=='') {
echo "Please enter a name.";
}
elseif($pass=='') {
echo "Please enter a password.";
}
elseif($email=='') {
echo "Please enter an email.";
}
elseif($gender != "Male" && $gender != "Female") {
echo "Please choose a legit gender.";
}
elseif($class != "Warrior" && $class != "Mage") {
echo "Please choose a legit class.";
}
else {
$q = mysql_query("SELECT * FROM `users` WHERE `name`='".$name."'");
$c = mysql_num_rows($q);
$q1 = mysql_query("SELECT * FROM `users` WHERE `email`='".$email."'");
$c2 = mysql_num_rows($q1);
if($c > '0') {
echo "This name is already in use.";
}
elseif($c1 > '0') {
echo "This email is already in use.";
}
else {
$pass2 = sha1($pass);
mysql_query("INSERT INTO `users` (`name`, `pass`, `email`, `type`, `gender`) VALUES ('".$name."', '".$pass2."', '".$email."', '".$class."', '".$gender."')") or die(mysql_error());
echo "You have successfuly registered, ".$name.".";
}
}
}
else {
?>
<head>
<script type='text/javascript' src='require/jquery.js'></script>
<script type='text/javascript' src='require/main.js'></script>
<script type='text/javascript'>
function function1() {
var user = window.document.reg.user.value;
var pass = window.document.reg.password.value;
var email = window.document.reg.email.value;
var gender = window.document.reg.gender.value;
var class = window.document.reg.class.value;
var url = "reg.php?user=" + user + "&pass=" + pass + "&email=" + email + "&gender=" + gender + "&type=" + class + "";
loadlink(url);
}
</script>
</head>
TestRegister
<form name='reg' onsubmit='return function1();'>
<table width='98%' style='border: 1px solid black;' valign='top' align='center'>
<tr><td width='30%' style='border: 1px solid black;'>Username</td><td width='70%' style='border: 1px solid black;'><input type="text" name="user"></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Password</td><td width='70%' style='border: 1px solid black;'><input type="password" name="password"></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Email</td><td width='70%' style='border: 1px solid black;'><input type="text" name="email"></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Gender</td><td width='70%' style='border: 1px solid black;'><select name="gender"><option value="Male">Male</option><option

value="Female">Female</option></select></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Class</td><td width='70%' style='border: 1px solid black;'><select name="class"><option value="Warrior">Path Of Swordsmen</option><option

value="Mage">Guidance Of Magician</option></select></td></tr>
<tr><td width='30%' colspan='2' style='border: 1px solid black;'><input type='submit' name='submit' value='Register!' onclick='function1();'></td></tr>
</table>
</form>
<?php
}
?>

 

If you tried this, it would be successful, but it would say that the username is already registered, so it really lies. Can someone point out the error on this AJAX/PHP?

Link to comment
Share on other sites

<?php
session_start();
require('require/connect.php');
require('require/clean_code.php');
$name = safe($_GET['user']);
if(isset($_GET['user'])) {
$pass = safe($_GET['pass']);
$email = safe($_GET['email']);
$gender = safe($_GET['gender']);
$class = safe($_GET['type']);
if($name=='') {
echo "Please enter a name.";
}
elseif($pass=='') {
echo "Please enter a password.";
}
elseif($email=='') {
echo "Please enter an email.";
}
elseif($gender != "Male" && $gender != "Female") {
echo "Please choose a legit gender.";
}
elseif($class != "Warrior" && $class != "Mage") {
echo "Please choose a legit class.";
}
else {
$q = mysql_query("SELECT * FROM `users` WHERE `name`='".$name."'");
$c = mysql_num_rows($q);
$q1 = mysql_query("SELECT * FROM `users` WHERE `email`='".$email."'");
$c1 = mysql_num_rows($q1);
if($c > 0) {
echo "This name is already in use.";
}
elseif($c1 > 0) {
echo "This email is already in use.";
}
else {
$pass2 = sha1($pass);
mysql_query("INSERT INTO `users` (`name`, `pass`, `email`, `type`, `gender`) VALUES ('".$name."', '".$pass2."', '".$email."', '".$class."', '".$gender."')") or die(mysql_error());
echo "You have successfuly registered, ".$name.".";
}
}
}
else {
?>
<head>
<script type='text/javascript' src='require/jquery.js'></script>
<script type='text/javascript' src='require/main.js'></script>
<script type='text/javascript'>
function function1() {
var user = window.document.reg.user.value;
var pass = window.document.reg.password.value;
var email = window.document.reg.email.value;
var gender = window.document.reg.gender.value;
var class = window.document.reg.class.value;
var url = "reg.php?user=" + user + "&pass=" + pass + "&email=" + email + "&gender=" + gender + "&type=" + class + "";
loadlink(url);
}
</script>
</head>
TestRegister
<form name='reg' onsubmit='return function1();'>
<table width='98%' style='border: 1px solid black;' valign='top' align='center'>
<tr><td width='30%' style='border: 1px solid black;'>Username</td><td width='70%' style='border: 1px solid black;'><input type="text" name="user"></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Password</td><td width='70%' style='border: 1px solid black;'><input type="password" name="password"></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Email</td><td width='70%' style='border: 1px solid black;'><input type="text" name="email"></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Gender</td><td width='70%' style='border: 1px solid black;'><select name="gender"><option value="Male">Male</option><option

value="Female">Female</option></select></td></tr>
<tr><td width='30%' style='border: 1px solid black;'>Class</td><td width='70%' style='border: 1px solid black;'><select name="class"><option value="Warrior">Path Of Swordsmen</option><option

value="Mage">Guidance Of Magician</option></select></td></tr>
<tr><td width='30%' colspan='2' style='border: 1px solid black;'><input type='submit' name='submit' value='Register!' onclick='function1();'></td></tr>
</table>
</form>
<?php
}
?>

 

 

I did that, and still, the same result, I think it has something to do with my AJAX.

Link to comment
Share on other sites

I would print out the query and make sure it contains the expected data.  Check your database for a user entry that's blank.  The script could be losing the username somewhere and trying to insert a blank user, which would be used after the first try. 

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.