why can a parameter sent with GET survive several requests? - html

In my JSP file I have a HTML form that sends data via GET to my servlet.
<form method="GET">
<input name="cmd" type="hidden" value="firstValue"/>
.....
</form>
If I now trigger a different form having also a variable called cmd, but using POST, the old cmd value will stay in the parameter list I can read out in my servlet like this: request.getParameterValues("cmd").
<form method="POST">
<input name="cmd" type="hidden" value="secondValue"/>
.....
</form>
Why can the GET parameters survive several requests? Where/how are the they stored in contrast to the POST parameters in the servlet?

When not specified, the form's action URL defaults to the current request URL as you see in browser's address bar, including the query string. If you'd like to avoid that, explicitly specify the action URL.

Related

Sending additional parameters via form using POST: hidden fields or query parameters

I have some HTML forms and I need to provide additional parameters which the user will not fill up in the form UI. I understand I can do it using query parameters or hidden form fields. Examples :
a form whose data must be submitted to the server together with an option chosen in the previous web page
Ex: URL /createImage?option=option_value (accessible via GET by user) would contain the HTML code below
Query parameter
<form action='/createImage?option=option_value'>
<input type='textfield1' name='var1'>
</form>
Hidden field
<form action='/createImage'>
<input type='hidden' name='option' value='option_value'>
<input type='textfield1' name='var1'>
</form>
(in this case, I suppose a third solution would be to use a HTTP session variable instead)
a fake form used to associate a POST HTTP request with a image.
Query parameter
<form action='/createContainer?image=image1'>
<input type='image' src='container.png'>
</form>
Hidden field
<form action='/createContainer'>
<input type='hidden' name='image' value='image1'>
<input type='image' src='container.png'>
</form>
(in this case, I suppose a third solution would be to use a standard img HTML tag and use Javascript to send the POST request to the server on its click() event)
Are there any upsides/downsides in each one of those approaches or is it just a matter of personal preference?

HTML form does not submit correctly

I am using Laravel for my back end.
This does not work
<form action="/promo/update" style="background-color: #fafafa" method="GET">
This works
<form action="/promo" style="background-color: #fafafa" method="GET">
Is there something about using a GET and an extra "/"?
.. Is there something about using a GET and an extra "/"?
Short answer? No.
As others have noted, just make sure your routes file is expecting the correct parameters. I'd also suggest reading here to learn about PUT vs POST, and here to learn about form method spoofing.
Generally speaking, an UPDATE endpoint should be a POST or PUT. That's not your question though, so I'll just stick to answering what you have here.
Routes/web.php
Route::get('/promo/update', 'PromoController#update')->name('promo.update');
Blade
<form method="GET" action="/promo/update">
#csrf <!-- remember to pass your token -->
<input type="text" name="foo" id="foo" class="form-control" />
<button type="submit" class="btn btn-primary">Submit Form</button>
</form>
PromoController
public functon update( Request $request )
{
// Process your data
}
Because your route's method (/promo/update) is post and you use get...You can see available routes methods just by run below code in terminal:
php artisan route:list
First, make sure you have made the route in your routes file.
Second...
Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application.
Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the #csrf Blade directive to generate the token field:

HTML form that causes a GET request

Basic question. If I have a form in my HTML (where in my case someone inputs a date), how can I have my users cause a GET request with the contents of that form instead of a POST.
e.g. form entry (e.g date)... so 20190312
what I am trying to achieve is such that AFTER the user submits the form.. the user is the lead to page that has
GET domain.tld/scriptname.php?variable=20190312
and then the system then processes the GET request accordingly.
thank you
Maybe i'm missunderstanding what you are asking.
This can easly be achived using builtin GET method in FORM tag
<body>
<form id="form" method="GET" action="scriptname.php">
<input id="date-txt" type="text" name="date">
<input id="search-btn" type="submit" value="Submit">
</form>
</body>
While filling up above field ad clicking "Submit" form will be submitted and you can see in your url path/or/page/scriptname.php?date=INPUT_FIELD_VAL
for every input in #form with a name, if GET method is used, you'll see a ?name=value in the url
What you describe is the default behaviour of a form. If you don't want a POST request, then don't use a method attribute to set the request type to POST.
<form action="//domain.tld/scriptname.php">
<input name="variable" value="20190312">
<button>Submit</button>
</form>

HTML Form Action: How to fetch text field & create a URL for Action (Method: POST)

I have a form like:
<form method="post" action="http://www.2-myWebSiteUri.com/file1.php" id="login">
<input type="text" name="TEXTBOX1"></input>
<input type="text" name="TEXTBOX2"></input>
<button type="submit" class="button" title="login" name="send" id="send2">Login</button></form>
I would like to perform custom "action=" in FORM using POST (method) however I am not getting how to perform it. Form action should be like, for example:
<form action="http://www.2-myWebSiteUri.com/TEXTBOX1/file1.php" method="post" id="login">
BUT form should collect the text value (TEXTBOX1) from the TEXTBOX1 which user will enter and same should be used as a input value in action URL as given above in the place of TEXTBOX1. Suppose in TEXTBOX1, I have written input value as "pasta" & submitted via button, then action url should be like:
<form action="http://www.2-myWebSiteUri.com/pasta/file1.php" method="post" id="login">
or if input value is noodles then action url should be like
<form action="http://www.2-myWebSiteUri.com/noodles/file1.php" method="post" id="login">
upon submission.
I've small authentication based scripts in different directories of domain
www.2-myWebSiteUri.com and I am trying to create a single form so that all can be logged in using a single form. User will just enter directory name "such as pasta and noodles as given in above example". This form will be hosted on another domain www.1-myWebSiteUri.com and when user wants to make login any of the scripts then he will open:
www.1-myWebSiteUri.com/login.php and this is the same login form for which I want to have custom actions.
Actually, I am trying to use directories as a category of games which host few online games. Categories will not be fixed and it will be added randomly, so i want to keep TEXTBOX instead of a Drop Down. We may halt few features in the game for different users. You can take this as a different directories with different games which can be accessed only via authentication. I just want to post all the credentials using a single form instead of 100 forms for each game and it will be very tough to share all these login links with users.
I have tested couple of codes including javascripts however none of them are working. I will be more then happy if any of you assist me in this regard.
No, that will not work.
You need a middle-ware script. Your description is lacking detail but this may help.
Change action destination to middle.php
<form method="post" action="http://www.2-myWebSiteUri.com/middle.php" id="login">
<input type="text" name="TEXTBOX1"></input>
<input type="text" name="TEXTBOX2"></input>
<button type="submit" class="button" title="login" name="send" id="send2">Login</button></form>
middle.php
Value of TEXTBOX1 is inserted into url as $txt1.
<?php
$txt1 = trim($_POST['TEXTBOX1']);
$txt2 = trim($_POST['TEXTBOX2']);
include("http://www.2-myWebSiteUri.com/$txt1/file1.php");
?>
Depending on values of TEXTBOX1 you may need to add urlencode:
$txt1 = urlencode(trim($_POST['TEXTBOX1']));

How do I use Sinatra and Sequel to get an array from a checkbox HTML form?

Here's my form:
<form action="/results" method="post" name="checkbox_form">
<input type="checkbox" name="item" value="Bacon">Bacon (1 lb., sliced)<br>
<input type="checkbox" name="item" value="Eggs (dozen)">Eggs (1 dozen)
<input type="submit" value="Calculate">
</form>
If the user checks off both the 'Bacon' and 'Eggs (1 dozen)' boxes, how do I use Sinatra and/or Sequel to return this array:
["Bacon","Eggs (dozen)"]
As you can see, the array elements are the form's 'value' elements.
As a side question: How does Sinatra treat checkbox forms anyway? I couldn't find any good info in the Sinatra manual.
You don't actually need Sequel to receive an item from a post request. Sequel is a database, so you would save the returned item to the database after receiving it from your post. So, you don't actually need it in order to return an array in this case.
Also, forms and checkboxes are not specific to Sinatra but are specific to HTML.
When you submit the form as a post request, it is sent back to the server and Sinatra where it can be handled. Sinatra provides a params method which contains a hash of the values of the form that was submitted.
In the example below, I use a special syntax (item[]) to provide the values named item as an array within the params hash. For example, this is what the params hash returns {"item"=>["Bacon", "Eggs (dozen)"]} with both checkboxes checked.
require 'sinatra'
get '/results' do
erb :results
end
post '/results' do
params[:item].inspect
end
__END__
## results
<form action="/results" method="post" name="checkbox_form">
<input type="checkbox" name="item[]" value="Bacon">Bacon (1 lb., sliced)<br/>
<input type="checkbox" name="item[]" value="Eggs (dozen)">Eggs (1 dozen)<br/>
<input type="submit" value="Calculate"/>
</form>
The get block receives a get request from a user's browser and handles it. In this example, it returns an HTML form to the user.
The post block receives a post request from a user's browser after they have submitted the form. The user is shown the response from the server which in this case is an array of the checkbox values.