Textbox help pu-lease

This is a discussion on "Textbox help pu-lease" within the JavaScript Forum section. This forum, and the thread "Textbox help pu-lease are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jun 17th, 2008, 12:00
New Member
Join Date: Nov 2007
Location: Hungary
Age: 22
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Textarea help pu-lease

Hi!

I've made some helping requests here in this forum, and I always get help from you guys. I'm counting on you this time too.

Here's the problem I need help with.

I have a textarea and I'm sending the data inside with a POST and get it on another page with .php as a string.

I need javascript to check how many lines there are and put a certain string (for example: "<br />" at the end of every line.

Is it possible to do this?

We can maybe use another hidden form element to store the data "textarea with the <br>s"...

So any help here would be much appreciated.

THANK YOU!

Last edited by hegerp; Jun 17th, 2008 at 12:18.
Reply With Quote

  #2 (permalink)  
Old Jun 18th, 2008, 20:39
Aso's Avatar
Aso Aso is offline
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,114
Blog Entries: 2
Thanks: 5
Thanked 35 Times in 32 Posts
Re: Textbox help pu-lease

If you're using PHP, then nl2br (new line to break) should sort you out.

PHP: Select all

$string 'This is
text
with line
breaks'
;

echo 
nl2br($string); 
will echo;
Code: Select all
This is<br />text<br />with line<br />breaks
HTH
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote
  #3 (permalink)  
Old Jun 18th, 2008, 22:36
New Member
Join Date: Nov 2007
Location: Hungary
Age: 22
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Textbox help pu-lease

Okay. I messed the first post up a little...

The main thing I want to do is to connect to a database and get the text data from there and then output the data with \n as a javascript alert box.

I made a little sample code and it does not work!

Can you please have a look at it for me?

Code: Select all
<?php
// connect and get the data is fine
$textarea = "Hi!
My name is Bond.
James Bond!"; // This is the text
 
 
$text_separated = nl2br($textarea); // Do the trick you showed me!
$array = explode("<br />",$text_separated); // I make an array from the textarea divided the parts that need a line break
 
echo "<script type='text/javascript'>alert('"; // start of the javascript alert
 
foreach($array as $key => $value){
echo $value; // write the text
?>\n<?php // to show the line break
};
 
echo "')</script>"; //finishing the javascript
?>
The most hurting part is that if I change the $value into a random string it does fine!

Last edited by hegerp; Jun 18th, 2008 at 22:39.
Reply With Quote
  #4 (permalink)  
Old Jun 19th, 2008, 11:50
Aso's Avatar
Aso Aso is offline
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,114
Blog Entries: 2
Thanks: 5
Thanked 35 Times in 32 Posts
Re: Textbox help pu-lease

Found the problem - posting solution shortly
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote
  #5 (permalink)  
Old Jun 19th, 2008, 12:08
Aso's Avatar
Aso Aso is offline
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,114
Blog Entries: 2
Thanks: 5
Thanked 35 Times in 32 Posts
Re: Textbox help pu-lease

Ok, the problem lied in your foreach statement. If you look at the source code it generated, you'll see that the JavaScript code looked like this;
HTML: Select all
    <script type='text/javascript'>alert('Hi!\n
    My name is Bond.\n
    James Bond!\n')</script>
In JavaScript, you can't carry the code onto a new line. So in this case, your alert statement broke when it hit the end of the first line.

This new slice of code builds the alert statement first, combining the lines back together into a new, single-line string (with each piece now separated with a'\n' line feed)
PHP: Select all

<?php

// connect and get the data is fine
$textarea "Hi!
            My name is Bond.
            James Bond!"
;
    
$text_separated nl2br($textarea); // Do the trick you showed me!
    
$line_array explode("<br />",$text_separated); // I make an array from the textarea divided the parts that need a line break
    
foreach ($line_array as $line) {
           
    
// we need to remove the carriage returns still in each line and trim unwanted space
    
$line trimstr_replace("\r"''str_replace("\n"''$line) ) ); 
           
    
// recombine lines (seperated with un-parsed line break \n) into a new string 
      
$new_textarea .= $line '\n';
}
    
echo 
"<script type='text/javascript'>";
    
echo 
"alert('" $new_textarea "');"// alert the new string
    
echo "</script>" 
    
?>
Now your source code will look like this;
HTML: Select all
<script type='text/javascript'>alert('Hi!\nMy name is Bond.\nJames Bond!\n');</script>
Any questions, please ask!
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)

Last edited by Aso; Jun 19th, 2008 at 12:10.
Reply With Quote
  #6 (permalink)  
Old Jun 19th, 2008, 13:43
New Member
Join Date: Nov 2007
Location: Hungary
Age: 22
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Textbox help pu-lease

Thank you very much!

You are very helpful. I appreciate sites like this.
Reply With Quote
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
textbox in datalist ktsirig ASP.NET Forum 0 Dec 14th, 2007 15:40
TextBox problem accessman Databases 1 Oct 15th, 2005 01:19
Error Message: TextBox accessman Databases 1 Oct 15th, 2005 01:07
javascript textbox changer Monie JavaScript Forum 8 Aug 19th, 2004 06:46
VS.NET, TextBox, overlay, DataGrid Smokie ASP.NET Forum 10 Jul 27th, 2004 12:17


All times are GMT. The time now is 07:20.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43