Could we submit and display data in the same page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giandeo
    New Member
    • Jan 2008
    • 46

    Could we submit and display data in the same page?

    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:

    Code:
    <% @language="VBScript" %>
    <% option explicit %>
    
    
    <%
    if request.form("action") <> "query_importer" then
    %>
    
    
    <form method="post">
    <input type="hidden" name="action" value="query_importer">
    
    Importer Code: <input type="text" name="impcode">
    <br/>
    <input type="submit" name="submit" value="submit">
    </form>
    
    
    <%
    else
    
    dim strImpcode
    strImpcode = request.form("impcode")
    
    dim adocon, adorst, strSQL
    
    set adocon = server.createobject("ADODB.connection")
    adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
    
    set adorst= server.createobject("ADODB.Recordset")
    
    strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
    
    adorst.open strSQL, adocon
    %>
    
    <html>
    <body>
    
    <%
    do while not adorst.eof
    response.write adorst("imp_sname")
    response.write("<br/>")
    response.write adorst("imp_oname")
    response.write("<br/>")
    response.write adorst("imp_address")
    response.write("<br/>")
    response.write adorst("imp_tel")
    
    adorst.movenext
    
    loop
    %>
    
    <%end if%>
    
    </body>
    </html>
    Please help ........
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    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:
    Code:
     
    <input type="text" name="impcode" value=<%=request.form("impcode")%>>
    And lastly you need to put another If...Then...Els e 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:

    Code:
     
    <% @language="VBScript" %>
    <% option explicit %>
     
    <form method="post">
    <input type="hidden" name="action" value="query_importer">
     
    Importer Code: <input type="text" name="impcode" value=<%=request.form("impcode")%>>
    <br/>
    <input type="submit" name="submit" value="submit">
    </form>
     
    <%
    if request.form("action") = "query_importer" then
    dim strImpcode
    strImpcode = request.form("impcode")
     
    dim adocon, adorst, strSQL
     
    set adocon = server.createobject("ADODB.connection")
     
    adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
     
    set adorst= server.createobject("ADODB.Recordset")
     
    strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
     
    adorst.open strSQL, adocon
    %>
     
    <html>
    <body>
     
    <%
    If adorst.eof Then
    Response.Write("Importer code does not exist!")
    Else
    do while not adorst.eof
     
    response.write adorst("imp_sname")
    response.write("<br/>")
    response.write adorst("imp_oname")
    response.write("<br/>")
    response.write adorst("imp_address")
    response.write("<br/>")
    response.write adorst("imp_tel")
    adorst.movenext 
    loop
    End If
    %>
     
    <%end if%>
     
    </body>
    </html>
    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

    Comment

    • giandeo
      New Member
      • Jan 2008
      • 46

      #3
      Originally posted by DrBunchman
      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:
      Code:
       
      <input type="text" name="impcode" value=<%=request.form("impcode")%>>
      And lastly you need to put another If...Then...Els e 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:

      Code:
       
      <% @language="VBScript" %>
      <% option explicit %>
       
      <form method="post">
      <input type="hidden" name="action" value="query_importer">
       
      Importer Code: <input type="text" name="impcode" value=<%=request.form("impcode")%>>
      <br/>
      <input type="submit" name="submit" value="submit">
      </form>
       
      <%
      if request.form("action") = "query_importer" then
      dim strImpcode
      strImpcode = request.form("impcode")
       
      dim adocon, adorst, strSQL
       
      set adocon = server.createobject("ADODB.connection")
       
      adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
       
      set adorst= server.createobject("ADODB.Recordset")
       
      strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
       
      adorst.open strSQL, adocon
      %>
       
      <html>
      <body>
       
      <%
      If adorst.eof Then
      Response.Write("Importer code does not exist!")
      Else
      do while not adorst.eof
       
      response.write adorst("imp_sname")
      response.write("<br/>")
      response.write adorst("imp_oname")
      response.write("<br/>")
      response.write adorst("imp_address")
      response.write("<br/>")
      response.write adorst("imp_tel")
      adorst.movenext 
      loop
      End If
      %>
       
      <%end if%>
       
      </body>
      </html>
      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

      Code:
       
      <% @language="VBScript" %>
      <% option explicit %>
       
      <form method="post">
      <input type="hidden" name="action" value="query_importer">
       
      Importer Code: <input type="text" name="impcode" value=<%=request.form("impcode")%>>
      <br/>
      <input type="submit" name="submit" value="submit">
      </form>
       
      <%
      if request.form("action") = "query_importer" then
      dim strImpcode
      strImpcode = request.form("impcode")
       
      dim adocon, adorst, strSQL
       
      set adocon = server.createobject("ADODB.connection")
       
      adocon.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "&server.mapPath("sp.mdb")
       
      set adorst= server.createobject("ADODB.Recordset")
       
      strSQL="SELECT * FROM importer WHERE imp_code='"&strImpcode&"'"
       
      adorst.open strSQL, adocon
      %>
       
      <html>
      <body>
       
      <%
      If adorst.eof Then
      Response.Write("Importer code does not exist!")
      Else
      do while not adorst.eof
      %>
      
      <input type="text" value="<%adorst("imp_sname")%>
      <br/>
      <input type="text" value="<%adorst("imp_oname")%>
      <br/>
      <input type="text" value="<%adorst("imp_address")%>
      <br/>
      <input type="text" value="<%adorst("imp_tel")%>
      
       
      <%
      adorst.movenext 
      loop
      End If
      %>
       
      <%end if%>
       
      </body>
      </html>

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        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...Els e 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:
        Code:
         
        <%
        Dim sName
        Dim oName
        Dim address
        Dim tel
         
        If adorst.eof Then
        	Response.Write("Importer code does not exist!")
        	sName = ""
        	oName = ""
        	address = ""
        	tel = ""
        Else
        	 sName = adorst("imp_sname")
        	 oName = adorst("imp_oname")
        	 address = adorst("imp_address")
        	 tel = adorst("imp_tel")
        End If
        %>
         
        <input type="text" value="<%=sName%>
        <br/>
        <input type="text" value="<%=oName >
        <br/>
        <input type="text" value="<%=address >
        <br/>
        <input type="text" value="<%=tel >
        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

        Comment

        • magicben
          New Member
          • Feb 2008
          • 1

          #5
          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

          Comment

          Working...