Forum

Thread tagged as: Question, Problem, Forms

Auto submit on Perch Forms

Im trying to get my <perch:form> to auto submit with <perch:input type="select">. Anyone done this before? I tried with JS but cant get it to work..

<perch:form id="filter" method="get" name="hyttefilterauto">
        <div class="filter-hytteserie">
        <h2>Velg hytteserie</h2>
            <perch:input type="select" id="hytteserie" options=", Familieserien|familieserien, Panoramaserien|panoramaserien, Oppstuguserien|oppstuguserien" onChange="hyttefilterauto.submit();" />
        </div>

            <div><perch:input type="submit" value="Filter" /></div>
</perch:form>
Erick Green

Erick Green 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Not really an issue specific to Perch forms, it should be the same with any HTML form - see if this helps!

https://stackoverflow.com/questions/7231157/how-to-submit-form-on-change-of-dropdown-list

Something like this?

<perch:input type="select" id="hytteserie" options=", Familieserien|familieserien, Panoramaserien|panoramaserien, Oppstuguserien|oppstuguserien" onchange="this.form.submit()"/>

Cant get it to work. Its inside an forms html template.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, something like that. As I say, this isn't really a Perch question, but maybe someone else can help out.

If you are using jQuery something like this should work:

var formSelect = ('form[id$='filter']  select');

$(formSelect).on('change', function(){
  this.form.submit();
});

Thanks for replying Mike. I cant get any response from the formSelect function, maybe i've done it wrong. This is my code:

<perch:form id="filter" method="get">

        <div class="filter-hytteserie">
            <perch:input class="cs-select cs-skin-border" type="select" id="hytteserie" options="Familieserien|familieserien, Panoramaserien|panoramaserien, Oppstuguserien|oppstuguserien" placeholder="Velg hytteserie" />
        </div>

        <div class="filter-pris">
            <perch:input class="cs-select cs-skin-border" type="select" id="nokkelferdig" options="under 1.600.000 kr|under1600000, 1.600.000 til 1.799.999 kr|1600000til1799999, 1.800.000 til 2.000.000 kr|1800000til2000000, over 2.000.000 kr|over2000000" placeholder="Velg pris nøkkelferdig" />
        </div>

        <div class="filter-storrelse">
            <perch:input class="cs-select cs-skin-border" type="select" id="storrelse" options="under 69 kmv|under69, 70 til 89 kvm|70til89, 80 til 109 kvm|80til109, over 110 kvm|over110" placeholder="Velg hyttestørrelse (BRA)" />
        </div>

        <div class="filter-soverom">
            <perch:input class="cs-select cs-skin-border" type="select" id="soverom" options="1, 2, 3, 4 eller flere|flere" placeholder="Velg antall soverom" />
        </div>

        <div class="filter-valg"><perch:input type="submit" value="Søk" /></div>

        <a href="/hyttevelger.php"><p class="filter-valg-small" style="background: none; color: #1e1e1e;">Nullstill søk</p></a>

    </perch:form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
    var formSelect = ('form[id$='filter']  select');
        $(formSelect).on('change', function(){
        this.form.submit();
        });
    </script>

I think for this to work you need to not a have a submit as part of the form. Try it with this removed and see if it works. In production you will want to remove it using javascript, so if someone is not running javascript they still get the submit button to use

Still doesnt work. If you want to see the page live: https://tet.familiehytta.no/hyttevelger.php. Really appreciate the help.

Drew McLellan

Drew McLellan 2638 points
Perch Support

This isn't specific to Perch - have you looked outside this forum for help?

Hmm, I think my code will only work if there is a single select in the form. I would try a reduced test case with only one select, and maybe just console log the change to test if it is working.

But as Drew says, if you post the question somewhere else e.g. Stack Overflow you will open it up to a lot more people to help you out