Could we save 5 lines of records in a text box and display same via asp?

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

    Could we save 5 lines of records in a text box and display same via asp?

    Hello Experts.

    I am facing a strange problem.

    I have created an input text box with 5 lines. Unfortunately, when the user enters data in the text box only the first line is saved in the table. Meaning that after entering the first line of text when the user presses enter on the keybord that part of data onwards is not recorded in the table.

    Please help me out.......
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Giandeo, could you show us your code please?

    Thanks,

    Dr B

    Comment

    • Nicodemas
      Recognized Expert New Member
      • Nov 2007
      • 164

      #3
      All five are considered different sources of input. Ergo, you would have to test all five textboxes to see if there is content within them, and save each individually, or compile all 5 into one, then save to the database.

      Any fields that contain no data will not be passed on to the script from the form.

      Comment

      • markrawlingson
        Recognized Expert Contributor
        • Aug 2007
        • 346

        #4
        Judging from giandeo's post, I don't think it's 5 separate text boxes. I think he's referring to a textarea, or a multiline textbox control as the asp.net guys say.

        If it is indeed 5 separate textboxes - refer to Nicodemas post - however if it really is ONE text box then you're going to have to show us how you're creating this "input texbox with 5 lines", giandeo. Show your code so we can take a look.

        Sincerely,
        Mark

        Comment

        • giandeo
          New Member
          • Jan 2008
          • 46

          #5
          Originally posted by DrBunchman
          Hi Giandeo, could you show us your code please?

          Thanks,

          Dr B
          Hi expert. Greatful to seek your help again.

          Here is my codes

          Code:
          <% @language=VBScript%>
          <% option explicit %>
          <html>
          <head>
          <title>Sample Program</title>
          
          </head>
          
          <body>
          <form name="hello" method="post" action="update_imp_table.asp">
            <table width="50%"  border="0.5" align="center">
              <tr>
                <td><span class="style4">Address </span></td>
                <td><textarea name="theaddress" cols="40" rows="4" wrap="VIRTUAL" id="theaddress"></textarea></td>
              </tr>
              <tr>
                <td><input type="reset" name="reset" value="Reset">
                <input name="Submit" type="submit" value="Save"></td>
                <td>&nbsp;</td>
              </tr>
            </table>
          </form>
          </body>
          </html>

          The following program will update the importer table based on the data acquired from the form input

          Code:
          <% @language=VBScript%>
          <% option explicit %>
          
          
          <%
          
          dim conn, sql, rs
          dim strdate, strtime
          
          strdate=date()
          strtime=time()
          
          'create ADO connection and recordset object
          set conn=Server.CreateObject("ADODB.Connection")
          set rs = Server.CreateObject("ADODB.Recordset")
          
          'we open connection to the database
          conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("sp.mdb")
          
          
          sql="SELECT imp_address FROM importer;"
          
          'open the recordset with sql query
          rs.open sql, conn
          
          'prepare the database to add a new record 
          rs.AddNew
          rs.Fields("imp_address")=Request.Form("theaddress")
          
          'save the update
          rs.update
          rs.close
          
          on error resume next
          if err<>0 then
              Response.Write("<h1 align='center'>No update permissions! </h1>")
          else 
          	Response.Write("<h1 align='center'>Record Sucessfully Added !</h1>")
          end if
          
          
          set rs=Nothing
          set conn=Nothing
          %>
          Problem Encountered:

          When the user key-in data in the address textbox without pressing the ENTER key, all the data is saved in the address field (40 characters maximum).

          But, if the user key-in data in the address textbox pressing ENTER key on the keyboard each time, then only the first line is stored in the table.

          For Example if the user enters the address as follows:

          Royal Road
          [PRESS ENTER KEY]
          Pont-Lardier
          [PRESS ENTER KEY]
          Bel-Air Riviere Seche

          ONLY THE FIRST LINE : Royal Road is saved in the table

          I am sure you will be agreeable with me that users tend to press enter when they type the address of someone. I want to make them feel comfortable with the application.

          Please help me out....

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            Hi giandeo, wouldn't it be easier to request the address as separate fields? e.g.

            [HTML]
            <input type="text" name="txtAddres s1" />

            <input type="text" name="txtAddres s2" />

            <input type="text" name="txtCity" />

            <input type="text" name="txtPostco de" />
            [/HTML]

            These could then be stored in your database as separate fields which means you could sort by and select street names, cities and countries with ease.

            But if you don't want to do that...

            I don't know why the full value of your textarea is not being saved but you could use a bit of javascript to replace the carriage returns with html line breaks. These would be stored in your database and upon retreiving the data the carriage returns would be displayed again.

            Code:
             
            <script language="javascript">
            function RealTimeConvertion(e, textArea)
            {
            	var keyCode
            	 e = event
            	 keyCode = e.keyCode
            if(keyCode == 13)
            {
            	textArea.value +="<br>"	 
            	return true
            }
            }
            </script>
            If you stick this script between your head tags and call it in your textarea's onKeyPress event it will add the <br> to your input automatically.

            [HTML]<textarea onKeyPress="Rea lTimeConvertion (event,this)" name="theaddres s" cols="40" rows="4" wrap="VIRTUAL" id="theaddress" ></textarea>[/HTML]

            I would recommend the first option though.

            I hope this helps you. Let us know how you get on.

            Dr B

            Comment

            • giandeo
              New Member
              • Jan 2008
              • 46

              #7
              Originally posted by DrBunchman
              Hi giandeo, wouldn't it be easier to request the address as separate fields? e.g.

              [HTML]
              <input type="text" name="txtAddres s1" />

              <input type="text" name="txtAddres s2" />

              <input type="text" name="txtCity" />

              <input type="text" name="txtPostco de" />
              [/HTML]

              These could then be stored in your database as separate fields which means you could sort by and select street names, cities and countries with ease.

              But if you don't want to do that...

              I don't know why the full value of your textarea is not being saved but you could use a bit of javascript to replace the carriage returns with html line breaks. These would be stored in your database and upon retreiving the data the carriage returns would be displayed again.

              Code:
               
              <script language="javascript">
              function RealTimeConvertion(e, textArea)
              {
              	var keyCode
              	 e = event
              	 keyCode = e.keyCode
              if(keyCode == 13)
              {
              	textArea.value +="<br>"	 
              	return true
              }
              }
              </script>
              If you stick this script between your head tags and call it in your textarea's onKeyPress event it will add the <br> to your input automatically.

              [HTML]<textarea onKeyPress="Rea lTimeConvertion (event,this)" name="theaddres s" cols="40" rows="4" wrap="VIRTUAL" id="theaddress" ></textarea>[/HTML]

              I would recommend the first option though.

              I hope this helps you. Let us know how you get on.

              Dr B
              Hello Dr B

              You are absolutely correct Sir. Address has to be stored in the database as separate fields which then could be sorted easily.

              Thank you very much for your kind advice.

              Sir one more advice please - Let us say a user wants to input his findings in a text box and he is free to insert a carriage return (ENTER KEY) any where in his report. Could we apply the same technique as you mentioned in the second part ?

              Comment

              • DrBunchman
                Recognized Expert Contributor
                • Jan 2008
                • 979

                #8
                Yes you could.

                You could run a function to do it as the user is typing or perhaps do it upon the click of a button. So the user types in their data including carriage returns, clicks a button to save it and at this point you call a function which would go through and replace all the carriage returns with <br>'s. I've just nicked the following from a quick google search:

                Code:
                 
                <script language="javascript">
                function ConvertCarriageReturns(textarea, strReplace)
                {
                textarea.value = escape(textarea.value)
                for(i=0;i<textarea.value.length;i++)
                {
                	 if(textarea.value.indexOf("%0D%0A") > -1 )
                	 {
                		 textarea.value = textarea.value.replace("%0D%0A",strReplace)
                	 }
                }
                textarea.value = unescape(textarea.value)
                }
                </script>
                And you would call it like so:

                [HTML]
                <textarea name="theaddres s" cols="40" rows="4" wrap="VIRTUAL" id="theaddress" ></textarea>

                <input type="button" onclick="Conver tCarriageReturn s(this.form.the address,'<br>') " value="Replace Returns!" />
                [/HTML]

                So from your example earlier:

                Royal Road
                [PRESS ENTER KEY]
                Pont-Lardier
                [PRESS ENTER KEY]
                Bel-Air Riviere Seche

                Would become:

                Royal Road<br>Pont-Lardier<br>Bel-Air Riviere Seche

                Which would look strange in the database but as soon as you wrote that back to the browser as html it would format itself correctly:

                Royal Road
                Pont-Lardier
                Bel-Air Riviere Seche

                Let me know if this works for you.

                Dr B

                Comment

                • giandeo
                  New Member
                  • Jan 2008
                  • 46

                  #9
                  Originally posted by DrBunchman
                  Yes you could.

                  You could run a function to do it as the user is typing or perhaps do it upon the click of a button. So the user types in their data including carriage returns, clicks a button to save it and at this point you call a function which would go through and replace all the carriage returns with <br>'s. I've just nicked the following from a quick google search:

                  Code:
                   
                  <script language="javascript">
                  function ConvertCarriageReturns(textarea, strReplace)
                  {
                  textarea.value = escape(textarea.value)
                  for(i=0;i<textarea.value.length;i++)
                  {
                  	 if(textarea.value.indexOf("%0D%0A") > -1 )
                  	 {
                  		 textarea.value = textarea.value.replace("%0D%0A",strReplace)
                  	 }
                  }
                  textarea.value = unescape(textarea.value)
                  }
                  </script>
                  And you would call it like so:

                  [HTML]
                  <textarea name="theaddres s" cols="40" rows="4" wrap="VIRTUAL" id="theaddress" ></textarea>

                  <input type="button" onclick="Conver tCarriageReturn s(this.form.the address,'<br>') " value="Replace Returns!" />
                  [/HTML]

                  So from your example earlier:

                  Royal Road
                  [PRESS ENTER KEY]
                  Pont-Lardier
                  [PRESS ENTER KEY]
                  Bel-Air Riviere Seche

                  Would become:

                  Royal Road<br>Pont-Lardier<br>Bel-Air Riviere Seche

                  Which would look strange in the database but as soon as you wrote that back to the browser as html it would format itself correctly:

                  Royal Road
                  Pont-Lardier
                  Bel-Air Riviere Seche

                  Let me know if this works for you.

                  Dr B
                  Sir,
                  I have tried the code you suggested.
                  As soon as I press the return key in the textbox, a <BR> appears on the editor and same is saved in the database.
                  When I retrieve the data entered through the textbox, the <BR> appears again.

                  I want to get rid of the <BR>

                  Please advise....

                  Comment

                  • DrBunchman
                    Recognized Expert Contributor
                    • Jan 2008
                    • 979

                    #10
                    That's why I suggested you replace the carriage returns after the user has finished typing rather than during. You could then silently replace the spaces with <br>.

                    How are you ouputting the text? If you are using response.write I would expect the <br> tags to be rendered by the browser as line breaks. Can you show me your code please?

                    Thanks,

                    Dr B

                    Comment

                    • giandeo
                      New Member
                      • Jan 2008
                      • 46

                      #11
                      Originally posted by DrBunchman
                      That's why I suggested you replace the carriage returns after the user has finished typing rather than during. You could then silently replace the spaces with <br>.

                      How are you ouputting the text? If you are using response.write I would expect the <br> tags to be rendered by the browser as line breaks. Can you show me your code please?

                      Thanks,

                      Dr B
                      Sir,
                      Here is my code.

                      Input Data in the Textbox

                      Code:
                      <script language="javascript">
                      function ConvertCarriageReturns(textarea, strReplace)
                      {
                      textarea.value = escape(textarea.value)
                      for(i=0;i<textarea.value.length;i++)
                      {
                           if(textarea.value.indexOf("%0D%0A") > -1 )
                           {
                               textarea.value = textarea.value.replace("%0D%0A",strReplace)
                           }
                      }
                      textarea.value = unescape(textarea.value)
                      }
                      </script>
                      
                      
                      Supervisor's Report<textarea onKeyPress="ConvertCarriageReturns(this.form.theorder,'<br>')" name="theorder" cols="40" rows="4" wrap="VIRTUAL" id="theorder"></textarea>
                      Retrive data from the table

                      Code:
                      Supervisor's Report<textarea name="theremarks" cols="55" rows="3" id="textarea" value="<%=rsList("thereport")%>"></textarea>

                      Comment

                      • DrBunchman
                        Recognized Expert Contributor
                        • Jan 2008
                        • 979

                        #12
                        I see why...html tags don't render inside a textarea or other text input. so the <br> is treated as normal text. The thing to do is to convert it back to carriage returns before assigning it to the value of the text area. Once i've got a few minutes spare I'll knock something together that's a bit tidier.

                        Dr B

                        Comment

                        • CroCrew
                          Recognized Expert Contributor
                          • Jan 2008
                          • 564

                          #13
                          Hello giandeo,

                          Why don’t you just do it on the fly? Try this:

                          [code=asp]
                          Supervisors Report<textarea name="theremark s" cols="55" rows="3"><%=Rep lace(rsList("th ereport"), "<br>", (chr(13)&chr(10 )))%></textarea>
                          [/code]


                          Hope this helps you out~

                          Comment

                          • giandeo
                            New Member
                            • Jan 2008
                            • 46

                            #14
                            Originally posted by CroCrew
                            Hello giandeo,

                            Why don’t you just do it on the fly? Try this:

                            [code=asp]
                            Supervisors Report<textarea name="theremark s" cols="55" rows="3"><%=Rep lace(rsList("th ereport"), "<br>", (chr(13)&chr(10 )))%></textarea>
                            [/code]


                            Hope this helps you out~
                            Sir, I will try the code and will inform you soon Sir. By the way, I wish to know whether we can trigger two different functions on the click of a button on submit.

                            Example:
                            Code:
                            <input type="button" onClick="ConvertCarriageReturns(this.form.theaddress,'<br>')" ******and call another function to check whether mandatory fields are filled properly>
                            Thank you in advance.

                            Comment

                            • CroCrew
                              Recognized Expert Contributor
                              • Jan 2008
                              • 564

                              #15
                              You can do it in two ways.

                              Many people believe that if you’re going to call multiple functions or subs from one event that create a function/sub that will call the multiple functions/subs:
                              Code:
                              	<input type="button" onClick="MasterFunction()">
                              
                              	<script language="javascript">
                              		function MasterFunction()
                              		{
                              			SubFunctionOne();
                              			SubFunctionTwo();
                              			SubFunctionThree();
                              		}
                              
                              		function SubFunctionOne()
                              		{
                              			Alert(“Hello World!”);
                              		}
                              
                              		function SubFunctionTwo()
                              		{
                              			Alert(“How are you?”);
                              		}
                              
                              		function SubFunctionThree()
                              		{
                              			Alert(“I am doing fine.”);
                              		}
                              	</script>
                              Or just call the multiple functions/subs within the event:
                              Code:
                              	<input type="button" onClick="MasterFunction(SubFunctionOne(); SubFunctionTwo(); SubFunctionThree();)">
                              Good luck~

                              Comment

                              Working...