Jump to content

Any way to keep a form state ?


adrianmak
 Share

Recommended Posts

My site have a search

post-2272-0-90954400-1453349338_thumb.pn

Changing value of a select option field will dynmaic query (ajax) in the back-end

post-2272-0-56190200-1453349347_thumb.pn

to show the 3rd select option

post-2272-0-30108700-1453349333_thumb.pn

When search is submitted, the form will reset to initial ( the first screen)

I want to keep the last form selected state, even the form is submitted.

Link to comment
Share on other sites

Here is the link to Soma's post about:

https://processwire.com/talk/topic/2089-create-simple-forms-using-api/

Or topic / hint about session saved values

https://processwire.com/talk/topic/8488-form-api-store-data-in-session-and-repopulate-hint/

I use an extended InputfieldForm object with FormHelper module:

https://processwire.com/talk/topic/7860-module-formhelper/

  • Like 1
Link to comment
Share on other sites

Do u mean rebuild the search form with the populated form variables in the search template ?

So the third select is generated on the fly, so it's not on the page if previous selects are empty?

If so, you may trigger a 'change' event for the second select (using the POST/GET variables, if the exist), and this should create the third select. Basically it's the same as if a user had entered something to the second select.

Link to comment
Share on other sites

Let me state more clearly

The search form started for two fields only, (3rd field is not on the page. i.e it is not hide by css)

1st field, a text input field

2nd field, a select option field.

When  a user select an option in 2nd field, based on the option value, a ajax query to back-end to retrieve options, which will return 3rd select option thru ajax.

the javascript script

$(document).ready(function() {
	toggleFields();
	$('#type').change(function() {
        toggleFields();
    });
});

function toggleFields() {
	var query_value = $('#type').val();
    if ($('#type').val() == '') {
        $("span.search_options").hide();
    } else {
        $.ajax({
    		type: "POST",
    		url: "/query.php",
    		data: { query: query_value },
    		success: function(html) {
    			//console.log(html);
    			$("span.search_options").html(html);
    			$("span.search_options").show();    			
    		}
    	});        
    }         
    //console.log(query_value);
}

query.php

<?php
// ProcessWire bootstrap
include('./index.php');
$out = "";

$query_string = wire(sanitizer)->text($wire->input->post->query);
$categories = wire(pages)->get($query_string)->children();

$out = "<select class='search__select' id='typeoptions' name='typeoptions'>";

foreach($categories as $category) {
    $out .= "<option value='{$category->id}'>{$category->title}</option>";
}

$out .= "</select>";

echo $out;

initial search form

    <form method='post' action='<?php echo $config->urls->root; ?>search/'>
        <input class='search__input' type='text' name='search_keywords' id='search_keywords' value='' />
        <select class='search__select' id='type' name='type'>
            <option value=''>All</option>
            <?php foreach($pages->get(1028)->children() as $category) {
                echo "<option value='{$category->id}'>{$category->title}</option>";
            }
            ?>
        </select>
        <span class='search_options'></span>
        <input class='search__button' type='submit' value='Search' />
    </form>
Link to comment
Share on other sites

Yep, just set the first two selects' value using php (POST/GET), and then your toggleFields function in document ready should do the rest.

Edit: of course you should trigger the 'change' event to make it work, e.g in the end of document.ready function

$('#type').trigger('change');
Link to comment
Share on other sites

Yep, just set the first two selects' value using php (POST/GET), and then your toggleFields function in document ready should do the rest.

Edit: of course you should trigger the 'change' event to make it work, e.g in the end of document.ready function

$('#type').trigger('change');

My code is working. However, I want the form to retain previous user selection states after a keyword search submission

Link to comment
Share on other sites

I'm talking about the same thing. In the video above, you should set the second select's "News" option selected (php), and set "container" as the value of the first input (also in php). These variables (IDs) should be present in POST/GET.

Once you manage to populate these two, trigger the 'change' event to get the third select.

Link to comment
Share on other sites

I'm talking about the same thing. In the video above, you should set the second select's "News" option selected (php), and set "container" as the value of the first input (also in php). These variables (IDs) should be present in POST/GET.

Once you manage to populate these two, trigger the 'change' event to get the third select.

understood ~~~~~~

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...