Thread: PHP and AJAX
View Single Post
Old 09-27-2009, 09:26 PM   #1 (permalink)
9three
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default PHP and AJAX

Hey,

I have a method that registers users through an AJAX form.

HTML:
Code:
<form method="POST" id="nameForm">
Firstname:
<br />
<input type="text" id="fname" />
<br /><br />
Lastname:
<br />
<input type="text" id="lname" />
<br /><br />
Username:
<br />
<input type="text" id="username" />
<br /><br />
Password:
<br />
<input type="text" id="password" />
<br /><br />
Email:
<br />
<input type="text" id="email" />
<br />
<input type="submit" name="submit" value="Submit" />
<div class="msgbox"></div>
</form>
When you click submit the ajax file is called:

Code:
$('#screen').show();
      setTimeout(function () {$('#screen').fadeOut();}, 3000);
      $.post('ajax.Register.php', {
        fname: fname,
        lname: lname,
        username: username,
        password: password,
        email: email
        }, function (data) {
        if (data == 0) {
          setTimeout(function() {
            $('.msgbox').fadeIn('def', function () {$(this).html('Ok!');});
          }, 3002);
        }
        else {
          setTimeout(function() {
            $('.msgbox').fadeIn('def', function () {$(this).html('There was an error registering you. Please try again.');});
          }, 3002);
        }
      });
My ajax file calls a php file

PHP Code:
require_once('package/class.User.php');

try {
  
$objUser = new User('localhost''root''''ajax');
}
catch (
PDOException $e) {
  echo 
'Unable to connect';
}

$strFirstname $_POST['fname'];
$strLastname $_POST['lname'];
$strUsername $_POST['username'];
$strPassword $_POST['password'];
$strEmail $_POST['email'];

try {
  
$objUser->Register($strFirstname$strLastname$strUsername$strPassword$strEmail);
}
catch(
Exception $e) {
  echo 
'Unable to query';

This php file initiates an object which I created in my class User:

PHP Code:
public function Register($strFirstname$strLastname$strUsername$strPassword$strEmail) {

    
$strFirstname $this->hDB->real_escape_string(ucfirst($strFirstname));
    
$strLastname $this->hDB->real_escape_string(ucfirst($strLastname));
    
$strUsername $this->hDB->real_escape_string($strUsername);
    
$strPassword $this->hDB->real_escape_string(trim($strPassword));
    
$strEmail $this->hDB->real_escape_string(trim($strEmail));

    if (!
$this->validateRegisterFirstname($strFirstname))
      
$errors true;

    if (!
$this->validateRegisterLastname($strLastname))
      
$errors true;

    if (!
$this->validateRegisterUsername($strUsername))
      
$errors true;

    if (!
$this->validateRegisterPassword($strPassword))
      
$errors true;

    if (!
$this->validateRegisterEmail($strEmail))
      
$errors true;

    if (
$errors) {
      echo 
1;
      return 
false;
    }
    
$register $this->hDB->query("INSERT INTO users

                                   (firstname, lastname, username, password, email)

                                   VALUES ('
$strFirstname', '$strLastname', '$strUsername', '$strPassword', '$strEmail')");

    if (
mysqli_connect_error())
      throw new 
Exception('Query failed');

    if (
$register) {
      echo 
0;
      return 
true;
    }
  } 
I can't seem to get my ajax form to work if I add validation to my method. If I remove the validation then the ajax form works perfect.

What I'm understanding from doing some debugging is that the php is loaded before the ajax values are sent, thus returning false. My validation does a couple of simple things, such as, check if its empty, and make sure its a string. I've tried using sleep function but it did no good (to allow the ajax values to be passed before the script is ran).

Anyone know of any other methods I could use?
9three is offline  
Reply With Quote