Jump to content

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


FaTTzZO

Recommended Posts

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

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

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:

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

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
Link to comment
Share on other sites

look this is the code im using to upload the data

[code]<?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'
);");[/code]

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

[code]   
<? $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>[/code]

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
Link to comment
Share on other sites

Do not do the str_replace when you store the description, use the nl2br() function when you display it:
[code]<?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()); ?>[/code]

When you want to display the value:
[code]
<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>
[/code]

Ken
Link to comment
Share on other sites

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

ive replace the functions you said.

but now when y type something like this

[quote]this
is
a
test[/quote]

i get something like this

[quote]thisisatest[/quote]

now, when i open the edit form, i get

[quote]this(br /)
is(br /)
a(br /)
test(br /)[/quote]

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

what i want, is something like this forum :P

i want to type

[quote]this
is
a
test[/quote]

and when i query the result i want to see

[quote]this
is
a
test[/quote]

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

[quote]this
is
a
test[/quote]

so i can edit it with no problems ;)

thx in advance
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.