Elements With name Attributes Going to Server: PHP Convention Only? - javascript

Many places have said that only elements with a name attribute go to the server on page change, and only the element name and its value attribute's value travel between client & server. Is this a PHP feature or is it also present in other scripting languages? For example, is this the case with Node.js, or any of its popular server frameworks like express or grunt? Also, are there ways to send other elements or attributes to the server?
I know that AJAX can cause pretty much anything to go to the server, but this is usually asynchronous, and even when it isn't the info doesn't usually go to the server right when the page is sent. If you have any relevant info on AJAX, though, please share it.

When you give an element a name attribute, the browser will send the form data in the body of the request (if using POST) or in the query string (if using GET). This happens no matter what language or framework you use.
name is the only attribute that does this - it will not work with an id - but you can also do this with AJAX, by passing a querystring to XMLHttpRequest.send (if you're using jQuery, read up on jQuery.post). Requests via AJAX are, to the server, identical to requests from the client. If you send data in an AJAX POST request, it will be identical to a request via an equivalent form to the server. This is the same whether you're using PHP, Node.js, or any other web framework.
Helpful references:
http://en.wikipedia.org/wiki/POST_(HTTP)
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data

Related

When or How should we pass the params to the URL?

Using PHP, HTML and Javascript.
With PHP we are using a MVC Framework.
I have a fundamental question regarding web communication, that perhaps someone could clear some things here.
REST apart, and taking only "post" and "get":
Let's say that, if we do:
http://blabla.com/companyA/income/
It will list all income of a given company.
If we do:
http://blabla.com/companyA/income/2010/
It will list all income on 2010 for that company.
And so on.
Now, I wish to allow the user to, from a html form, select some values, and according to those values, return, from the server, the appropriate data.
How does this work?
a) Should we concatenate the URL string on the client side, (form action) and send it to the server side?
b) Our, should we send the params to the server side, and it returns the URL?
Anyway will work? Only one way will work? What are the consequences of those paths? Is there a third possibility?
a) Should we concatenate the URL string on the client side, (form action) and send it to the server side?
That's how it's always been done historically and it still works fine. It's the equivalent of constructing the complete HTML by hand, but using PHP (or any server side language) to help you remove manual labor, by basing the HTML on the data you have easy access to.
b) Our, should we send the params to the server side, and it returns the URL?
That's unnecessary, you already have all the data ready so going with a) is more solid since it lets the client become a bit dumber, by excluding the render-url-from-arguments logic.
Is there a third possibility?
Yes. Since you already expose lists of resources at /companyA/income/2010/ (perhaps through JSON), you could serve an empty page from the server side, trigger an ajax call to your backend and generating the list dynamically on the client side. This brings a bunch of things to think of, some are:
How is SEO affected when (google) bots request empty pages?
Can we reuse the rendering logic for different views, without reloading the empty page?
You could read up on SPA, or, Single-page applications.
You should make url when processing with PHP, so you can pass ID or any other information from server. Making URL from client side requires additional processing.
<form action="companyA/income/<?php echo (int)$years; ?>">
...
<form action="<?php echo $this->makeUrl('companyA/income', $years); ?>">
...

How can I process POST data sent from one HTML to another? [duplicate]

I want to pass some textbox value strictly using POST from one html page to another...
how can this be done without using any server side language like asp.net or php
can it be done using javascript??
thnx
You can't read POST data in any way on javascript so this is not doable.
Here you can find similar questions:
http://forums.devshed.com/javascript-development-115/read-post-data-in-javascript-1172.html
http://www.sitepoint.com/forums/showthread.php?454963-Getting-GET-or-POST-variables-using-JavaScript
This reading can also be interesting: http://en.wikipedia.org/wiki/POST_%28HTTP%29
This expecially suggests why this answer (wikipedia is the source):
GET
Requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect.
(This is also true of some other HTTP methods.)[1] The W3C has
published guidance principles on this distinction, saying, "Web
application design should be informed by the above principles, but
also by the relevant limitations."[10] See safe methods below.
POST
Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request.
This may result in the creation of a new resource or the updates of
existing resources or both.
POST data is added to the request. When you do a GET request the data is added to the url, and that's why you can access it through javascript (and that's why it's not parsed and you have to do it manually). Instead, POST send data directly into the http requests, which is not seen in any way by the html page (which is just a part of what is sent through the http request).
That said, only server side language will receive the full HTTP request, and definitely you can' access it by javascript.
I'm sorry but that is the real answer

Asp.net access sessionstate from JavaScript

Our system using HttpContext.Current.Session("Client") to store the current user info.
One property in the session is a roleID i.e. CType(HttpContext.Current.Session("Client"), Client).RoleId
By checking the value of RoleId, the system can identify whether the user can access a couple of pages.
I've validated it in the server-side. But for the easiest way to present the Notice Message I think is using JavaScript.
So is it possible to get the session value in JavaScript (even in a external JavaScript)?
How about Cookie? What is the drawback for adding Cookies for an existing system?
And any other suggestions if you have.
Thx
Yes, I did the validation in server side. Later again, I'll add restrictions in DBs as well.
Result:
I used webMethod inside a web service, caz it is a Master Page.
Thanks for you answer.
but another issue raised:
Trigger/Prevent page event by using asynchronous webmethod return value in JavaScript
please give me some advise on that question as well, thx.
You could do it as a cookie, but it would slow down your round trip for every resource. Hence, I don't recommend this approach.
One option is to have a dynamic page that returns a javascript object in global with the appropriate variables printed out. You then could just include it as a standard script tag.
Another approach is to make an AJAX call.
Keep in mind, you should still always validate the base request and never trust the client.
Sending roles to the client and using JavaScript for business logic based upon these roles is a security risk. Users (hackers) know how to manipulate client-side code to gain access to things they're not supposed to.
I recommend sending down only the content the user has access to or use AJAX to retrieve the content dynamically from the client.
But to answer your question, no, you cannot retrieve session data directly from the client.
You can make ashx page or WCF service and call that with javascript. But don't return roleID and check that ID on client, instead just return true / false if user has access. Use jQuery ajax call to ashx or WCF service, you should find tons of examples on google

request parameters ordering undefined [in multipart/form-data or in general] - what to do?

I am writing a web application that submits a form (one of its fields is mulitpart/form-data, so obviously POST must be used and not GET, since the files might be really big). One of the fields is kinda transaction/upload_id and the other is obviously the file contents. While uploading, a progress bar must be displayed.
The known fact says that the order of parameters is undefined in general, meaning that any of (file content / upload_id) might come first.
Is there any acceptable / recommended way to cause the browser to send the upload_id before sending the file content?
Is it a considered a correct implementation - to expect the upload_id to come first OR there is a better / most common / more correct way to handle the problem? In that case - it would be fantastic to hear some details.
Update: my server-side language is Java/Servlets 3.0
Well, the better answer (without utilizing filters) would be to publish the upload_id(s) as a part of the URL (after '?'), even when issuing a POST request. In that case, they will be always processed ahead of files' contents.
Using servlets as well, and in my case I wanted to run my CSRF filter in my servlet before I started streaming the file: if the filter failed, I can kill the request before I've uploaded my 20gb video file, as opposed to the default PHP implementation where the server only hits your script AFTER its parsed the entire request.
Its been a bit of a hack on my part, but in the couple of cases I've had to do this I've cheated and put the non-file request parameters into the URL and in every case (using pretty much every browser I've tested with) an Iterator over the request parameters on the server (I'm using commons fileupload in streaming mode) has received the non-file request parameters first before the file data was received. Somewhat fragile, but not unworkable.
I'm assuming that if you order your request parameters with the file <input> as the last item you'll get the same behavior.
You shouldn't have to worry about the order in which the parameters are sent. If so, then your server-side code is very brittle.
A multi-part request will contain the field name of every form field that is passed in. Use the name to reference that field regardless of the order it was sent in.
If you are parsing the post body by hand, I suggest you look at existing projects like Apache FileUpload which abstract that away.

Is it possible to send non POST data with javascript?

I'm trying to submit a postscript print job directly to printer on port 9100. I tried submitting a form directly to the IP and port, but it includes a lot of header information which obviously messes it up.
Is there a way to do this with jQuery or AJAX (or some other term I don't know about)?
You can't do it with Javascript, it'll only do HTTP requests (e.g. POST/GET), which means you get the full HTTP headers included.
Once WebSockets get more widespread, you could use those and send arbitrary data without the HTTP overhead/payload, but at present, that's only in 'bleeding edge' browsers.
This means you're stuck using a Flash or Java applet at present.
You can create a proxy php script which will accept your POST data from the form, format this data and send it to the printer
If you'd like to submit data to this script in background - please see my answer to the following post:
JavaScript: How do I create JSONP?

Categories

Resources