469,592 Members | 2,032 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

Could we submit and display data in the same page?

46
Hello experts.

I wish to post a string and display the output in the same page.

The Scenario:

Table name: importer
Field name: imp_code, imp_sname, imp_address, imp_tel

I have a page which consists of
(i) a form
(ii) output fields

When the user key-in the imp_code through the keyboard the system should check whether the code exist in the database.

If the code does not exist, it should display a pop up window "Importer code does not exist!"

Else if the code exist in the database the system should populate the text fields keeping the form field imp_code still displayed with what the user has typed.

I have given it a try, but it does not work !

Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <% @language="VBScript" %>
  3. <% option explicit %>
  4.  
  5.  
  6. <%
  7. if request.form("action") <> "query_importer" then
  8. %>
  9.  
  10.  
  11. <form method="post">
  12. <input type="hidden" name="action" value="query_importer">
  13.  
  14. Importer Code: <input type="text" name="impcode">
  15. <br/>
  16. <input type="submit" name="submit" value="submit">
  17. </form>
  18.  
  19.  
  20. <%
  21. else
  22.  
  23. dim strImpcode
  24. strImpcode = request.form("impcode")
  25.  
  26. dim adocon, adorst, strSQL
  27.  
  28. set adocon = server.createobject("ADODB.connection")
  29. adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
  30.  
  31. set adorst= server.createobject("ADODB.Recordset")
  32.  
  33. strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
  34.  
  35. adorst.open strSQL, adocon
  36. %>
  37.  
  38. <html>
  39. <body>
  40.  
  41. <%
  42. do while not adorst.eof
  43. response.write adorst("imp_sname")
  44. response.write("<br/>")
  45. response.write adorst("imp_oname")
  46. response.write("<br/>")
  47. response.write adorst("imp_address")
  48. response.write("<br/>")
  49. response.write adorst("imp_tel")
  50.  
  51. adorst.movenext
  52.  
  53. loop
  54. %>
  55.  
  56. <%end if%>
  57.  
  58. </body>
  59. </html>
  60.  
  61.  
Please help ........
Feb 18 '08 #1
4 2306
DrBunchman
979 Expert 512MB
Hi Giandeo,

This requires only a couple of changes to the code you've printed above.

Firstly, take your form outside of the If..Then...Else statement. Doing this means that it will always display the text box when you have submitted your form.

Secondly you need to set the value of your text box to whatever the user just typed:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type="text" name="impcode" value=<%=request.form("impcode")%>>
  3.  
And lastly you need to put another If...Then...Else statement in which checks whether anything has been found in the database. If so it will display it and if not it will print a message.

Your code should then look like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <% @language="VBScript" %>
  3. <% option explicit %>
  4.  
  5. <form method="post">
  6. <input type="hidden" name="action" value="query_importer">
  7.  
  8. Importer Code: <input type="text" name="impcode" value=<%=request.form("impcode")%>>
  9. <br/>
  10. <input type="submit" name="submit" value="submit">
  11. </form>
  12.  
  13. <%
  14. if request.form("action") = "query_importer" then
  15. dim strImpcode
  16. strImpcode = request.form("impcode")
  17.  
  18. dim adocon, adorst, strSQL
  19.  
  20. set adocon = server.createobject("ADODB.connection")
  21.  
  22. adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
  23.  
  24. set adorst= server.createobject("ADODB.Recordset")
  25.  
  26. strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
  27.  
  28. adorst.open strSQL, adocon
  29. %>
  30.  
  31. <html>
  32. <body>
  33.  
  34. <%
  35. If adorst.eof Then
  36. Response.Write("Importer code does not exist!")
  37. Else
  38. do while not adorst.eof
  39.  
  40. response.write adorst("imp_sname")
  41. response.write("<br/>")
  42. response.write adorst("imp_oname")
  43. response.write("<br/>")
  44. response.write adorst("imp_address")
  45. response.write("<br/>")
  46. response.write adorst("imp_tel")
  47. adorst.movenext 
  48. loop
  49. End If
  50. %>
  51.  
  52. <%end if%>
  53.  
  54. </body>
  55. </html>
  56.  
If you want to display a message box when no record is found rather than using response.write then you could do so instead by changing line 35.

Let me know if this helps,

Dr B
Feb 18 '08 #2
giandeo
46
Hi Giandeo,

This requires only a couple of changes to the code you've printed above.

Firstly, take your form outside of the If..Then...Else statement. Doing this means that it will always display the text box when you have submitted your form.

Secondly you need to set the value of your text box to whatever the user just typed:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type="text" name="impcode" value=<%=request.form("impcode")%>>
  3.  
And lastly you need to put another If...Then...Else statement in which checks whether anything has been found in the database. If so it will display it and if not it will print a message.

Your code should then look like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <% @language="VBScript" %>
  3. <% option explicit %>
  4.  
  5. <form method="post">
  6. <input type="hidden" name="action" value="query_importer">
  7.  
  8. Importer Code: <input type="text" name="impcode" value=<%=request.form("impcode")%>>
  9. <br/>
  10. <input type="submit" name="submit" value="submit">
  11. </form>
  12.  
  13. <%
  14. if request.form("action") = "query_importer" then
  15. dim strImpcode
  16. strImpcode = request.form("impcode")
  17.  
  18. dim adocon, adorst, strSQL
  19.  
  20. set adocon = server.createobject("ADODB.connection")
  21.  
  22. adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
  23.  
  24. set adorst= server.createobject("ADODB.Recordset")
  25.  
  26. strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
  27.  
  28. adorst.open strSQL, adocon
  29. %>
  30.  
  31. <html>
  32. <body>
  33.  
  34. <%
  35. If adorst.eof Then
  36. Response.Write("Importer code does not exist!")
  37. Else
  38. do while not adorst.eof
  39.  
  40. response.write adorst("imp_sname")
  41. response.write("<br/>")
  42. response.write adorst("imp_oname")
  43. response.write("<br/>")
  44. response.write adorst("imp_address")
  45. response.write("<br/>")
  46. response.write adorst("imp_tel")
  47. adorst.movenext 
  48. loop
  49. End If
  50. %>
  51.  
  52. <%end if%>
  53.  
  54. </body>
  55. </html>
  56.  
If you want to display a message box when no record is found rather than using response.write then you could do so instead by changing line 35.

Let me know if this helps,

Dr B
Hello Sir
Your solution is fantastic. It works perfectly well. Thank you so much.

But still, I wish to seek your help for this.

Presently, Only the input field imp_code and the submit button are shown on load. Once you key-in the imp_code then the results are displayed.

Sir, I wish to show a page consisting of the input field imp_code along with the display text fields imp_sname, imp_oname, imp_address and imp_tel. Meaning that the user can see the whole form. Once he key-in the imp_code and presses the submit button, the text fields are populated.

Could you please help me....


I have given it a try but it does not work

Here is my code

Expand|Select|Wrap|Line Numbers
  1.  
  2. <% @language="VBScript" %>
  3. <% option explicit %>
  4.  
  5. <form method="post">
  6. <input type="hidden" name="action" value="query_importer">
  7.  
  8. Importer Code: <input type="text" name="impcode" value=<%=request.form("impcode")%>>
  9. <br/>
  10. <input type="submit" name="submit" value="submit">
  11. </form>
  12.  
  13. <%
  14. if request.form("action") = "query_importer" then
  15. dim strImpcode
  16. strImpcode = request.form("impcode")
  17.  
  18. dim adocon, adorst, strSQL
  19.  
  20. set adocon = server.createobject("ADODB.connection")
  21.  
  22. adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
  23.  
  24. set adorst= server.createobject("ADODB.Recordset")
  25.  
  26. strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
  27.  
  28. adorst.open strSQL, adocon
  29. %>
  30.  
  31. <html>
  32. <body>
  33.  
  34. <%
  35. If adorst.eof Then
  36. Response.Write("Importer code does not exist!")
  37. Else
  38. do while not adorst.eof
  39. %>
  40.  
  41. <input type="text" value="<%adorst("imp_sname")%>
  42. <br/>
  43. <input type="text" value="<%adorst("imp_oname")%>
  44. <br/>
  45. <input type="text" value="<%adorst("imp_address")%>
  46. <br/>
  47. <input type="text" value="<%adorst("imp_tel")%>
  48.  
  49.  
  50. <%
  51. adorst.movenext 
  52. loop
  53. End If
  54. %>
  55.  
  56. <%end if%>
  57.  
  58. </body>
  59. </html>
  60.  
Feb 19 '08 #3
DrBunchman
979 Expert 512MB
If I understand you correctly you want to display the textfields even when they're blank?

In your code above the text fields are sitting inside an If...Then...Else statement and they'll only be displayed if there is anything found in the database. If you take them out of this condition they will always be displayed.

It would make sense here to separate the code reading your database from your html controls by storing the recordset values as variables. Like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <%
  3. Dim sName
  4. Dim oName
  5. Dim address
  6. Dim tel
  7.  
  8. If adorst.eof Then
  9.     Response.Write("Importer code does not exist!")
  10.     sName = ""
  11.     oName = ""
  12.     address = ""
  13.     tel = ""
  14. Else
  15.      sName = adorst("imp_sname")
  16.      oName = adorst("imp_oname")
  17.      address = adorst("imp_address")
  18.      tel = adorst("imp_tel")
  19. End If
  20. %>
  21.  
  22. <input type="text" value="<%=sName%>
  23. <br/>
  24. <input type="text" value="<%=oName >
  25. <br/>
  26. <input type="text" value="<%=address >
  27. <br/>
  28. <input type="text" value="<%=tel >
  29.  
I hope I've understood your problem correctly and that you can see how the code works. Let me know how you get on :-)

Dr B
Feb 19 '08 #4
Why don't you use Ajax ?
You can post data, get a response and update display according to the response.

you can check these solutions:
- Dynajax (http://www.dynajax.org )
- Ajax control toolkit (by Microsoft)
- ScriptService annotation

Regards,
hope I could help.
Magic Ben
Feb 19 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.