Jump to content

Form submission dependant on option selected


massimassi

Recommended Posts

Hello,
I need to change the "name" part of the <input> of a form accordingly to an option selected from a dropdown.
Please refer to the code to understand what I mean.
The form is something like this, with a submit onchange


Code:
<form id="myform" method="a_method" action="an_action">
    <select class="field" name="name_of_select" onchange="$('myform').submit();"
    <option value="1">choise1</option>
    <option value="2">choise2</option>
    </select>

<input type="hidden" name="this_name_changes_when_option_selected_changes">
</form>

I'm not a programmer but I'm willing to learn as I need to make it work, but please explain things to me in a human understandable waysmile.gif
Thanks a lot.
mass

Link to comment
Share on other sites

I think you need to explain what your overall goal is, because that sounds like a really weird thing to do.

 

Hello.

I have a CMS that can be customized throught template changes, the form I want to modify in fact is part of a template (smarty).

 

The "Original" code is something like this:

Code:
<form action="" id="zip" method="post">

        <select class="field" name="set_zip_filter_distance" onchange="$('zip').submit()">

            <option value="">Distance</option>

            <option value="1">1km</option>

            <option value="5">5km</option>

            <option value="10">10km</option>

            <option value="15">15km</option>

.....and so on.....

        </select>

        <input type="hidden" name="submit_zip_filter"/>

 

        <!-- BUTTON1---<input type="submit" name="submit_zip_filter" value="submit" class="button" />

         BUTTON2---<input type="submit" name="clear_zip_filter" value="reset" class="button" />-->

</form>

The parts in bold are the ones I added.

At the bottom of it you will noticed 2 lines of code I commented out. They were 2 submit button I'm trying to get rid of, and that's the reason why I want to change the "name part" of the <input> line to change in accordance to the option selected.

The code above works but it only submits, I wanto to add 1 more option (<option value="RESET">RESET</option>) and cause the form to submit as if it was BUTTON2 if "RESET" is selected, and like BUTTON1 if another option is selected.

 

So far I tried among other things, this code

Code:
         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<script type="text/javascript">

 

    if $('#set_zip_filter_distance').change(function(){

    $('#submit_line').attr('name', 'clear_zip_filter');

    this.form.submit();

}

    else

    {

    this.form.submit();

    });

    </script>

...but it doesn't work:(

 

Maybe someone experienced can help me?

Thanks a lot.

mass

 

 

P.S. Jessica, is that a chiwawa the beast is kissing you in your profile's picture?

Link to comment
Share on other sites

It looks like your trying to fool your own website into accepting a submit option because you don't know where the form submission handler is. You should look for the PHP handling of the form and change that portion.

 

I'm actually quite surprised that $('zip').submit() works since it should probably be $('#zip').submit(). You can change the name of an input, but I don't think that's you're best option. If you are dead set on changing the name, jQuery has the .prop method.

 

 

$('item').prop('name', 'Not the best solution');
Link to comment
Share on other sites

My goal is to get rid of the 2 SUBMIT BUTTONS (those that are at the bottom of the script) and submit the form automatically on change of option selected.

 

This is what the code looks like without my modifications:

 

<form action="" method="post">
        <select class="field" name="set_zip_filter_distance">
            <option value="">Distance</option>
            <option value="1">1km</option>
            <option value="5">5km</option>
            <option value="10">10km</option>
            <option value="15">15km</option>
.....and so on.....
        </select>
        <input type="submit" name="submit_zip_filter" value="submit" class="button" />
        <input type="submit" name="clear_zip_filter" value="reset" class="button" />
</form>
 

 

Numerical options should "emulate" BUTTON NUMBER1, then I want to add a new option (something like <option value="RESET">RESET</option>) and use it to emulate the BUTTON NUMBER 2.

 

If there are options different than changing the name of an input field can you please tell me what to do?

Thanks.

mass
Link to comment
Share on other sites

You need to find the code in smarty / PHP that handles the processing of the form after the results have been submitted. Not the HTML form itself. Then you can modify the PHP code to not use buttons but instead rely on something else.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.