Page 1 of 1

Validating, then redirecting...

Posted: Mon Sep 25, 2006 8:07 am
by christian_phpbeginner
Hi, I have a form, which sends the data to validateForm.php, so it can be validated within the validateForm.php:

Code: Select all

<form method = "post" name = "registrationForm" action ="validateForm.php">
   <input type ="text" name = "txtUserName"></input>
   <input type="password" name = "txtPassword"></input>
</form>
What I want to do in validateForm.php....after the data are being validated and all are valid, I want the user to be redirected to another page. All this time, I am doing it by echoing <script> tag, and embed the javascript within PHP. I am wondering, if there is more efficient and prefered way of redirecting user ?

Note:
I have read the jason's tutorial about redirecting user using header() function. But in my case, the header has been sent to the output. And I am wondering if there is another way to redirect user in my case.

Thank you very much,
Chris

Code: Select all

<?php
   session_start();
   $_SESSION['username'] = $_POST['txtUserName'];
   $_SESSION['password'] = $_POST['txtPassword'];

   /**
    * function to check form validity
    * @return boolean
    */
   function checkValidity() {
     //the logic here...
   }

   $isAllValid = checkValidity();
   
   if ($isAllValid) {
      //redirect user using Javascript
     echo '<script src = "validations.js" type = "text/javascript"></script>';
     echo '<script language = "javascript" type = "text/javascript">';
        echo 'redirectUser("registrationCompleted.php");';
     echo '</script>';     
   }   
?>

Posted: Mon Sep 25, 2006 8:38 am
by CoderGoblin
Rather than use javascript (after all this is in the PHP-Code section) you can use the php header function

Code: Select all

$valid=0;

// Validate if necessary and if valid set $valid=1

if ($valid==1) {
  header("Location:http//www.sitename.com/wherenext.php");
  exit;
}

// output form
The exit is important as the rest of the code following the relocation would normally run which, other than using processing time could cause unpredictable errors or screen flicker. Feyd the "guru" of this board also states that you should include the full path name rather than a relative path name (see Feyd I do sometimes remember :wink: ).

Edit: After a search on this forum.. The reason to use full URL's:
feyd wrote:And always, always use full URLs. Standards strict browsers will be unable to follow relative URLs

Posted: Tue Sep 26, 2006 1:34 am
by christian_phpbeginner
Hi ! Wow, I am so proud to be a member in the biggest PHP forum, I mean, just yesterday I posted this question, and today, it is already in the second page ! So many people come here and ask questions and so many helps, which mean, they trust this community. I feel very safe here.
CoderGoblin wrote:Rather than use javascript (after all this is in the PHP-Code section) you can use the php header function

Code: Select all

$valid=0;

// Validate if necessary and if valid set $valid=1

if ($valid==1) {
  header("Location:http//www.sitename.com/wherenext.php");
  exit;
}

// output form
The exit is important as the rest of the code following the relocation would normally run which, other than using processing time could cause unpredictable errors or screen flicker. Feyd the "guru" of this board also states that you should include the full path name rather than a relative path name (see Feyd I do sometimes remember :wink: ).

Edit: After a search on this forum.. The reason to use full URL's:
feyd wrote:And always, always use full URLs. Standards strict browsers will be unable to follow relative URLs
Thanks CoderGoblin ! Yes, I read feyd's post about it. The reason I didn't use the full path is because I am testing it on my local server. And later, in the webserver, where the application shall be deployed, I would use the full path.

Sincerely,
Chris