FutureQuest, Inc. FutureQuest, Inc. FutureQuest, Inc.

FutureQuest, Inc.
Go Back   FutureQuest Community > General Site Owner Support (All may read/respond) > General Coding/Development
User Name
Password  Lost PW

Reply
 
Thread Tools Search this Thread Display Modes
Old 01-10-2001, 04:04 PM   Postid: 22369
dank
Registered User

Forum Notability:
410 pts: Community Guru
[Post Feedback]
 
Join Date: Mar 2000
Location: MWV
Posts: 3,986
JavaScript handling newline characters

I have a script that uses onClick to send different values to a form field through JavaScript.  One problem I've run into is when using newline characters (\n) in the passed value (for representing blank lines in the textarea).  Doing so creates blank lines in the actual html source code, which is what the JavaScript sees client-side, resulting in something that apparently cannot be interpreted.

I can sort of get around this by placing
tags in the passed value instead of \n, then doing an ereg_replace to switch
back to \n once the form is submitted, but that simply places
's into the form textarea, whereas blank lines would be much preferred.

So, to make a long story short, does anyone know of a way to make JavaScript understand everything between single quotes, regardless of whether or not it is on the same line?

edit:  The next best thing I can think of is to refresh the page with onClick, passing a variable back in for which template to load (the JavaScript would only see the variable, not the actual template with newlines), but I'd rather not have to reload the page every time...

Thanks,
Dan
[This message has been edited by dank (edited 01-10-01@4:07 pm)]
dank is offline   Reply With Quote
Old 01-10-2001, 06:17 PM   Postid: 22370
dank
Registered User

Forum Notability:
410 pts: Community Guru
[Post Feedback]
 
Join Date: Mar 2000
Location: MWV
Posts: 3,986
Well, I figured out a decent solution, although it loses some of the flexibility I had hoped for.  Instead of passing a variable (php; so the page (i.e. the JavaScript) sees it as a string of text once loaded in the browser) to the JavaScript function through onClick, I set it up to pass a number corresponding to which radio button was selected.  Then, the former php variables are placed in the JavaScript function as array variables, of the form:

var template = new Array();
template[1] = "blah...";

The passed in number is received as "text", so the JavaScript passes the appropriate string back to the form's textarea as template[text].  Seems a bit loopy, but it works...

Unfortunately, working with variables in JavaScript seems much less flexible than in PHP, so I'm not sure how well things will work like reading a file into and/or appending multiple lines into a variable...

Better ideas?

Dan
dank is offline   Reply With Quote
Old 01-10-2001, 10:30 PM   Postid: 22371
Rich
Merchant Rep
 
Rich's Avatar

Forum Notability:
153 pts: Ambassador of Goodwill
[Post Feedback]
 
Join Date: Nov 1998
Location: Indiana, USA
Posts: 1,658
Text fields are usually filled in by the user typing in them and I *think* special characters (like \n) are encoded in the process. So, if you are going to populate the text field programmatically, you would need to encode the characters.

Perhaps if you told us more about these text boxes that are populated by your script based on a radio button being pressed?

Rich
Rich is offline   Reply With Quote
Old 01-10-2001, 11:11 PM   Postid: 22372
dank
Registered User

Forum Notability:
410 pts: Community Guru
[Post Feedback]
 
Join Date: Mar 2000
Location: MWV
Posts: 3,986
Hi Rich,

Getting the newline (\n) characters in and out of the form field is not a problem, per se, it's getting them into a variable that will be passed through a JavaScript function and into the form field that is problematic.  Let's see if I can illustrate in a clear fashion...


Code Sample:

<script language=&quot;JavaScript&quot;>
<!--
function setTemplate(text) {
     document.email_form.message.value = text;
}
//-->
</script>

<?php
$template[1] = &quot;string of text...&quot;;
$template[2] = &quot;string of text...&quot;;
$template[3] = &quot;string of text...\n\n&quot;;
$template[3] .= &quot;string of text, continued...&quot;;
?>

<form>
<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;1&quot; onClick=&quot;setTemplate('<?php echo($template[1]); ?>')&quot;> Template 1

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;2&quot; onClick=&quot;setTemplate('<?php echo($template[2]); ?>')&quot;> Template 2

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;3&quot; onClick=&quot;setTemplate('<?php echo($template[3]); ?>')&quot;> Template 3

</form>




That will only work if the \n newlines in $template[3] are replaced with
's.  The reason seems to be that once PHP processes the file and sends it to the browser, the \n characters have already been replaced by their html equivalent, resulting in something that looks like:

onClick=&quot;setTemplate('string

rest of string')&quot;

which kills the JavaScript.  

The solution I outlined in the second post is the following:


Code Sample:

<script language=&quot;JavaScript&quot;>
<!--
function setTemplate(text) {
     var template = new Array();
     template[1] = &quot;string of text...&quot;;
     template[2] = &quot;string of text...&quot;;
     template[3] = &quot;string of text...\n\nstring of text continued...\n\n&quot;;
     document.email_form.message.value = template[text];
}
//-->
</script>

<form>
<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;1&quot; onClick=&quot;setTemplate('1')&quot;> Template 1

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;2&quot; onClick=&quot;setTemplate('2')&quot;> Template 2

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;3&quot; onClick=&quot;setTemplate('3')&quot;> Template 3

</form>




That apparently works because the JavaScript gets to handle the newline characters before the browser tries to interpret it (?).  It's just much more limited what I can do with the templates in that fashion...

(edited to simplify the JavaScript examples)

Dan
[This message has been edited by dank (edited 01-10-01@11:14 pm)]
dank is offline   Reply With Quote
Old 01-12-2001, 11:01 AM   Postid: 22373
rsh
Registered User

Forum Notability:
10 pts: User-friendly
[Post Feedback]
 
Join Date: Nov 2000
Posts: 16
You'll need to escape the escape character that designates the new line -- have you tried this?


Code Sample:

<?php
$template[3] = &quot;string of text...\\n\\nstring of text continued...\\n\\n&quot;;
?>

...

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;3&quot; onClick=&quot;setTemplate('<?php echo($template[3]); ?>')&quot;> Template





[This message has been edited by rsh (edited 01-12-01@12:16 pm)]
rsh is offline   Reply With Quote
Old 01-12-2001, 01:06 PM   Postid: 22374
dank
Registered User

Forum Notability:
410 pts: Community Guru
[Post Feedback]
 
Join Date: Mar 2000
Location: MWV
Posts: 3,986
That's a good suggestion, but it doesn't seem to work.  The double (escaped) slashes show up in the html source code, keep the variable's text string all on one line, get passed through to the JavaScript function, and are spit out in the form textarea as &quot;\n&quot; as text (the slash no longer escaped).  I'm not quite sure how that last part is happening...

I know JavaScript offers pattern matching regular expressions, but I haven't seen anything for matching and replacing.  Is there anything of that sort available?

I'm thinking I'll veer toward full-blown, parseable templates, and use the JS method for small add-ons.

Dan
dank is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 visitors)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 11:14 PM.


Running on vBulletin®
Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.
Hosted & Administrated by FutureQuest, Inc.
Images & content copyright © 1998-2013 FutureQuest, Inc.
FutureQuest, Inc.