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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass values from one page/form to another 1

Status
Not open for further replies.

southbean

Technical User
Jun 3, 2005
147
US

Hello All,

I’m struggling to find a way to pass data from one page to another.

The code to my input form is below. This works fine.

Code:
<script type="text/javascript">
	function GoToForm2()
		{
		parent.location = 'Form2.asp'
		}
</script>

<Form action="Form1.asp" method="get">

<p>
            <input type="text" name="tbxSearch" id="tbxSearch" value=""/>
            <input type="submit" name="btnSubmit" id="btnSubmit" value="Search" />
            <br />
            <% 
	if request.querystring("btnSubmit") = "Search" then    
    strSearch = request.querystring("tbxSearch")
%>
     <SELECT name="UserName" onchange="GoToForm2()";>
<% 
'open DB
set conn=createobject("ADODB.Connection")
conn.ConnectionString = "DSN=Server1;Uid=user1;Pwd=password1"
conn.open

'retrieve dropdown menu contents
sql = "SELECT table1.last_name, " &_
		"table1.first_name, " &_
		"table1.unique_id, " &_
	"FROM table1 " &_
    "WHERE table1.last_name LIKE '%" & strSearch & "%'" 

set rs = conn.execute(sql)

'populate dropdown
While Not rs.EOF
    strLName = rs("last_name")
	strFName = rs("first_name")
	strUID = rs("unique_id")
        response.write "<OPTION>" & strLName + ", " +  strFName + ", " + strUID & "</OPTION>"
    rs.MoveNext
Wend
rs.close
Set rs = Nothing
%>
            </SELECT>
 <% end if %>  
</form>

However, I need to pass three fields (‘last_name’, ‘first_name’, ‘unique_id’) to another page/form.

I’ve tried several variations on the code below but nothing has worked.

Code:
<INPUT <% Response.Write(Request.QueryString(“last_name")); %> >

Any help or suggestions with this would be GREATLY appreciated!

- tl
 
request.querystring will give you the values that are given to the script (page) by using variables eg:

form1.asp?variable1=1


in this case you don't do that, you need
request.form("UserName") to get the value of that field in the previous form.



form1.asp:
Code:
<form action="form1.asp" method="post">
<input type="text" name="tbxSearch" >
<input type="submit" value="Search" />
</form>

<form action="form2.asp" method="post">
<%
if request.servervariables("REQUEST_METHOD")= "POST" then    
%>
<SELECT name="UserName" onchange="submit();">
<option value="first option">List with
<option value="second option">names like '%<%= request.form("tbxSearch") %>
</SELECT>
<% end if %>  
</form>


form2.asp
Code:
Selected: <%= request.form("UserName") %>



 

Hello foxbox,

Thank you very much for your reply! However, I'm not that good with ASP so forgive me for having some difficulty understanding it.

What my 'Form1.asp' does is populate a dropdown box with values %LIKE% what is typed in the txtSearch textbox.

I'm not sure what Form2.asp does in your method. Can you please explain a bit more?

Again, thank you very much for your reply!!!

- tl
 
That is probably because he changed you from a get method to a post method. Get passes values with request.querystring while post passes with request.form.

The reason I suggested session variables is because you were using Javascript and not the form action to move between the page you posted and the one you didn't.

 
Please create a form1.asp and a form2.asp with the code above. then start form1.asp in your browser.
i skipped the database part, and made this sample with hard coded option, just to make clear that this works.
the trick is that form1 has TWO forms. the first one will fire form1.asp after pressing the search button.
form2 is fired after an onchange event of the pulldown.

because the forms are POSTing, you must use request.form to get values.

 

Hi BigRed1212 & foxbox,

Thank you BOTH for your replies!!!

I tried your suggestion foxbox but realized that I can not use the 'Post' because there are too many records (over 100,000) and the page just times out trying to load. I have to use the 'Get' method.

You are right, BigRed1212, it looks like session variables may be the right way. I looked at the link you provided but am not sure how I would use that to pass the value in the dropdown list generated by the 'Get'.

Could I impose on you to perhaps provide a little example?

Again, thank you both VERY much for your assistance. I really appreciate it!!!

- tl
 
????????????????????
Your Table1 has over 100.00 records
???????????????????

i think you should consider another way of searching, NOT using a dropdown (SELECT) list. There is a practical limit to the number of options any HTML page can handle (and i really do not see why GET would help around that).

if you really want that pulldonn list:
1. Start using a minimum length for "tbxSearch", eg 3.
2. Change the WHERE in your SQL from
table1.last_name LIKE '%" & strSearch & "%'"
into
table1.last_name LIKE '" & strSearch & "%'"

(look carefully). You'll get records with last_name's that START with "strSearch"

BTW: does your program now always timeout? then just maybe there is no rs.movenext in the loop!





 

Hi foxbox,

Thank you again for your reply!

This does not have to be a dropdown (SELECT) list. If I could display query in a table that would be fine with me. Anything that works!

In fact, would I be able to display results in a table and have the "unique_id" be a hyperlink? Would that pass the record values to a new page that I could then use the 'Request.QueryString' method?
Code:
<INPUT <% Response.Write(Request.QueryString("unique_id")); %> >

Thank you again for all your help!

- tl

 
Everything is possible, but it all starts with your ideas about how it should work. It look likes you need the user to pick a Name (in fact a unique_id), in order to use that on another page.

So you could make a searchpage and give users the posibility to select via a link (href=page2.asp?id=). Your page2.asp does the request.querystring("id") etc.

Search.asp:
Code:
<form action="search.asp" method="post">
<input type="text" name="tbxSearch" >
<input type="submit" value="Search" />

<%
if request.servervariables("REQUEST_METHOD")= "POST" then    

set conn=createobject("ADODB.Connection")
conn.ConnectionString = "DSN=Server1;Uid=user1;Pwd=password1"
conn.open

sql = "SELECT top 200 table1.last_name, " &_
      "table1.first_name, " &_
      "table1.unique_id, " &_
      "FROM table1 " &_
     "WHERE table1.last_name LIKE '" & trim(request.Form("tbxSearch")) & "%'"
set rs = conn.execute(sql)
While Not rs.EOF
    response.write "<a href=page2.asp?id=" & rs("unique_id") & ">" & rs("last_name") & ", " & rs("first_name") & "</a><br>"
    rs.MoveNext
Wend
rs.close
Set rs = Nothing
conn.close
set conn = nothing

end if 
%>
(only the first 200 names are listed)
</form>


 

Hello Foxbox,

Thanks very much for your reply!!! I think you've put me on the right track! And, I'm getting close!

However, I'm getting an error message:
Code:
ADODB.Recordset error '800a0cc1' 

Item cannot be found in the collection corresponding to the requested name or ordinal.

I did some research and it seems to indicate some type of SQL syntax error. However, I can't see anything wrong with it.

I should mention that this is an Oracle DB I'm querying - which I'm not all that familiar with. I tried omitting the 'Top 200' clause but that seemed to make no difference.

Any further help you could provide would be greatly appreciated!!!

- tl
 
SELECT TOP does not wotk on Oracle.
item not found means you have a fieldname wrong/ misspelled
 
use "WHERE ROWNUM <= 200" in Oracle to limit the number of results.
Just maybe it is a good idea to sort the results? Oracle's rownum works different than SELECT TOP! You need to do it this way:

Code:
SELECT last_name,first_name,unique_id
FROM
(
 SELECT  last_name,first_name,unique_id
 FROM table1
 WHERE last_name LIKE 'Foxb%'
 ORDER BY last_name
)
WHERE rownum < 200
 

Hello Foxbox,

Thank you very much! That worked! I now have the value passing from Form1.asp to Form2.asp.

However, that has presented me with another issue. I'm using
Code:
Response.Write(Request.QueryString("unique_id"))
in a form in Form2.asp to display the value passed from Form1.asp. On that form in Form2.asp users fill out several more textboxes. A user then clicks a submit button on that form on Form2.asp which sends that data to another page (which sends an email to an administrator with the values of the entire form).

If I could impose on you a bit more, how can I pass that value Response.Write(Request.QueryString("unique_id")) to another page?

Again, I can not thank you enough for your assistance so far! It has been invaluable!!!

- tl


 
if you SUBMIT a page, you must do a request.form([fieldname]) .

It now sounds if you have a form with some input fields together with this list of names (all with
page2.asp?id=" & rs("unique_id") )

those links will bring you to page2 directly **without** a submit, so request.form will not work.

many options here (as always). eg change those links and call the same page. At page start test if there was a submit with "?id=" and if yes, retrieve the name. Put that in a (readonly) field (together with the others). Write a tiny piece javascript to test if a name was selected. If yes: enable the SUBMIT button, otherwise disable it.

in page2.asp you retrieve the entered values then with request.form([fieldname])





 

Hi Foxbox,

Thank you for your reply!

Unfortunately, that sounds like it's a little over my head.

I was looking at Hidden Inputs and wondering if that would work for this issue.
Code:
<input type="hidden" name="last_name" value="<% response.write last_name %>">
<input type="hidden" name="first_name" value="<% response.write first_name %>">
If I placed these in page1.asp would they populate? If so, could I pass them to page2.asp to populate the <INPUT> textbox.

Again, thank you very much for all you help!!!

- tl
 
Create these 3 files, run pag1.asp, see what happend and try to understand it:

Page1.asp
Code:
<form name="page1" action="page2.asp" method="post">
Firstname: <input  type="text" name="fFirstname" value="<%= session("sFName") %>"><br />
Lastname: <input type="text" name="fLastname"  value="<%= session("sLName") %>"><br />
<input type="submit" value="Submit" />
</form>

Page2.asp
Code:
<%
if request.ServerVariables("REQUEST_METHOD") = "POST" then
 ' We come from page1.asp
 session("sFName") = trim(request.Form("fFirstname"))
 session("sLName") = trim(request.Form("fLastname"))

 ' Check on blank input
 if session("sFName") = "" or  session("sLName") = "" then
  response.Write "Please enter a first- and a lastname!<br>"
  response.Write "<input type=button value=OK onclick=""location.href='page1.asp'"">"
  response.end
 end if
  
  
end if
 
%>
<form name="page2" action="page3.asp" method="post">
<input type="hidden" name="fFirstname" value="<%= session("sFName") %>" />
<input type="hidden" name="fLastname" value="<%= session("sLName") %>" />
Request.form fFirstname = <%= request.Form("fFirstname") %><br />
Request.form fLirstname = <%= request.Form("fLastname") %><br />

Session variable sFName = <%= session("sFName") %><br />
Session variable cLName = <%= session("sLName") %><br />

<% if request.QueryString("id") = "" then %>
<br />
Choose:<br />
<a href="page2.asp?id=1">Apple</a><br />
<a href="page2.asp?id=2">Banana</a><br />
<a href="page2.asp?id=3">Ananas</a><br />

<% 
else 
response.Write "Fruit:"
dim cID
cID = request.QueryString("id")
select case cID
case "1"
 response.Write "Apple"
case "2"
 response.Write "Banana"
case "3"
 response.Write "Ananas"
end select

' Either in a session var or hidden field
session("sID") = cID
%>
<input type="hidden" name="fID" value="<%= cID %>" />

<br />

<input type="submit" value="OK" />
<input type="button" value="Cancel" onclick="location.href='page1.asp'" />

<% end if %>
</form>


Page3.asp
Code:
Firstname: <%= request.Form("fFirstname") %><br />
Lastname:  <%= request.Form("fLastname") %><br />
Fruit ID (request form): <%= request.Form("fID") %> or<br />
Fruit ID (sesion var): <%= session("sID") %>


 

Hello Foxbox,

Brilliant! This dolt finally gets it!

Now I just need to carefully work through implementing it on my pages.

Again, I can't thank you enough for patiently walking me through this. I really learned a lot!

Thanks again!

- tl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top