Jump to content

Converting textbox new line to html new line


PHPLeader

Recommended Posts

Hi.

 

I want a simple textbox, that when submited, will replace very every new line, with the <br> tag.

 

What will happen, when it submits, it will take the contents of a textbox, add the <br> tag where a new line is suposed to be, and save the string to a MySQL Database.

 

I think this is the easiest way of allowing a user to edit what appears on a website when logged in, but if there is a easier way, then please tell me.

 

What I am trying to do, is a login page, and once logged in, you enter new text into the textbox, click submit, and on the website homepage, the main text will change to what was submitted. But if there is a new line, I think the only way in HTML to make one is to put a <br> tag, so I want the PHP to but that tag wherever there is a new line.

 

Sorry if I am confusing, I am not that advanced at PHP, but I would be very happy if you could supply me with the correct code. Time is running out...

 

If you do not understand me, please tell me :D

 

--

PHPLeader (not)  :P

Link to comment
Share on other sites

I believe this is what you're looking for http://php.net/manual/en/function.nl2br.php  8)

 

Yes, thanks, but there is a small problem. The nl2br changes the /n to <br>, and if the data is entered into a textbox, it is just a normal press of the return key. By return key I mean enter, which you can press at the end of forms, without having to press the submit button.

 

I may be wrong, but  if I am could you correct me?

 

Thanks for your help so far...

Link to comment
Share on other sites

Yes, thanks, but there is a small problem.

 

What is the problem?

 

I may be wrong, but  if I am could you correct me?

 

The only part your wrong about is that nl2br doesn't replace newlines with <br /> tags, it simply inserts <br /> tags after newlines.

Link to comment
Share on other sites

Ok let me try to explain this.

 

lets say we enter this text into the textbox:

 

This is the first line

And this is the second line

this line is the third

and this line is the fourth

 

Now that was easy. I just pressed the enter key on my keyboard. Now correct me if I am wrong, if I submitted that to a DB as a string, when I recall it and but it inside a div on the homepage, it will give this:

 

This is the first lineAnd this is the second linethis line is the thirdand this line is the fourth

 

So I need something simple that will insert a br tag where the enter key was pressed, so as far as I know, the nl2br wont work as there is no "/n" anyway in the text.

 

I want would like the string, once recalled from database, to look like the first quote when inside the div, as my the people using my website have never written a single character of code (maybe a bit, but I doubt it)

 

Thanks again...

 

PHPLeader

 

Note:  If this is forum is using BBCode, that somehow adds a <br> wherever a person entered a new line into the editing box. I dont want any fancy bold or anything, I simply want it to add a <br> wherever the person pressed the enter key, or made a new line.

Link to comment
Share on other sites

So I need something simple that will insert a br tag where the enter key was pressed, so as far as I know, the nl2br wont work as there is no "/n" anyway in the text.

 

This is exactly what nl2br was designed to do. New lines are invisible characters. Try it and see.

Link to comment
Share on other sites

Ok. Not as easy as I thought.  :confused:

 

I might start a new topic if I don't get any replies to this, but I need a bit of help.

 

I have 2 pages. On the first, there is a form.

 

<form method="post" action="submitnl.php">
<textarea name="datatoenter" cols="40" rows="5">
Text here...
</textarea><br>
<input type="submit" value="Submit" />
</form>

 

Now, on page two, submitln.php there is this:

 

<?php
mysql_connect("localhost", "**********", "**********") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("**********") or die(mysql_error());
echo "Connected to Database";

$result = mysql_query("UPDATE new_line_test SET data=$_POST['datatoenter'] WHERE ID='1'") 
or die(mysql_error());

header('Location: http://www.**********.co.uk/newline.php');
?>

 

So page two, I would like it to set the data column on my MySQL on the first row (id='1') to what was send in the form, but MySQL gave me an error, I think it doesnt like the variable. Is there any way of passing it into the Query? I may have an idea, but I have to go so I wanted to quickly post this in case I dont finish.

 

This is the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /customers/e/e/6/mastermovies.co.uk/httpd.www/submitnl.php on line 7

 

I am quite sure I have connected to the database successfully etc...

 

PHPLeader

 

Note: I have starred out important info...

Link to comment
Share on other sites

Strings need to be surrounded by quotes in sql, integers do not. Complex structures (like arrays) should also be surrounded by {curly braces} when within double quoted strings.

 

$result = mysql_query("UPDATE new_line_test SET data = '{$_POST['datatoenter']}' WHERE ID = 1")

 

Also, you should not place user data directly into your queries as it leads to all sorts of security issues. Run $_POST['datatoenter'] through mysql_real_escape_string first.

 

$data = mysql_real_escape_string($_POST['datatoenter']);
$result = mysql_query("UPDATE new_line_test SET data = '$data' WHERE ID = 1")

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.