Forums

This topic is locked

Inserting text in textfields with at button?

Posted 05 Sep 2002 15:52:01
1
has voted
05 Sep 2002 15:52:01 Henrik Sandeberg posted:
I have a form and I need to insert a br in the textfield, like the form that you post messages in, when you hit the italic or bold button it inserts the code directly in this textfield, how do i do that???



Edited by - smalbenet on 05 Sep 2002 16:03:06

Replies

Replied 05 Sep 2002 19:29:09
05 Sep 2002 19:29:09 Ned Frankly replied:
It sounds like you actually need something that will convert Carriage Returns / Line Feeds to break tags. The ASP/javascript function below will do that. Just wrap your database field in ConvertBR() and your text box input will be displayed with the line breaks the user put in.

<%
function ConvertBR(input) {
// Converts carriage returns
// to <BR> for display in HTML

var output = "";
if (input) {
for (var i = 0; i < input.length; i++) {
if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
i++;
output += " <br>";
} else {
output += input.charAt(i);
}
}
} else { output = "" }
return output;
}
%>

Ned Frankly
Replied 06 Sep 2002 07:49:10
06 Sep 2002 07:49:10 Henrik Sandeberg replied:
YES!!! thnx, this was just what i needed.

Replied 06 Sep 2002 08:27:46
06 Sep 2002 08:27:46 Henrik Sandeberg replied:
Hmm, i dont know WHERE to insert it, i have a file called newpost.asp wherer the user will out in text and send it to the db, but where will i insert this command, please help, i also have a file called index2.asp which will show the result. But this is exactly what i want!
Code below of my newpost.asp

<pre id=code><font face=courier size=2 id=code> &lt;%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%&gt;
&lt;!--#include file="../../../Connections/Camf1.asp" --&gt;
&lt;%
' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME")
If (Request.QueryString &lt;&gt; "" Then
MM_editAction = MM_editAction & "?" & Request.QueryString
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%&gt;
&lt;%
' *** Insert Record: set variables

If (CStr(Request("MM_insert") = "frmnypost" Then

MM_editConnection = MM_Camf1_STRING
MM_editTable = "Ordet"
MM_editRedirectUrl = "index.asp"
MM_fieldsStr = "vecka|value|overskrift|value|ingress|value|huvudtext|value"
MM_columnsStr = "vecka|none,none,NULL|overskrift|',none,''|ingress|',none,''|huvudtext|',none,''"

' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|"
MM_columns = Split(MM_columnsStr, "|"

' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl &lt;&gt; "" And Request.QueryString &lt;&gt; "" Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString &lt;&gt; "" Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If

End If
%&gt;
&lt;%
' *** Insert Record: construct a sql insert statement and execute it

Dim MM_tableValues
Dim MM_dbValues

If (CStr(Request("MM_insert") &lt;&gt; "" Then

' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),","
MM_delim = MM_typeArray(0)
If (MM_delim = "none" Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none" Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none" Then MM_emptyVal = ""
If (MM_formVal = "" Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal &lt;&gt; "" Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'" Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''" & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i &lt;&gt; LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & " values (" & MM_dbValues & ""

If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command"
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

If (MM_editRedirectUrl &lt;&gt; "" Then
Response.Redirect(MM_editRedirectUrl)
End If
End If

End If
%&gt;
&lt;html&gt;
&lt;head&gt;


&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;link href="../../../include/amf1.css" rel="stylesheet" type="text/css"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;p class="headul"&gt;L&auml;gg till ny Chefen har ordet.&lt;/p&gt;
&lt;form action="&lt;%=MM_editAction%&gt;" method="POST" name="frmnypost" id="frmnypost"&gt;

&lt;table width="794" border="0" cellpadding="0"&gt;

&lt;tr&gt;
&lt;td width="153" class="litenoverskrift"&gt;Vecka (ex 235)&lt;/td&gt;
&lt;td width="635" class="litenoverskrift"&gt;&Ouml;verskrift&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input name="vecka" type="text" class="textbox2" id="vecka"&gt;&lt;/td&gt;
&lt;td&gt;
&lt;input name="overskrift" type="text" class="textbox2" id="overskrift" size="55"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="litenoverskrift"&gt;Ingress&lt;/td&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;
&lt;textarea name="ingress" cols="80" class="textbox2" id="ingress"&gt;&lt;/textarea&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="litenoverskrift"&gt;Huvudtext&lt;/td&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2"&gt;
&lt;textarea name="huvudtext" cols="80" rows="4" class="textbox2" id="huvudtext"&gt;&lt;/textarea&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;td&gt;&nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input name="okknapp" type="submit" class="knappar" id="okknapp" value="L&auml;gg till"&gt;&lt;/td&gt;
&lt;td&gt;
&lt;input name="Submit2" type="reset" class="knappar" value="Rensa formul&auml;r"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;input type="hidden" name="MM_insert" value="frmnypost"&gt;
&lt;/form&gt;
&lt;p&gt;&nbsp; &lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</font id=code></pre id=code>

Replied 06 Sep 2002 15:08:39
06 Sep 2002 15:08:39 Ned Frankly replied:
To execute JavaScript serverside within a VBScript ASP page, instead of wrapping the Function in &lt;%...%&gt;, wrap it in this and put it in the head:

&lt;script language='javascript' runat=server&gt;
function ....

....

...
&lt;/script&gt;

Then you can call it with &lt;%=ConvertBR(yourdata)%&gt;

Also - this is NOT executed within the input form - it's called at the moment you want to DISPLAY the data, for instance, on your detail page. The Carriage returns and Line feeds ARE in your table data, you (usually) can't see them unless you convert the whole thing to ascii.

Ned

Edited by - NedFrankly on 06 Sep 2002 15:12:14

Edited by - NedFrankly on 06 Sep 2002 15:12:46

Reply to this topic