Hi

I have an index.php page with two radio buttons on a form that control the visibility of certain div’s on the form. If a user leaves the index.php page, without hitting the form submit button, by selecting a menu item, which loads another page on the same site, then hits the browser back button I want the form to re-display as it did at the time the user left the indx.php page, including any data the user may have entered.

One way to control this may be if the user selects the first radio button a php session variable is set to 1. If the user selects the second radio button the php session variable set to 2. When the user leaves the page then returns, code in the index.php page header links a CSS file based on the php session variable. The linked CCS file displays the proper elements of the form.

What code to I need to pass a variable to the php session variable based on the selection of the radio buttons?

Is there a better way to do this?

    I would think AJAX would be the best way. As you could use event handlers to run a javascript function that sends each fields data to some php code that could store the data in the session.

    Also you could use javascript to store the data in a cookie, with the same technique.

    Another option would be to use javascript to detect all onclick events so that on links instead of letting the next page load it would submit the form to the url in the link and of course all pages would include the file that could process the form data and store it in the session. This idea does have a short coming in that if the user click the back or forward browser button the data would be lost.

      Thanks Kirk

      What would the ajax code look like?

      I tried cookies. I could get a cookie written no problem, but have problems with the php finding and reading the cookie.

        If you are not using a javascript framework such as prototype, jquery or YUI, have a look at http://www.w3schools.com/ajax/default.asp. Else, check the documentation for your particular framework.

        While I agree that ajax would be a good way to deal with this, and especially superior in regards to user experience, the simplest way would be to autosubmit the form when a radiobutton changes

        <form ...>
        	<div|fieldset or other block element>
        		<input type="radio" name="..." value="..." onchange="this.parentNode.parnetNode.submit();"/>
        		<input type="radio" name="..." value="..." onchange="this.parentNode.parnetNode.submit();"/>
        	</div|fieldset>
        </form>
        

        You should of course still provide a submit button for users who don't use javascript. Otherwise they have no way of changing their settings.

          If you are familiar with javascript using AJAX will be fairly easy. I would say just use google to find a tutorial

          On working with cookies in PHP you will want to look at the $_COOKIE [man]superglobal[/man]

            Write a Reply...