Page 1 of 1

Setting default values in a PHP Form fields

Posted: Fri Feb 06, 2009 4:26 pm
by Mizhad
Hello every body
I have a form, when I submit it it is saved in the database and I return to the form page again and all fields are cleard, I want to keep some values in the fields when I return to the form page... How can I do that? Will be thankful for an answer..

/Mizhad

Re: Setting default values in a PHP Form fields

Posted: Fri Feb 06, 2009 4:39 pm
by Drachlen
You have a lot of options depending on your needs.

Do you want to re-populate some of the form with the same data you just entered? Or with preexisting data from the database?

Let's say for example you have a form that posts to itself, you could then take the now-existing $_POST values and directly print them into the form output of your page.

Code: Select all

<form method="post" action="thispage.php">
<input type="text" name="field1" value="<?php echo $_POST['field1']; ?>">
<input type="submit">
</form>
If you wrote this into a document and saved it as "thispage.php", you should be able to infinitely submit random text and find that it's constantly repopulating itself with the same values.

You can apply this same concept using a session or even extracting specific information from your database.

If you can explain your exact scenario more specifically, I can provide a solution better catered to your needs.

Good luck.

Re: Setting default values in a PHP Form fields

Posted: Fri Feb 06, 2009 5:06 pm
by Mizhad
Thank you so much Drachlen for your quick reply.

So great, this is exactly what I wanted to know,
<input type="text" name="field1" value="<?php echo $_POST['field1']; ?>">
This really resolved problems with INPUT tags, but what about SELECT tags? I tried the same as INPUT but does not work. My scenario is as following:

A user fills a searching form with almost 10 criteria, then clicks submit button, results are fetched from the database and pushed below the searching form, and here I want to keep the criteria were entered when submit button was clicked. This is my scenario.

Re: Setting default values in a PHP Form fields

Posted: Fri Feb 06, 2009 11:26 pm
by Mizhad
Does any body know how to resolve this? I mean setting the previous vlaue in a SELECT control when I return to the search form?

Re: Setting default values in a PHP Form fields

Posted: Sat Feb 07, 2009 5:39 pm
by Drachlen
Hi Mizhad, Sorry for the delayed response.

Here is a solution for retaining the select information.

I might be over-thinking this, but I poked around and it doesn't look like there is just a universal solution to setting the default option for a select.

The method used is this:

Code: Select all

<option label="x" value="x" selected="selected">x</option>
As you can see, the selected="selected" portion tells the browser to select that option by default. However, in a situation where you want to have different options selected based on user input, we require a loop to iterate through to specify what option to select.

In this example, $fruitArray contains a few different selections. The first option apple is regarded as 0. (then banana as 1)

Once the form is posted, the variable $_POST['fruit'] will contain an integer anywhere between 0 and 2. It's also possible that a user could post other data, but that will not affect the outcome of the script.

The script loops through the fruitArray until it finds a match between the current iteration of the loop, and the current posted value. If there is none, it will simply use default selection.

Name this file "thispage.php" to test:

Code: Select all

<?php
    $fruitArray[] = "apple";
    $fruitArray[] = "banana";
    $fruitArray[] = "cabbage";
?>
 
<form method="post" action="thispage.php">
<input type="text" name="field1" value="<?php echo $_POST['field1']; ?>">
<select name="fruit">
<?php
    for($i=0; $i<count($fruitArray); $i++)
    {
        if( $_POST['fruit'] == $i )
        {
            echo '<option label="'.$i.'" value="'.$i.'" selected="selected">'.$fruitArray[$i].'</option>';
        } else {
            echo '<option label="'.$i.'" value="'.$i.'">'.$fruitArray[$i].'</option>';
        }
    }
?>
</select>
<input type="submit">
</form>
I hope this helps. If you need additional explanation to wrap your head around it, let me know.

Re: Setting default values in a PHP Form fields

Posted: Sat Feb 07, 2009 11:26 pm
by Mizhad
Hello Drachlen
I'm really so appreciative for the time and efforts you have done to resolve my problem, very kind of you.

Now if you do not mind I have another question: To retrieve data in my database I as normal need to build a WHERE clause, I built mine and works very nice, but I want to show you the code and please if you have time take a look on it and let me know if this algorithm I use is good or there are better ones. I thank you so much. By the way, JRequest::getVar() is equivalent to $_POST[].

Code: Select all

    [color=#FF0000]function [/color]_buildWhereClause()
    {
        $RequestList = [color=#FF0000]array[/color](   "Pass_Number" => JRequest::getVar('reqpassnum'),
                                "Visa_Arrival_Date" => JRequest::getVar('reqarrivedate'),
                                "Visa_Depart_Date" => JRequest::getVar('reqleavedate'),
                                "CurNationality" => JRequest::getVar('reqcitizenship'),
                                "Visa_Type_Of" => JRequest::getVar('reqvisatype'),
                                "Visa_Travel_Purpose" => JRequest::getVar('reqtravelpurpose'),
                                "Visa_Status" => JRequest::getVar('reqvisastatus')
                            );
 
        $ReqList = array();
        [color=#FF0000]foreach[/color]($RequestList as $key => $value)
            [color=#FF0000]if[/color](!empty($value))
                $ReqList[$key] = $value;
 
        $WhereClause = '';
        $Count = 0;
        [color=#FF0000]foreach[/color]($ReqList as $key => $value)
           [color=#FF0000] if[/color](!$Count)  // This control is just to avoid AND in the beginning of the sentence.
            {
                $WhereClause .= ' '.$key.' = "'.$value.'" ';
                $Count ++;
            }
            [color=#FF0000]else[/color] $WhereClause .= ' AND '.$key.' = "'.$value.'" ';   
            
            $WhereClause .= ' ORDER BY Last_Name ASC;';
        [color=#FF0000]return[/color]($WhereClause);
    }