Page 1 of 1

OOP php and ajax

Posted: Wed Dec 01, 2010 10:31 am
by Wilbo
Hi there,

I want to use ajax to make my sites a bit sexier, but I'm stuggling to grasp how I'm going to implement it.

For example, here's a sample script for a sign-up form:

Code: Select all

<?php
session_start();

if(isset($_POST['signup']))
{
  include('classes/class.signup.php');

  $signUp = new SignUp();

  if($signUp->signThemUp())
  {
     echo '<h3>Success!</h3><p>You have been successfully signed up.</p>';
  }
  else
  {
    $signUp->displayErrors();
  }
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<p><label>First Name: </label><input type="text" name="firstname" /></p>
    <p><label>Last Name: </label><input type="text" name="lastname" /></p>
    <p><label>Email: </label><input type="text" name="email" /></p>
    <p><label>Password: </label><input type="password" name="password" /></p>
	<p><label>Retype Password: </label><input type="password" name="repassword" /></p>
    <input type="hidden" name="token" value="<?php echo $token; ?>" />
    <p><input type="submit" name="signup" value="Sign Up" /></p>
</form>
Nice and simple, if signThemUp() returns true then the success message is displayed.
Now, my question is, how do I ajax-ify it? I'm not asking for a tutorial on ajax, I just want to know the best way of implementing it when using objects as above.

Thanks for your time!

Re: OOP php and ajax

Posted: Thu Dec 02, 2010 7:01 pm
by spedula
What you need to do is separate the form file and the PHP file.

In the form file you could use jQuery to post the AJAX request to the PHP file, wait for response then act accordingly. I find it easier to have php json_encode the output, then just tell jQuery to expect a json return.

Here is a sample code I use quite often.

Code: Select all

//jQuery in FORM FILE

$(document).ready(function(){

	$("#login_form").submit(function() {
		$.post('ajax.php', {
                                firstname: $('[name=firstname]').val(),
				lastname: $('[name=lastname]').val(),
                                email: $('[name=email]').val(),
                                password: $('[name=lastname]').val(),
                                repassword: $('[name=repassword]').val(),
                                token: $('[name=token]').val()
                                }, 
			function(data) {
				if(data.success) {
					window.location=data.redirect
				}
				else{
					msg = data.message
                                        $('#errorConsole').html(msg)
				}
			
		}, 'json');
		return false;
	});
});

//PHP FILE
if($_POST) {
	if(whatever conditions you need met) {
		$data['success'] = true;
		$data['redirect'] = 'index.php';
	}
	else {
		$data['success'] = false;
		$data['message'] = '<font color="#FF0000">Your username and/or password is invalid.</font>';
	}
	echo json_encode($data);
}

Re: OOP php and ajax

Posted: Mon Dec 06, 2010 4:16 am
by Wilbo
Thanks a lot, I'll give it a go!