Jump to content

PHP redirect after ajax form validation


doforumda

Recommended Posts

While trying to create a user friendly registration system i created an ajax validation works by sending the form data to register.php which in turn processes the data and displays an error above the form if anything is incorrect or missing.

 

However, the problem arises when i try to redirect the user to the next stage of registration. How can we do this?

my code is here

script.js

function sign() {
    $('#container').load('login.html');
}

function joinNow() {
    $('#container').load('register.html');
}

function register() {
    var url = 'register.php';
    
    var firstname = $('input#firstname').val();
    var lastname = $('input#lastname').val();
    var email = $('input#email').val();
    var password = $('input#password').val();
    var repeatpassword = $('input#repeatpassword').val();
    var sex = $('#sex').val();
    var day = $('#day').val();
    var month = $('#month').val();
    var year = $('#year').val();
    
    var queryString = 'firstname=' + firstname + '&lastname=' + lastname + '&email=' + email + '&password=' + password + '&repeatpassword=' + repeatpassword + '&sex=' + sex + '&day=' + day + '&month=' + month + '&year=' +year;
                        //alert(queryString);
/*
    $.post(url,queryString,showRegister);
    
    function showRegister(d) {
        $('#error').html(d);
    }
*/    
   
    $.ajax(
    {
        type: 'POST',
        url: url,
        data: queryString,
        dataType: 'html',
        success: showRegister
    });
    
    function showRegister(d) {
        $('#error').html(d).fadeIn('slow');
    }

}

 

register.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script>
<script src="js/jqueryscript.js" type="text/javascript"></script>
</head>

<body>
<div id="error"></div>
<div id="actionForm">
<p>Fill following form to Join us Now</p>
<form id="form1" name="form1" method="post" action="">
<div>
    <label>First Name:</label>
      <input type="text" name="firstname" id="firstname" />
    </div>
  	<div>
    <label>Last Name:</label>
      <input type="text" name="lastname" id="lastname" />
    </div>
	<div>
    <label>Your Email:</label>
      <input type="text" name="email" id="email" />
    </div>
    <div>
    <label>Password:</label>
      <input type="password" name="password" id="password" />
    </div>
    <div>
    <label>Repeat Password:</label>
      <input type="password" name="repeatpassword" id="repeatpassword" />
    </div>
	<div>
    <label>I am:</label>
      <select name="sex" id="sex">
        <option value="sex">Select Sex</option>
        <option value="m">Male</option>
        <option value="f">Female</option>
      </select>
    </div>
  	<div>
    <label>Birthday:</label>
      <select name="month" id="month">
        <option>Month</option>
        <option value="jan">Jan</option>
        <option value="feb">Feb</option>
        <option value="mar">Mar</option>
        <option value="apr">Apr</option>
        <option value="may">May</option>
        <option value="jun">Jun</option>
        <option value="jul">Jul</option>
        <option value="aug">Aug</option>
        <option value="sep">Sep</option>
        <option value="oct">Oct</option>
        <option value="nov">Nov</option>
        <option value="dec">Dec</option>
      </select>
    
      <select name="day" id="day">
        <option>Day</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
      </select>
      
      <select name="year" id="year">
        <option selected="selected">Year</option>
        <option value="1900">1900</option>
        <option value="1901">1901</option>
        <option value="1980">1980</option>
        <option value="1987">1987</option>
        <option value="1995">1995</option>
        <option value="2000">2000</option>
      </select>
  </div>
  <div class="actions">
      <input type="button" name="submit" id="submit" value="Join Now" onclick="register()" />
   </div>
</form>
</div>
</body>
</html>

 

register.php

<?php

$firstname = strip_tags($_POST['firstname']);
$lastname = strip_tags($_POST['lastname']);
$email = strip_tags($_POST['email']);
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$sex = strip_tags($_POST['sex']);
$month = strip_tags($_POST['month']);
$day = strip_tags($_POST['day']);
$year = strip_tags($_POST['year']);

$dob = date($year."-".$month."-".$day);
/*
echo $firstname.'<br>';
echo $lastname.'<br>';
echo $email.'<br>';
echo $password.'<br>';
echo $repeatpassword.'<br>';
echo $sex.'<br>';
echo $month.'<br>';
echo $day.'<br>';
echo $year.'<br>';
*/
if($firstname&&$lastname){
	if($email) {
		if (ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
   				
			$connect = mysql_connect("localhost","user","user");
   				mysql_select_db("facebook");
   				$emailcheck = mysql_query("SELECT email FROM users WHERE email='$email'");
   				$count = mysql_num_rows($emailcheck);
   				
   				if($count == 0){
   					if($sex=='m'||$sex=='f') {
   						if($password&&$repeatpassword) {
   							if($password==$repeatpassword) {
   								if(strlen($password)<25 && strlen($password)>=6) {
   									$password = md5($password);
   									//echo $password;
   									$reg = mysql_query("INSERT INTO users VALUES ('','$email','$password')");
   									$_SESSION['email'] = $email;
   									echo "Welcome ".$firstname." ".$lastname.", You are now registered.";
   									//header("Location: /main.php");
   								}
   								else 
   									echo "Length of password must be between 6 and 25 characters.";
   							}
   							else 
   								echo "Password and Repeat password do not match!";
   						}
   						else 
   							echo "Please fill in password and repeat password fields!";
   					}
   					else 
   						echo "Pease select your gender!";
   				}
   				else 
   					echo "user with this email address already exist! Please choose another email.";
  			}
  			else 
  				echo "Invalid email address!";	
	}
	else 
		echo "Please fill in your email address!";
}
else 
	echo "Please fill in firstname and lastname.";		
?>

Link to comment
Share on other sites

In order to redirect using php you'd have to submit the form and then handle the redirect on submission

if(isset($_POST['redirect']) && $_POST['redirect']) {
header("Location: http://www.google.com");
}

or use javascript to redirect

window.location = "http://www.google.com/"

 

or you have to submit the form

 

can you please tell me how can i use these in my code.

Link to comment
Share on other sites

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.