massimassi — 2013-03-02T17:27:18-05:00 — #1
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
<form id="myform" method="a_method" action="an_action">
<select class="field" name="name_of_select" onchange="$('myform').submit();"
<input type="hidden" name="this_name_changes_when_option_selected_changes">
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 way:)
Thanks a lot.
ralphm — 2013-03-02T17:37:26-05:00 — #2
Hi massimassi. Welcome to the forums.
A couple of things:
- firstly, it's better to user a server side script like PHP for form submission. JS is not reliable.
- secondly, you don't need to switch names values or anything like that. The backend script will just pick up which option was selected.
<label for="business">Business Type</label>
<select id="business" name="business">
<option value="First Choice">First Choice</option>
<option value="Second Choice">Second Choice</option>
<option value="Third Choice">Third Choice</option>
<option value="Fouth Choice">Fouth Choice</option>
massimassi — 2013-03-02T17:48:48-05:00 — #3
Thank you ralph.m.
I semplified the code, which is part of something bigger.
The form is part of a template (smarty) of a commercial cms I bought.
I do need to submit the form with 2 different names, accordingly to the option selected, if you want I can post the whole code but I don't think it's worth to know it.
I don't want to use php as the original code uses already some JS and also because I'm not comfortable with it.
picnictutorials — 2013-03-02T19:35:23-05:00 — #4
I've been doing this for 10 years and I would be hard pressed to edit the code of a pre built CMS.
massimassi — 2013-03-03T06:41:38-05:00 — #5
This CMS is structured to be "easily" customized; the form I want to modify in fact is part of a template (smarty).
So, no problem with code changing.
The "Original" code is something like this:
<form action="" id="zip" method="post">
<select class="field" name="set_zip_filter_distance" [B]onchange="$('zip').submit()[/B]">
.....and so on.....
[B]<input type="hidden" name="submit_zip_filter"/>[/B]
[B]<!--[/B] BUTTON1---<input type="submit" name="submit_zip_filter" value="submit" class="button" />
BUTTON2---<input type="submit" name="clear_zip_filter" value="reset" class="button" /[B]>-->[/B]
The parts in bold arethe 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 the other option are selected.
So far I tried
...but it doesn't work:(.
Maybe someone experienced can help me?
Thanks a lot.