Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can Javascript access the same GET and POST values that PHP can?

Status
Not open for further replies.

SparceMatrix

Technical User
Mar 9, 2006
62
US
I need to retrieve the GET (and/or POST) values that are submitted from a form on one page in a Javascript running on the loaded targeted page. Can Javascript access those values? I've a lot experience doing this with PHP, but of course Javascript is client scripting. Do I have to parse out document.url? Do I have any options?

Any and all tips or clues would be appreciated.
 
Client-side Javascript can only access the GET information from a submitted page. You do this by reading the URL and parsing the data from that.

Lee
 
OK, so it's possible. Is it "document.URL" that needs to be read?
 
OK, right out of my little O'Reilly pocket reference,

Location is an object and refers me also to Link where there looks like there is a bunch of stuff to work with.

Thanks! I'll be back to summarize how I managed.
 
Well, I see that "window.location.search" will get me my query string like this,

Code:
?Name1=var1&Name2=vara+varb+varc&Name3=var3 ...

But am I obliged to parse everything out myself? It seems like there is a natural associative array there. Is there no object that has this already done for me?
 
In the time you've taken to ask the question and wait for an answer, you could have written a method to handle this. It isn't THAT hard to do.

How would you dynamically get the associative array indices without knowing what they were ahead of time? I could see an array of objects with a name field and a value field, but AFAIK that isn't built in yet.

Lee
 
Could have and did before I posted, actually. Thanks, I don't need that kind of advice.

What else is an associative array but a name field and a value so that the string,
Code:
?Name1=var1&Name2=vara+varb+varc&Name3=var3 ...
becomes,
Code:
SearchArray["Name1"]["var1"]
SearchArray["Name2"]["vara varb varc"]
SearchArray["Name3"]["var3"]
....
I'm not sure where you see an obstacle.
 
If you know the names of the fields, there's no obstacle. A built-in method would somehow have to return the name fields to you, too, which would probably best be handled by an array of objects with the name as one property and the value as the other. That's the "obstacle" that of returning an associative array from a built-in function.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top