Include GET Variables in Form Action Attribute - html

I've seen questions like this but none really answer this specific question. Can you include get variables in the form tag's action attribute? For example:
<form action="script.php?id=4" method="get">
<input type="text" name="thing" value="temp">
<input type="submit">
</form>`
This would theoretically result in the get request: script.php?id=4&thing=temp
I'm aware that you can simply do this:
<form action="script.php" method="get">
<input type="hidden" name="id" value="4">
<input type="text" name="thing">
<input type="submit">
</form>`
But I'm curious as to if the previous method is at all possible.

Yes you can absolutely do that! You can append any GET variables in your action attribute and just see your URL,it'll clearly show you the changes.The best use of this strategy is when you want both $_GET and $_POST variables to be there.That is,you can pass variables as GET variables by appending them to action attribute and you can simultaneously pass POST variables through the form(obviously with method attribute set as "post").

Related

Using checkboxes without a value attribute

I Understand the ins and outs of forms, but say for example I have the following code:
<form method="post" action="urltoserver">
<input type="checkbox" name="mycheckbox" value="1">Question</input>
</form>
Here I have set a value for the $_POST array. However, if I simply want to know if the checkbox is selected or not (ie im not bothered about the returned value), is it feasible/common practice to leave off the value attribute all together? Would it just return true/false in this case?
I'm assuming PHP since you mentioned $_POST; correct me if I'm wrong.
if a checkbox is checked (and there is no value assigned) you should (IIRC) receive an on value back (as unchecked boxes aren't sent with the request). So, if you had:
<form method="POST">
<input type="checkbox" name="foo" /> Foo
<input type="checkbox" name="bar" checked="checked"/> Bar
<input type="checkbox" name="baz" checked="checked"/> Baz
</form>
You'd see:
isset($_POST['foo']) == false
isset($_POST['bar']) == true
isset($_POST['baz']) == true
on the server-side when you went to read the check state.
the easiest thing to do though, if you're learning, is submit it to a page with the following code:
<?php
var_dump($_REQUEST);
That will show you, verbatim, what was submitted (based on the form's populated values). From there you can play with how you want to format the form to receive the values you're expecting.

Getting value form input type hidden

I am using input type hidden for getting hidden data in URL. Here is my code
<input type="hidden" name="post_type" value="kcc-product">
<input type="hidden" name="post_type" value="kcc-manufacturer">
When I am using it, I am getting URL as &post_type=kcc-product&post_type=kcc-manufacturer
But I need it like: &post_type=kcc-product&kcc-manufacturer
Just I have to remove **post_type=** from URL. I tried with
<input type="hidden" name="post_type" value="kcc-product">
<input type="hidden" name="" value="kcc-manufacturer">
But, it is not working. How can I remove **post_type=** from URL? Any Ideas?
<input type="hidden" name="post_type[]" value="kcc-product">
<input type="hidden" name="post_type[]" value="kcc-manufacturer">
try this. Both value of post_type will be avilable in $_GET['post_type'] as an array.
Not that I am advocating this approach but ..
why don't you just have one hidden field variable instead of both?
<input type="hidden" name="post_type" value="kcc-product&kcc-manufacturer">
That will produce the url you are after "&post_type=kcc-product&kcc-manufacturer"
First thing is, there should not be two hidden fields with same name.
So, You can remove one extra hidden field and make them as one. And then write as below:
<input type="hidden" name="post_type" value="kcc-product&kcc-manufacturer">
Otherwise, if you use two hidden fields, it considers as two separate parameters and shows as two separate parameters.
Otherwise specify method="POST" in form tag, it will automatically doesn't show in url.
I guess, it might helps.

How to change the url sent to an html form?

Suppose we have:
<form action="xxx">
<input type="checkbox" id = "checkbox" name="checked" value="1">
<input type="submit">Submit</input>
</form>"
When click the submit button, the url should be something like "xxx?checked=1". In this case, I want to append another parameter to the url. I know using hidden value is an option, but do we have a more direct way? Please suggest. Thanks
The fact is the url depends on the method you are using to send the form. This is set on the method attribute on the <form> tag.
The URL of the action can be any valid URL so to add extra attributes like something=1 and other=2 you could set the following:
<form action="xxx?something=1&other=2">
<input type="checkbox" id="checkbox" name="checked" value="1">
<input type="submit">Submit</input>
</form>
submitting the form will now send the GET request xxx?something=1&other=2&checked=1

HTML Form POST re-writing action attribute

I have an HTML form that I'm trying to get to post with part of a query string already inplace, but it keeps re-writing the URL.
<form id="mls_form" action="/index.php?option=com_mls&view=mls" method="get">
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>
Output is:
http://www.mysite.com/index.php?mlsnum=value
It seems really simple, but I don't know why it's re-writing the action attribute.
Use the POST method rather than the GET method. The URL parameters will be sent as specified in the action attribute, and the form inputs will be sent in the post data. Your server script can then read them each using whatever API is appropriate (in PHP, $_GET versus $_POST, or find them all in $_REQUEST).
If you must use GET you can give the additional parameters as hidden input fields.
<form id="mls_form" action="/index.php" method="GET">
<input type="hidden" name="option" value="com_mls" />
<input type="hidden" name="view" value="mls" />
<label>MLS#:</label>
<input type="text" name="mlsnum" />
<input type="submit" value="Go" />
</form>

How do I select form inputs based on user actions and choices?

Firstly, it was hard to include the question inside the title, so don't bash me.
I have a web framework created by someone and I need to learn to use it.
Let's say I have this HTML form:
<form action="servletX" method="get">
<input name="action" value="search" type="submit">
</form>
When the search button is submitted, inside of the servlet, is extracted, based on the word "action" (this is forced by the structure of the framework), what kind of action needs to be made.
One more thing: the action, in this case, "search" is in fact the key for a proprieties file which is read in one of the classes.
My question is:
How can I implement a search feature, using this framework.
I want to have a field where I enter the data based on which the search is made and 2 submitted buttons (2 options)
Something like this:
<form action="servletX" method="get">
<p>Search
<input name="action" type="text">
<input name="action" value="Option1" type="submit">
<input name="action" value="Option2" type="submit">
</p>
</form>
Use buttons or radio buttons instead of generic inputs. They are intended for mutually-exclusive options of the sort required here.