Jump to content

Recommended Posts

Posted

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.

Posted

Populate the two selects with PHP using POST or GET variables (according to your form's method).

Posted

Populate the two selects with PHP using POST or GET variables (according to your form's method).

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

Posted

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.

Posted

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>
Posted

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');
Posted

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

Posted

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.

Posted

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 ~~~~~~

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
  • Recently Browsing   0 members

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