Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP: Filling form variables from previous page 'post'

Options
  • 17-11-2006 1:32pm
    #1
    Registered Users Posts: 463 ✭✭


    I've got an order page that takes in Name, Surname, Phone etc.
    When one/more of the fields is left blank, a message comes back to notify them of the fields left blank and nothing is submitted to the database.

    However, all other fields are cleared and then need to be filled in again.
    Is there any way of retrieving the correctly filled in fields and having them appear pre filled on the "You forgot to enter your name" etc screen to prevent undue work by the user.

    The form is on a file called test.php and is submitted to test.php, so it's all in one page.

    Many thanks


Comments

  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    Have a Googly-moogly for PHP Sessions, they should help you out.


  • Registered Users Posts: 463 ✭✭Emerson


    Thanks Goodshape.
    I was desperately looking for Google direction.
    Sessions was the key word I was trying to think of!

    Cheers


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I tend to use a general shorthand for this. I would use sessions for the case where you want to prompt users for missing information, but not allow them to change the items they alreadu submitted.

    In general I do it as follows:
    <?php
        //Initialise the variables first - this is no substitute for validation!
        $varname = isset($_POST['varname'])? $_POST['varname'] : "";   
    
    ?>
    ...
    
    <input type="text" name="varname" value="<?php echo $varname ?>">
    
    


  • Registered Users Posts: 463 ✭✭Emerson


    Works splendidly.. thanks Seamus..
    Now, for pre selecting drop downs..
    Is there any way?

    <input name="selecttype" type="text" size="20" value="<?php echo $selecttype ?>" /> doesn't maintain the type by the looks of it..


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Emerson wrote:
    <input name="selecttype" type="text" size="20" value="<?php echo $selecttype ?>" /> doesn't maintain the type by the looks of it..
    No, that generally doesn't work.

    It's doable, but easy to not bother doing if you're lazy.

    If you're populating the select from an array (let's say from a database query), I'd do it like this:
    <?php
        $myselect = isset($_POST['myselect'])? $_POST['myselect'] : -1; 
    
    ?>
    
    ....
    
    <select name="myselect">
    	<option value="-1"<?php echo ($myselect == -1)? " selected" : "" ?>>Please Choose...</option>
       <?php
            while($row = mysql_fetch_array($result)) {
    			print("<option value=\"".$row['id']."\"");
    			
    			//Now we check if this record is the one which was previously selected
    			if($row['id'] == $myselect) print " selected";
    			
    			print(">".$row['name']."</option>");
            }
       ?>
    </select>
    
    


  • Advertisement
  • Registered Users Posts: 463 ✭✭Emerson


    seamus wrote:
    No, that generally doesn't work.

    It's doable, but easy to not bother doing if you're lazy.

    If you're populating the select from an array (let's say from a database query), I'd do it like this:
    <?php
        $myselect = isset($_POST['myselect'])? $_POST['myselect'] : -1; 
    
    ?>
    
    ....
    
    <select name="myselect">
    	<option value="-1"<?php echo ($myselect == -1)? " selected" : "" ?>>Please Choose...</option>
       <?php
            while($row = mysql_fetch_array($result)) {
    			print("<option value=\"".$row['id']."\"");
    			
    			//Now we check if this record is the one which was previously selected
    			if($row['id'] == $myselect) print " selected";
    			
    			print(">".$row['name']."</option>");
            }
       ?>
    </select>
    
    
    Since I'm not interacting with the database until all fields are validated, is it possible to use this code to retrieve just the posted variable for the drop down?
    Appreciate your time! Making good progress to getting this complete by today


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Emerson wrote:
    Since I'm not interacting with the database until all fields are validated, is it possible to use this code to retrieve just the posted variable for the drop down?
    Appreciate your time! Making good progress to getting this complete by today
    Yep. For static menus, just use the same format as the initial option, so

    <option value="-1"<?php echo ($myselect == -1)? " selected" : "" ?>>Please Choose...</option>
    <option value="1"<?php echo ($myselect == 1)? " selected" : "" ?>>Option 1</option>
    <option value="2"<?php echo ($myselect == 2)? " selected" : "" ?>>Option 2</option>
    <option value="3"<?php echo ($myselect == 3)? " selected" : "" ?>>Option 3</option>


Advertisement