TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 09-27-2009, 09:26 PM   #1 (permalink)
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
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easily Format JSON using PHP and Interpret using Javascript Wildhoney Advanced PHP Programming 13 02-01-2013 11:05 AM
Using AJAX and PHP to check filemtime($filename) buildakicker General 4 05-21-2009 10:15 PM
Trying to upload files with PHP and Prototype AJAX EyeDentify General 6 01-19-2009 10:04 PM
Help: Php User Messenger system with ajax tego10122 Advanced PHP Programming 2 10-01-2008 11:42 AM
Rating System: AJAX, MYSQL, PHP Orc General 6 02-26-2008 05:09 PM


All times are GMT. The time now is 09:32 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design