PHPLeader Posted April 3, 2012 Share Posted April 3, 2012 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 -- PHPLeader (not) Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/ Share on other sites More sharing options...
marklarah Posted April 4, 2012 Share Posted April 4, 2012 I believe this is what you're looking for http://php.net/manual/en/function.nl2br.php Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334192 Share on other sites More sharing options...
scootstah Posted April 4, 2012 Share Posted April 4, 2012 You should do this conversion on output, otherwise you'll get a bunch of <br>'s the next time someone tries to edit something, which you'll then have to convert back into newlines. Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334206 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 I believe this is what you're looking for http://php.net/manual/en/function.nl2br.php 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... Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334258 Share on other sites More sharing options...
trq Posted April 4, 2012 Share Posted April 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334259 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334269 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 I apologise for my bad spelling and grammar. Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334270 Share on other sites More sharing options...
trq Posted April 4, 2012 Share Posted April 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334272 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 Ok! Thanks a lot! I will try. If it works, I would like to thank everyone for helping! Testing... Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334276 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 Ok. Not as easy as I thought. 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... Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334299 Share on other sites More sharing options...
trq Posted April 4, 2012 Share Posted April 4, 2012 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") Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334302 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 Ok thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334303 Share on other sites More sharing options...
PHPLeader Posted April 4, 2012 Author Share Posted April 4, 2012 Guys, you are amazing. I managed to get everything working. Thank you for all your help, and if I ever need any, I know who to ask. Again, thanks PHPLeader Quote Link to comment https://forums.phpfreaks.com/topic/260304-converting-textbox-new-line-to-html-new-line/#findComment-1334412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.