Get is being stripped off an HTML form - html

I have an HTML form on a page. The method is POST, but I'm manually adding a GET parameter to the URL string depending on which button the user clicks. But when the form is submitted, the GET is being stripped off the URL.
I'm really baffled by this. This method has worked many times in the past, and this actual form itself used to work fine. Suddenly, the exact same (formerly working) code doesn't work.
I'm using Firefox Web Developer tools to look at which parameters are passed in the request, and there's no sign of the GET.
<form method="post" target="_blank">
<input type="hidden" name="report" value="abc">
...a couple Select form fields...
<input type="submit" action="reports.php?format=PDF" value="Go">
[<input type="submit" action="reports.php?format=preview" value="Web Preview">]
</form>
Basically, if they click "Go" they should get a PDF, and if they click "Web Preview" they get the same report as a web page; but the "format" parameter isn't received on the processing end of things.
(I've also tried it in multiple browsers.)
Edit to add: I can confirm that the POST data is being received on the processing end. Only the GET is missing.
Edit to add:
If I move the action to the <form> tag, it works. Of course that prevents me from having two buttons that do two things, so it doesn't solve the problem, but it's a clue to what might be happening. This DOES work:
<form method="post" action="reports.php?format=PDF" target="_blank">
<input type="hidden" name="report" value="abc">
...a couple Select form fields...
<input type="submit" value="Go">
<!-- [<input type="submit" action="reports.php?format=preview" value="Web Preview">] -->
</form>

The first place I would look would be to verify that the data is indeed being sent as a POST request. If, for any reason, the browser thinks the form should be using a GET request, it will quite happily delete and replace the hardcoded query string.
What I would suggest is doing a var_dump() on $_POST and $_GET in reports.php to get a definitive idea of what the browser is actually transmitting. There may be some clues in the output.
If the $_GET data is truly missing, I would then check to verify that no one has put a rewrite rule into place on the server to strip query strings off of PHP requests, or even requests to this specific page. As far as I am aware IIS does not normally strip query strings from POST requests, as this would go against well established standards, but it is always possible that rules were manually added to accomplish the same thing.

Solved -- In a submit <input>, an "action" attribute is invalid. I should have used "formaction":
<input type="submit" formaction="reports.php?format=PDF" value="Go">
Somewhere along the way an update to various web browsers must have stopped accepting "action".

Related

Using Perl to update HTML

I am having trouble wrapping my head around getting Perl to work with HTML. I am trying to do something I think is simple, but I cant find anything like it online.
Let's say I have a blank web page that has only a button labeled new, and when I press it, I want to destroy the button and create two new buttons, one that is a submit button, and one cancel that creates the old new button.
How would I go about doing that, without reloading the page?
From my understanding, the original HTML code would look something like this.
<form action="/cgi-bin/switchButtons.cgi" method="POST">
<input type="button" value="new">
</form>
and afterward should look like this.
<form action="/cgi-bin/switchButtons.cgi" method="POST">
<input type="submit" value="submit">
<input type="button" value="cancel">
</form>
On pressing cancel, it should refer back to the first snippet.
You can't do that.
/cgi-bin/switchButtons.cgi is a Perl program on the server. Clicking on one of the form's buttons sends a request message to the server, which runs switchButtons.cgi. The output from that program is the contents of a new web page which is sent back to the client (the browser). Of course that involves loading a new page
You could do it in JavaScript, which is part of the page and runs on the client. You can specify that a button will cause the browser to execute some JavaScript, which could alter the page 9n any way you want. But that doesn't answer your question

Are two or more submit buttons in single form valid html?

A very basic example:
<form action="somephp.php" method="post">
... some stuff
<input type="submit" name="button1" value="value1" />
<input type="submit" name="button2" value="value2" />
<!-- or maybe even a third or fourth one ... hell knows -->
</form>
While I am well aware that this is possible, I have difficulties in finding documentation or specification, whether this is actually valid or allowed. What is common is one submit and maybe a reset button and that is it. I do not have to deal with this aspect on a daily basis, but now and then I do.
https://html.spec.whatwg.org/multipage/forms.html#forms
https://www.w3.org/TR/html51/sec-forms.html#element-statedef-button-type-submit-button
There is a lot about forms, but not whether it is OK to use several buttons. It is indicated through the way a plural is avoided in terms of the submit button, that several for a single form is not what the standard has in mind.
Therefore, it would be really appreciated whether this aspect is something one should worry about and whether it could pose some problems. Links to points of references would be best. Thanks.
from w3.org "submit buttons: When activated, a submit button submits a form. A form may contain more than one submit button."
see link for full text.
One way to see if it's valid is to use a validator https://validator.w3.org
Looks like it's valid HTML5 http://i.imgur.com/fPm9qiq.png
I think it really doesn't matter if a form has a single Submit button or multiple Submit buttons with a post action. When the form method is Post, it simply posts the whole form data back to server.
When it comes to technology like ASP.Net webforms, having multiple Submit buttons makes a difference, as each button can be linked to a different method that has to be executed in on the server-side but if you are using some platform where you just get the decide the form data and it's all upto you to decide how to process it based on the data state then having two submit buttons in a form is obviously redundant.

Angular.js submit form old way

I'm migrating an old html page from jQuery to Angular, and it contains some old-school forms with <input type="submit">. When I enriched my pages with the ng-app directive, the old forms stopped working (I mean when you press the submit buttons then nothing happens).
I searched for this problem and scanned the Angular docs too, but it seems that nobody is submitting forms anymore.
Any suggestions on how to bring those forms to life again without much keystrokes would be appreciated.
Currently the form has following markup:
<form name="form_upload" method="post" action="" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="send" value="Upload">
</form>
All jQuery code I purged and starting fresh with Angular.
UPD:
I guess i now found cause, it's empty action attribute, it seems empty action is not welcomed by standarts, but very convenient to use allowing you to submit form to the current page URL, is there Angular way to do it ?
Going off of the docs: http://docs.angularjs.org/api/ng.directive:form
Angular's philosophy is to minimize data and page reloads, so they don't like "old school forms", but you can get around it by using an action attribute in the form.
Angular is designed with single page applications in mind and avoiding full page reloads as that is going to take longer to do. By using the ngSubmit directive, you can define a function to send over the form data to the server and get a response back much quicker than a full page reload. Bytes instead of Kilobytes.
Use a non-empty action attribute to make Angular submit the form
Fill action attribute with current location (using angular)
In your controller:
$scope.location = $window.location.href
In your HTML:
<form action="{{location}}">
See the plunkr demo.
I've done this. I'm pretty sure I was using jquery to submit the form, but you could probably do it with plain old javascript.
One of the comments on the angular form page suggests this:
<input onclick="javascript:$(this).parent().submit();" type="submit" value="">
http://docs.angularjs.org/api/ng.directive:form

Saving HTML input data to XML

I've searched through the forum and couldn't find a specific answer on my troubles.
What I want to do, is having a simple form, where a user can input information and then, when a button is pressed, get it saved on the server. And if its possible get the XML document saved with the name that specified in the first field.
I need to pull out the information in an app written in action script. Which is, sad to say, the only language I know properly.
I'm trying to get my information from this form (it should be bigger, but right now, I just need a working example):
<form action="" method="post" class="form">
Name: <input type="text" id="name" name="name">
<br>
Activity: <input type="text" id="activity1" name="activity1">
<br>
Activity: <input type="text" id="activity2" name="activity2">
<input type="submit" id="btnSub" name="btnSub" value="Save">
</form>
To be saved in a XML document.
And as said, everything is going to be on the server. If it helps anything.
I can't use ASP as one.com doesn't support this. :/
Hopefully some of you are able and willing to help me out.
I'll try to answer everything thats unclear.
This may be of help:
http://www.hardcode.nl/subcategory_1/article_431-xml-to-string-and-string-to-xml-in-javascript
You can then use an AJAX call in jQuery to send the data to PHP to save it if that is what you are needing.
EDIT: However, I do feel that it is much easier for this to be done server-side. I'm not great with PHP but it's very simple in .NET (unfortunately you explained you can't use this option).

Is the only way of passing POST parameters in HTML through a form?

In HTML, you can send data from one page to another using a GET request in a couple of ways:
http://www.example.com/somepage.php?data=1
...or...
<form action="somepage.php" method="get">
<input type="hidden" name="data" value="1" />
<input type="submit" value="Submit">
</form>
With a POST request though, I've only seen data being sent through form elements like this:
<form action="somepage.php" method="post">
<input type="hidden" name="data" value="1" />
<input type="submit" value="Submit">
</form>
If I only have one parameter I want to send to another page using POST, is there an easier way than wrapping it in a form?
There are only two ways to POST from a browser - a form, or an Ajax request.
Using HTML only, a form is the only way to generate a POST request. You can use server side scripting / Javascript to generate POST requests in other ways, but no other ways to do with plain HTML only.
As you've already discovered, there are exactly two ways to transmit data over the http protocol: GET or POST. There is also a third type of HTTP message called HEAD, but that's really only used to get the meta data around a resource without downloading it and isn't widely implemented.
Obviously both GET and POST are easily accessible through the use of a <form> tag. The GET is also easily accessible by manually adding query parameters to the URL in the form of name-value pairs (foo.html?a=1&b=2).
The beauty and complexity of the POST, however, is that the name-value pairs are communicated from the browser to the web server enclosed within the HTTP request header which is not as easily accessible. The only way to accomplish the POST without using a <form> tag is to manually alter the HTTP request header and add the name-value pairs in yourself.
Also keep in mind that an HTTP server doesn't intrinsically know whether a request (GET or POST) came from a main browser window or an AJAX call. Regardless, the web server will read the request, will decipher if it's a GET or POST request, look for name-value pairs as appropriate, and generate a response.
If you'd like additional detail on how to properly format a POST request you can go to jmarshall.com/easy/http/ or perhaps tcpipguide.com/free/t_HTTPRequestMessageFormat.htm. The definitive resource is always the W3C, but sometimes the RFCs can be terribly confusing for us mere mortals to read.
In HTML only it's with a form.
But you can do it if you play with your server side. Here is a good article that show you how to manipulate the Get to Change it to Post via PHP. This will require you to play with fsockopen... This way to do it will use your parameter ?id=1&param=2 ... and will create a POST request on the server side. You can make it generic, once it setups it will works, but it's a little work to setup everything first.
You can of course always do a GET to a page which contains server-side (or AJAX) logic which will create a POST request (e.g. GET /pageWhichCreatesAPost.py). Very messy of course, but there can be cases where such a work-around could maybe be useful.