2

My question is simple, I'm using AJAX and i want to redirect the user to another page if the user fill up the registration form properly, however if the user failed to match his/her password. i want to show an error message.

here is my PHP code:

if (isset($_POST['password']) && isset($_POST['retype_password']))
{
  $password = $_POST['password'];
  $retype_password = $_POST['retype_password'];
  if(!empty($password) && !empty($retype_password))
  {
     if($password == $retype_password)
     {
       header("Location: anotherpage.php");
       exit();
     }
     else
     {
        echo 'password does not match';
     }
  }
}

here is my ajax:

var frm = $('#frm_register');
    frm.submit(function (e)
    {
        e.preventDefault();
         $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                $('#error').text(data);
            }
        });
        return false;
    });

The problem here is that it doesn't redirect to another page unless i refresh the page.

3
  • where you using ajax and what is issue? Commented Sep 12, 2014 at 5:50
  • I feel you should sent success & fail status from php code and handle it on front end accordingly. Commented Sep 12, 2014 at 5:51
  • it doesn't redirect the page unless i refresh it. wait i'll show the ajax. Commented Sep 12, 2014 at 5:51

4 Answers 4

2

You can simply use javascript to redirect to the page like below:

if (isset($_POST['password']) && isset($_POST['retype_password']))
{
  $password = $_POST['password'];
  $retype_password = $_POST['retype_password'];
  if(!empty($password) && !empty($retype_password))
  {
     if($password == $retype_password)
     {
       echo true;
     }
     else
     {
        echo 'password does not match';
     }
  }
}

And for redirecting, you can use:

var frm = $('#frm_register');
    frm.submit(function (e)
    {
        e.preventDefault();
         $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                     if(data === true) {
                      window.location = 'Your url path here';
                     } else {
                      $('#error').text(data);
                    }
            }
        });
        return false;
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of header("Location: anotherpage.php"); just do echo '1' and in your AJAX call, if data['responseText'] == '1' than just do a document.location.href = 'anotherpage.php'

Comments

0

JavaScript does not work with header() as it is browser-based language whereas PHP communicates directly with the Server. The best solution would probably be to return an error flag and message json_encode()'d.

If you return 0 (error) then display a message.

If you return 1 (success) redirect with JavaScript to a URL passed by . That way you will be able to easily change the new URL should anything change in the website.

JavaScript

var frm = $('#frm_register');
    frm.submit(function (e)
    {
        e.preventDefault();
         $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            dataType: 'json',
            data: frm.serialize(),
            success: function (data) {
                if (data.r == 0){
                    $('#error').text(data.m);
                }
                if (data.r == 1){
                    document.location.href = data.m;
                }
            }
        });
        return false;
    });

PHP

if (isset($_POST['password']) && isset($_POST['retype_password']))
{
  $password = $_POST['password'];
  $retype_password = $_POST['retype_password'];
  if(!empty($password) && !empty($retype_password))
  {
     if($password == $retype_password)
     {
       echo json_encode(array(
           'r' => 1,
           'm' => 'anotherpage.php'
       ));
       exit();
     }
     else
     {
       echo json_encode(array(
           'r' => 0,
           'm' => 'Passwords do not match'
       ));
       exit();
     }
  }
}

1 Comment

I can't implement this sir, because i don't know json, my prof isrequiring us to explain how it worked.
0
var frm = $('#frm_register');
    frm.submit(function (e)
    {
        e.preventDefault();
         $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
              if(data) {
                winlow.location = data;
              }
            }
        });
        return false;
    });

In your action page just echo the link where you wanna redirect if you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.