Simple HTML form to pass values to URL - html

I have a simple HTML form with a select element. The purpose is this is to use Wordpress's built in query parameters to allow users to sort the posts in an archive. Date Added, Title, etc.
<form action="" method="GET">
<label id="sortLabel" for="orderby">Sort Songs:</label>
<select name="orderby" id="sortbox">
<option disabled selected>Sort by:</option>
<option value="date&order=asc">Oldest First</option>
<option value="date&order=dsc">Newest First</option>
<option value="title&order=asc">Alphabetical (A-Z)</option>
<option value="title&order=dsc">Alphabetical (Z-A</option>
</select>
<input type="submit" value="Filter" />
</form>
The option values are being passed through to the URL fine, but the URLs are encoding, causing the URL to look like this:
www.example.com/songs/?orderby=date%26order%3Dasc
Instead of this:
www.example.com/songs/?orderby=date&order=asc

This is simply how HTML forms work.
The value attributes are arbitrary text. The browser is sending the form request to www.example.com/songs/?orderby=<value>, where you happen to be setting the <value> to "date&order=asc", "date&order=dsc", etc.
The orderby's value has to make it to the server intact. & and = are reserved characters in a URL's query component, so that is why they are being percent-encoded when the orderby field is added to the URL query, thus allowing the server to properly receive the <value> that the user selected for orderby in the HTML.
To do what you want, you need to treat orderby and order separately in the HTML. I would add a separate <select> for order, eg:
<form action="" method="GET">
<label id="sortLabel" for="orderby">Sort Songs:</label>
<select name="orderby" id="sortbox">
<option disabled selected>Sort by:</option>
<option value="date">Date</option>
<option value="title">Title</option>
</select>
<select name="order" id="sortbox">
<option disabled selected>Order by:</option>
<option value="asc">Oldest First, Alphabetical (A-Z)</option>
<option value="dsc">Newest First, Alphabetical (Z-A)</option>
</select>
<input type="submit" value="Filter" />
</form>
If you wanted to make the order list a little cleaner, you could use client-side scripting to manipulate the display texts of the order options whenever the user selects a different orderby option.

Related

AngularDart - Use Form Action

I am trying to use a form action attribute in AngularDart to redirect to the specified url. In plain html I would do something like this:
<form action="http://localhost:8082" method="GET">
<select name="q">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="9">9</option>
</select>
<input type="Submit" value="Guess">
</form>
How would I implement this functionality with AngularDart?
Thanks in advance
In Angular you would instead do the request in code, because form action="..." would reload the application or even another page which usually is not what you want in a single page application (SPA):
<form (ngSubmit)="onSubmit()"
and then post the form in Dart code like shown in the question in Dart: AJAX form submit
I haven't done this myself in years and don't have a full example, but it should get you going.

Invalidate form if disabled select option is chosen

Is there a way to invalidate a form if value="0" in my select tag is chosen? The reason I did this is to have a default select option show up before a user see's anything.
But even if a user doesn't choose anything, the form is valid.
<form name="add_task_frm" ng-submit="!add_task_frm.$valid || functiontoUse()" novalidate>
<input type="text" ng-model="name" required />
<select ng-model="action" required>
<option value="0" selected disabled>Choose an Action</option>
<option value="description">See Description</option>
<option value="view">View</option>
</select>
<button type="submit">Submit</button>
</form>
I think you could just remove the value from the first option this should make it invalid if the user does not select anything. However I would recommend changing this implementation to use ng-options.
https://docs.angularjs.org/api/ng/directive/ngOptions

multiple='multiple' for <select> is not working

I m working on a application for mail sending and need to use select tag of HTML with showing multiple options (But select only one at a time).
I tried multiple='multiple' but it is showing only one option and arrow disappears form select box.
<select multiple="multiple">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
You may need to include a "size" in addition to the "multiple" attribute, in order to show more than one option at a time. Try something like this:
<select multiple="multiple" size="10">
<option value="...">...</option>
...
</select>
As an alternative, since you want only one option selectable, you could consider using a list of radio buttons. For example,
<input type="radio" name="auto" value="Audi" />Audi<br />
<input type="radio" name="auto" value="Honda" />Honda<br />
...

How can I hide one field of an HTML form?

I have a simple HTML form that I'm using to drive site search for a website I'm creating.
Two of the fields should not be used together, such as "make" and "model" of a car. You wouldn't want someone searching for a "Ford Ram Truck," for instance.
How can I modify my form so that if a certain value in one of the fields is selected, the other field disappears?
Thank you for your help!
<select name="make">
<option value="item 1">item 1</option>
<option value="item 2">item 2</option>
</select>
<select name="model">
<option value="item 1">item 1</option>
<option value="item 2">item 2</option>
</select>
<input name="" type="submit" />
You would need to use javascript and hook up to the change event of the radio buttons.
In your javascript you can set the visibility of any form element to hidden or visible (depending on which you want).
You would still need to validate/check on the server side in order to avoid such a search (since javascript may be off or a malicious user might override your client side validation).
I think Chained Select Menu can solve your problem.

Several simple select boxes to replace a multiple select box in HTML

I'd like to replace a multiple select box like:
<select multiple="multiple" name="options">
<option value="option1">option1</option>
<option value="option2">option2</option>
...
</select>
with an arbitrary number of simple select boxes:
<select name="options1">
<option value="option1">option1</option>
<option value="option2">option2</option>
...
</select>
<select name="options2">
<option value="option1">option1</option>
<option value="option2">option2</option>
...
</select>
Is there any way to send and retrieve via POST an array of select boxes or should I try to access every select box named options(number) until it fails? Seems a bit dirty.
I should be able to submit an action to "delete this select box" or "create new select box" so I need some way to distinguish the select boxes.
Just give the select elements the same name.
HTML forms have no concept of "an array". Every form handling library that handles arrays of input data generates them from a name having multiple values:
foo=bar&foo=baz&aDifferentField=fizzbuzz
This is what a multiple select (named foo) with two values selected will generate (when there is 'aDifferentField' in the form too).
Sometimes there are provisos involved.
Perl's CGI.pm needs the request for the data to be in list context:
my #foos = $cgi->param('foo');
PHP requires the name to end with the characters '[]'
name="foo[]"
foo[]=bar&foo[]=baz&aDifferentField=fizzbuzz
… but it all comes down to the names being the same (although the ids must still be different).
As for the deletion:
<label for="foo5">Group 5</label>
<select name="foo" id="foo5">
<option value="delete_foo5">Delete this group</option>
<option value="1">1</option>
<option value="2">2</option>
</select>