Jump to content


Photo

how to get rid of the <BR> code on a Form


  • Please log in to reply
7 replies to this topic

#1 FaTTzZO

FaTTzZO
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 October 2006 - 07:13 PM

hello there

im new here, and ive search the forums trying to find and answer to my problem, but i didnt find any :P

so here is my problem.

i have a form that submits data to mySQL, and i want to switch to <BR> when you hit enter, so i did this

    $description = $_POST['description'];
    $description = str_replace("
","<br>","$description");

and it works, but now i have a form that edits the Description, but when i get the data from mySQL i get the info and the <BR> codes. so when i submit the form again to edit something it uploads 2 <BR>.

what i want is to get the data form mySQL but i want it to appear without the <BR>, ive tried:

    $description = $_POST['description'];
    $description = str_replace("<br>","
","$description");

before i call the edit form. but that doesnt works :S im kinda lost :P

i need help please.

thank you =-]

excuse my english, is not leet :P

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 17 October 2006 - 07:16 PM

That will work
str_replace()
just trap all the incoming content in a variable, run it through string replace, replace <br />
with notags or something

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 October 2006 - 07:19 PM

You want to use the function nl2br() when you display the value on the browser. Leave it alone when storing in the DB.

Ken

#4 FaTTzZO

FaTTzZO
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 October 2006 - 08:54 PM

look this is the code im using to upload the data

<?php
if($_POST['Submit']){
include("sql.php");
$name = $_POST['name'];

$description = $_POST['description'];
$description = str_replace("
","<br>","$description");

$ok = mysql_query("INSERT INTO `table` ( `id` , `name` , `description`)
VALUES (
'NULL', '$name', '$description'
);");

so when someone use "Enter", it transform that "Enter" into a <BR>
now i want to add an edit form, so i can edit de description, ok?

but when i call the form and the data it shows me the "Enter" and the <BR> and i want to get rid of the <BR> cuze if i dont and i submit the data, now it multiply the <BR> and insted one i got <BR><BR> :|

so i did

     
<? $description = str_replace("<br>","
","$description"); ?>

 <td align="right">Description:</td>
      <td align="left"><textarea name="description" cols="60" rows="10" class="box" id="description"><?=$description;?></textarea></td>

but that dont works :|, now i dont know what to do :P im learning php. maybe its a silly question, but i cant find the answer.

thank you

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 18 October 2006 - 09:59 PM

Do not do the str_replace when you store the description, use the nl2br() function when you display it:
<?php
if($_POST['Submit']){
include("sql.php");
$name = mysql_real_escape_strimg(stripslashes($_POST['name']));

$description = mysql_real_escape_string(stripslashes($_POST['description'])); //escape dangerous characters

$query = "INSERT INTO `table` ( `id` , `name` , `description`) VALUES ('NULL', '$name', '$description')";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); ?>

When you want to display the value:
<td align="right">Description:</td>
      <td align="left"><textarea name="description" cols="60" rows="10" class="box" id="description"><?php echo nl2br($description);?></textarea></td>

Ken

#6 FaTTzZO

FaTTzZO
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 October 2006 - 10:55 PM

thx for your quick reply. but it is now working :(

ive replace the functions you said.

but now when y type something like this

this
is
a
test


i get something like this

thisisatest


now, when i open the edit form, i get

this(br /)
is(br /)
a(br /)
test(br /)


i dont type br with <> cuze the forum will put a new line :P

what i want, is something like this forum :P

i want to type

this
is
a
test


and when i query the result i want to see

this
is
a
test


and when i query the value into the edit form, i want to see

this
is
a
test


so i can edit it with no problems ;)

thx in advance

#7 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 18 October 2006 - 11:10 PM

Lets go over this from the start.

When you take data directly from the the form and store in the database, line breaks are "remembered" in the code.  You dont need to add them at all

When you want to display in normal HTML, you use nl2br() to add the linebreaks etc back in

If you are displaying the information in a textarea again, DONT use nl2br, just put the data in directly from the database
If you're not part of the solution, you're part of the precipitate

#8 FaTTzZO

FaTTzZO
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 October 2006 - 02:33 PM

ooohhhhh... ok... thank you pretty much. and all of you guys ^_^ =-]

:D :D :D :D :D :D :D :D :D :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users