Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Best practice to retain form values across postback

Complete PHP novice here, almost all my previous work was in ASP.NET. I am now working on a PHP project, and the first rock I have stumbled upon is retaining values across postback.

For the most simple yet still realistic example, i have 10 dropdowns. They are not even databound yet, as that is my next step. They are simple dropdowns.

I have my entire page inclosed in a tag. the onclick() event for each dropdown, calls a javascript function that will populate the corrosponding dropdowns hidden element, with the dropdowns selected value. Then, upon page reload, if that hidden value is not empty, i set the selected option = that of my hidden.

This works great for a single postback. However, when another dropdown is changed, the original 1'st dropdown loses its value, due to its corrosponding hidden value losing its value as well!

This draws me to look into using querystring, or sessions, or... some other idea.

Could someone point me in the right direction, as to which option is the best in my situation? I am a PHP novice, however I am being required to do some pretty intense stuff for my skill level, so I need something flexable and preferribly somewhat easy to use.

Thanks!

-----edit-----

A little more clarification on my question :)

When i say 'PostBack' I am referring to the page/form being submitted. The control is passed back to the server, and the HTML/PHP code is executed again.

As for the dropdowns & hiddens, the reason I used hidden variables to retain the "selected value" or "selected index", is so that when the page is submitted, I am able to redraw the dropdown with the previous selection, instead of defaulting back to the first index. When I use the $_POST[] command, I am unable to retrieve the dropdown by name, but I am able to retrieve the hidden value by name. This is why upon dropdown-changed event, I call javascript which sets the selected value from the dropdown into its corrosponding hidden.

-------- edit again -------- Alright alright, i see that i need to take a step wayyy back and explain the overall goal :) i apologize for not being very clear to begin with.

My final design would be a page where a user can select a department within our company, to view data for. Once that department is selected (from a dropdown), then I will display more specific data selection dropdowns for: color, size, vendor, style, date, store#, etc... at this point i will also display the sales data for the selected department. Upon selection of any color,size,etc- i will update the sales data results to meet the new criteria

--------- edit ---------- I cannot provide external access to my example, however here is a sceenshot with explination. In the image below, The user would expand the Department dropdown, to pick a department. At this point, the sales data below would refresh according to that department. Then, the user would select a "Group By" option such as Store, and the page/data would refresh to display data grouped by store. Then they could select a color such as black in my example, and the data would show sales for the selected department and color, grouped by store.

In order to do all this however, the page needs to retain the department, color, and grouping dropdown selections every refresh/post...

alt text

like image 216
Adam Avatar asked Dec 30 '10 18:12

Adam


People also ask

What is keep the value in the form in PHP?

PHP - Keep The Values in The Form To show the values in the input fields after the user hits the submit button, we add a little PHP script inside the value attribute of the following input fields: name, email, and website. In the comment textarea field, we put the script between the <textarea> and </textarea> tags.

What is post back in php?

A post back is a particular type of post request. I feel the moderators got a little gung-ho about marking this as a duplicate.


1 Answers

There is no native way to automated postbacks, you have to echo the data from the $_POST array like so:

<input name="id" value="<?php isset($_POST['id']) ? (int)$_POST['id'] : "" ?>" />

Another way to do this is to create a form helper that will dynamically create your input etc for you, and then check the post array for the key, for example:

class FormHelper
{
    public function input($name,$type = "text",$default_value = "",$postback = false)
    {
        return '<input name="' . $name  . '" type="' . $type . 'value="' . $this->_postback($name,$default_value ) . '" />';
    }

    //More Methods


    private function _postback($key,$default = "")
    {
        return isset($_REQUEST[$key]) ? htmlentities($_REQUEST[$key]) : $default;
    }
}

and then you can add will be able to build forms specifying wheather it should contain the previous value.

you would create forms like so:

<form>
    <?php echo FormHelper::getInstance()->input("id","text","Enter Id",true); ?>
</form>

Obviusly creating the singleton method getInstance()

Some links that may help you.

  1. Form validation using PHP
  2. PHP/CSS Form validation
  3. PHP Form Validation

After your update I may know what you mean even thought your post is hard to understand.

If im right you want to do the following:

  • Show a form on your page
  • Let the user fill out the form
  • When the user clicks submit
    • Send the data to the server via Ajax
    • Process the information supplied by the user
    • Generate a new form, or regenerate the old form with errors
    • Send the form back down the pipe to the page
    • Remove the old form replacing the old form with the newly generated one

If the above is correct then this is not a particular feature for PHP but mainly based around javascript

The best way to accomplish the above is to make sure your application is template based, so within your initial page load you show the form like so:

<html>
    <!-- Head, Meta, Etc-->
    <body>
        <?php $this->template->load("templates/forms/specific_form.php"); ?>
    </body>
</html>

This will us usually the way a page is loaded, then on click you want to submit the form via ajax, i would incorporate jQuery for this.

the form would be like so:

<form rel="__postback" ... >
</form>

And then within jQuery you would do the following:

$(document).ready(function(){
    $("form[rel*=__postback]").each(function(){
        var Form = $(this);

        //Send the form with ajax, bin the __PostbackComplete()
        function __PostbackComplete(server_data)
        {
            //Overwite the form using jQuery LIVE!!!!
        }
    });
});

Within PHP you would analyse the data and rep process the form from the templates section.

hope this helps.

like image 125
RobertPitt Avatar answered Oct 04 '22 01:10

RobertPitt