How to get value of hiddenfield in another form - javascript

iIhave value in hdnField in form1.aspx . I assign a value to hdnfield in javascript .I want to get that value in aspx.vb in another form, form2.aspx. How can I accomplish this?

If your Form1.aspx submits to Form2.aspx, then you have atleast a few ways to access the value of form fields (including Hidden fields):
The Request.Form property exposes a NameValueCollection containing all submitted form field names as Keys and their values as Values. You could use the syntax Request.Form["fieldName"] to access the value.
If this is ASP.NET 2+ and you used the Cross-page posting technique, you will be able to access field values in the previous page using the PreviousPage property of the Page.
If you use Server.Transfer, you can access values using the Current HttpContext.
If you need more info, you should take a look at Passing values between pages in ASP.NET.

I think your concept of session is wrong. Session is a server-side object, and javascript runs on the client, so you can not directly assign that value to a session. You can, instead, use some AJAX to send it to the server and then add code in the server so the value is assigned.

Hm... you have to think about the difference between serverside and clientside first...
You cant directly access changes you made in the client on the serverside, because you once send a request to server, as a response you get the site diplayed in your browser. As soon as you recieve the request, the server is finished and cant access the site anymore. Its like a letter you send away. As soon as you put it into the mailbox, you cant make changes anymore.
But you could post a new request to the server and add POST or GET peremeters. These can be accessed by the server. The way you send the request doesnt matter... you can send a request using AJAX or simply reload the page.

Related

Passing a Cookie Value Through Form To PHP

I made this post to try to see if there is a different way (or something I'm doing wrong) to get my Cookie with an value into a hidden input which can send the value through a form to my MySQL database.
I am trying to grab a cookie with my "getCookie()" function and then put it in a hidden form to then pass through to my database. There may be simpler ways I am unaware of, but currently this is what I have:
Inside my form I have:
var cookie_val = getCookie("id"); and
$('input[name=id]').val(cookie_val);
When outputting the "getCookie("user")" simply with a document write I get the value that I am looking for, however, in my MySQL database when it arrives it ends up as a value of 0. I am not fully understanding of how
$('input[name=id]').val(cookie_val);
works.
I am fairly confident that my PHP code is not the issue in all of this.

Why is my $_POST empty but file_get_contents('php://input') is not?

When I POST data to my server using a regular old form submit I can pull that data from the $_POST variable, but when POSTing JSON data via AJAX I need to access it via file_get_contents('php://input'). Why is that? In both cases I am using the POST method, are there some explicit headers I should be setting on my AJAX call? I have only ever come up against this problem on the current development server and have never had to use file_get_contents('php://input') before. Is there a server setting somewhere? Can I change this behaviour with a .htaccess?
Add this to the top of your .php file:
$_POST = json_decode(file_get_contents("php://input"), true);
so that the contents will be property decoded and available. After that, you can access individual keys as usual.
So as far as I have been able to find out, this has something to do with the way the data is received by the server - PHP can't natively parse JSON. Sending plain old JSON objects from Javascript to PHP will result in PHP not knowing what to do with the datatype, and thus it won't be able to prepopulate the appropriate global variables.
In order to get around it I added a check to my ajax wrapper function that intercepts the JSON, encodes is as a FormData object and shoots that off to the server instead.

Is there a way to get the form data easily with JavaScript?

I was looking on how to get values of form data that are passed with POST requests and found this answer.
I know you you can get GET query parameters quite easily in JavaScript doing window.location.search but is there a way to do similar for POST request or document.forms is my only option?
To expand on what RUJordan said:
When you do a POST, the information is sent to the server using an entirely different method and does not show up in the URL or anywhere else that JavaScript can get access to it.
The server, and only the server, can see it.
Now, it is possible to take some of the form data and fill JavaScript variables and/or hidden form fields so the server can pass data back down to the client.
If you need more help, you'd be better off opening another question explaining exactly what problem you are trying to solve.
Do you want javascript to see the data was POSTed to load the current page?
JavaScript does not have access to the request body (where the POST content is) that loaded the page. If you want to be able to interact with the POSTed parameters, the server that received the request would need to respond with the necessary data written back out on the page where javascript can find it. This would be done after the form was submitted, as part of the response to that POST request.
Or do you want to know what your page could POST form the forms that are on it?
Inspecting document.forms will let you see what could be POSTed later if those forms were submitted. This would be done before the form was submitted, without a request being made.

Will aspx not see changes made by javascript?

I am changing the contents of a span with javascript. I can see that it has changed with view source.
When I try to get the contents in a c# function right after, it still has the old value.
My guess is that asp is just seeing the html that it sent the client, and that javascript is changing the client html of which asp cannnot see?
this was my only guess on how to explain it. Would this be correct?
Thanks.
Unless what you are changing support POSTing the values back to the server, then your changes will be discarded. <span>'s do not get POSTed to the server.
If you change the value of INPUT's however, then the server will be able to see those new values on the nest page submission POST, provided they are runat=server.
ASPX is SERVER SIDE. Javascript is CLIENT SIDE. The two do not mix unless you make a call back to a webservice, or something similar.
Your <span> change is only on the client, and the server is not aware of it. Look into AJAX as a solution if you wish to communicate interaction back to the server.
Yes, your guess is correct!
Javascript sees only the client side and ASP.NET only the server side. If you make some change on the client side and you want to server side to be aware of that change, you can make use of AJAX.
See Ajax for more info.
In .NET you can use a WebService, a Controller, or a Page for receiving the ajax call.
add runat="server" to your span. this will cause it to get picked up by viewstate and the new value will be available during postbacks.
edit: according to jrummel and testing by mikemanne, a span will not post back even when decorated with runat=server.
I would suggest using an input (as others suggest) instead of a span if you want the value consistently available on the server.

JavaScript: How to read browser's cache of POST data?

Effort
I've read this question, but I still think there has to be a way to do this client side.
Case
I'm submitting a form that has a few inputs. When the form is submitted, the primary key of those inputs is shown on a results page along w/ other data and a different form.
The effect I'm trying to do is if the input-pk is modified, I want it to reload the page (think window.location.reload()), only I want to update that PK parameter's value with the changed value.
window.location.reload takes one of two values (true/false), which distinguishes if it should use browser cache or not. Thus, it seems like it should be accessible, especially since the Firebug::Net plugin shows the param in the HTTP Header.
The form requires 'Post' submissions, which adds a little more complexity.
Alternative
The other thing I've considered is to store the values in a cookie right before submission, which I can retrieve on the next page and then submit another Post; however I'd like to refrain from exposing the data in Cookies.
AFAIK, Javascript does not have access to the POST body. Can't think of an API call for that! If you are using php/.net/ruby, you can encode the POST body as JSON that your JS can use when it's reloaded, can't you?

Categories

Resources