Results 1 to 5 of 5

Thread: Form output redirect a variable

  1. #1
    Join Date
    Jul 2011

    Form output redirect a variable

    Alright, it might be an easy answer but for mine self, Im just stuck in it.

    Here is a small script code:

    if(empty($_POST["Username"]) || empty($_POST["password"]))
    echo "Fields has not been set";
    Now what I want is this:
    Add a variable if 1 of those 2 are empty, get the variable echo'd out back on the form page.
    The problem is the undefined index.
    I'm currently not using global arrays and I was wondering how can I move the variable to the previous page.
    However once the page has been refreshed, the variable must not be echo'd out because the user hasn't filled in the form yet "this time".

    I hope I make it as much understandable to be understood.

  2. #2
    Join Date
    Aug 2012
    A few things:

    1. To get rid of the undefined index try using isset, so in your case
    PHP Code:
    if((!isset($_POST['username']) && empty($_POST["Username"])) || (!isset($_POST['password']) && empty($_POST["password"]))) 
    2. You cannot output data (i.e. echo) before a header redirect so that will break your code

    3. To get an error message in your form page, set some variables in the form page that show the different errors and then add a variable to return with the redirect url and check for it back on the form page

    form page:
    PHP Code:
    $msg "All details are required";
    if (isset(
    $_GET['error']) && $_GET['error']==1) { echo $msg; }
    // The above needs putting in the page where you want it to appear 
    The form submit page:
    PHP Code:

  3. #3
    Join Date
    Jul 2011
    Alright! Thanks a lot, that did the trick.
    I though it might be some javascript/ajax or even mvc trick.

    And how I suppose to do if I want to echo out 2 different message?
    Like have the error username to be echo'd out above the username: text and the error password to be echo'd out above the password?

    Must I create another error bookmark page?

    I would like to get the visitors know they entered a wrong username, password, email or something like that.
    Last edited by kensha; 01-28-2013 at 05:22 AM.

  4. #4
    Join Date
    Aug 2011
    set up a simple error system to hold the errors

    PHP Code:
    //Error holder
    $error = array();

    //Username validate
    $error[] = "Please enter a username";
    //Password validate
    $error[] = "Please enter a password";

    //Do a check to see if array contains errors
    if(count($error) > 0){
    $error as $errors){
    "&bull;".$errors."<br />";
    //We have no errors

  5. #5
    Join Date
    Jul 2011
    Hey, after a while figuring it out why all kinda seem to fail I noticed I had to use a different approach and must use the $_SERVER['PHP_SELF']; Function.

    So this is the small scripting part of right now.

    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="text" size="10" name="Username"><br>
    <input type="password" size="10" name="password"><br>
    <input type="submit" value="login" name="logged">
    if(isset($_POST["Username"]) && isset($_POST["password"]))
    if (strlen($username) <3 || (preg_match ("/$pattern/", $username)))
    {echo "Invalid User <br>";}
    echo "Welcome: . $username . ";
    The PHP coding goes to the include.
    Now I got mine variables however they will not work until it has been set.
    For the login page it's fine to have the both text on the bottom of the loging script when the login is failed (I still am going to add the trim() function for the sql injection) however for the registration page I want that the $username variables goes at the bottom of the username input type text field.

    May be some'one got a tutorial on this part and perhaps it's by ajax, java script or some kind of that programming , I then don't mind to study it.

    Thanks a lot in advance.
    Last edited by kensha; 02-07-2013 at 04:26 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center