How to convert <br /> to newline on Pre-populated textarea - php

I am having issues with <BR /> tags appearing inside of a pre-populated textarea form (with no breaks obviously). So these break tags need to be converted to /n within the textarea. This prepopulated text is being retrieved via the URL which also appears to have the break tags within it as well.
Basically what is going on and what I need:
I am having to get a block of text from the previous page (which was originally pulled from the database) and load that block of text into a textarea field on the form submission page. The user then fills out the rest of the form fields and submits the form, this information is then stored into a specific table in the database.
In order to get the block of text to display properly on the first page, I had to use nl2br to get this section breaking properly on the page. Obviously, now it is outputting that text with breaks instead of new lines.
This means when you click on the link to populate the form on the next page, the link has break tags in it, which in turn gets displayed inside of the textarea field on the next page as well as in the URL.
I am confused on what I need to do to replace those <BR />'s with /n again. It seems like I could use str_replace or preg_replace, but I have yet to find an example of how to use either of those in conjunction with something like:
<textarea rows="10" cols="50"><? if(isset($_GET['text-property'])){ echo $_GET['text-property']; }?></textarea>
Keep in mind that I am still in the process of learning PHP, so I really need specifics and preferably examples if at all possible.
Thanks!

You need to use preg_replace,
<textarea rows="10" cols="50"><? if(isset($_GET['text-property'])){ echo preg_replace('/<br[^>]*?>/si', '\n',$_GET['text-property']); }?></textarea>
Demo

Related

How to prefill text without user´s possibility of deleting it

is there any way to prefill textarea with link of page that user is trying to share but so that he can´t delete it. I am talking about sharing form via email.
The form is obviously done in php.
Edit:
I used the field Edit Summary but I don´t see it anywhere so it may be here twice I can be blind :D
The onlyread function is not what I´ve wanted. User is allowed to write into textarea but the part of the text hasn´t be touchable. As junkfoodjunkie said it seems like the only way to do it. But still posting some advices if you see the whole code may change it.
most inputs can use readonly="readonly" to prevent predefined values from being edited.
<textarea name="verbage" readonly=readonly cols="5" rows="9">
Can't touch this
</textarea>
<input type="text" name='moreverbage" readonly=readonly value="NO EDITING ALLOWED">
If the mail sending is done via PHP, just have that default text be part of the server-side send-mail script, and put it in if the text in the message / textarea doesn't contain the default text. You cannot trust client side solutions, and all of the solutions mentioned above is removed by a simple code-editor (Firebug, for instance), before clicking submit.
Check the content in the mail-sending script, and if the default text isn't there, either replace or add/prepend the text you want.

Can't pre-populate textarea fields [duplicate]

This question already has answers here:
Setting value of a HTML form textarea?
(3 answers)
Closed 7 years ago.
I have a form that lets a user submit things like "header" and "description_1". I take this information an build a simple one page website for them. There's a link that lets them edit the page, which reads the fields back from the database and pre-populates the form with it. The form is pre-populated by setting the value attribute of the fields to the data pulled back from the database. This works fine for input fields (485-490 below) but it doesn't work for textarea fields (493-498 below). I've verified that $description_1 does have the right text but setting the textarea value to this text doesn't get it to show up when the form is displayed. Does anyone know what the problem might be? Thanks.
You are close, text areas are a bit different than input fields. While you set the value of an input, you populate a text area by echoing text between the opening and closing the text area tags.
<textarea><?php ehco $foo; ?></textarea>
Text areas work differently than other input fields. Default values are set by adding your text in between the HTML tags.
<textarea><? echo $description_1 ?></textarea>
It's been a while since I 've done this but I think you're going to run into some trickiness with carriage returns. To do that you need to convert the values with nl2br. I think it's like this:
<textarea><? echo nl2br($description_1) ?></textarea>

How do I echo the text of one input element to another input element in the same form using PHP and HTML?

I have two input elements:
one for typing
another for display the submitted text as a tag
These two input elements are on the same form.
I would like for a user to enter text into the first input element and then when they press "enter" on the keyboard the text is displayed in the second input field as a tag.
In the second input element I tried to execute this code but it is not working:
" />
Note: I am using Bootstrap 3 library.
Any help would be much appreciated.
use JS or jQuery for it.(something like this $('#yourelement').on('keyup',function(event){.....})); issues like this should be solved on client side, not on serverside using PHP.
to solve this issue with html+PHP only, you have to reload the page all the time after Enter press (you have to make a form and on Enter form will be submitted, then get this data in PHP and put in form again, BUT this is really bad way programming)
It won't work with PHP. PHP does not run in the browser only on the server side when the page is loaded. You need to use a client side language.

How do I display html tag in texts from Database

I am trying to save the data in DB through the user. User can add html markup so some data will be like <em>texts</em> and sometimes <br> tag is added.
I run into a problem when I extract the data out of the DB and display it to the user, I got a blank field for <br> tag instead of showing '<br>'. Is there a way to display <br> tag as a text in my web browser when the user get data from the DB? Thanks a lot!
Always use htmlspecialchars() for escaping user's data output.
You could wrap it in <pre> tags.
The HTML <pre> Element (or HTML Preformatted Text) represents preformatted text. Text within this element is typically displayed in a non-proportional font exactly as it is laid out in the file. Whitespaces inside this element are displayed as typed.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

Populate sign-up form field with an uneditable/undeletable sentence

One field in our website's sign-up form is occasionally left blank yet we need it to include one sentence that for legal reasons must not be editable. This is then later used as part of the user's profile and will sit in the same place if they added content to that field too during the initial sign-up.
How can we populate this single field with an uneditable/undeletable sentence, yet allow text to be added above it if the user chooses to?
Currently the field in question looks like this:
<textarea name="description" id="eBann" rows="2" maxlength="1500" cols="20" onKeyUp="toCount('eBann','sBann','{CHAR} characters left',1500);"><?php echo $description;?></textarea>
Why don't you just use the input for the optional extras and put the uneditable part in the HTML directly below it? For the prompt use something like: "Here's our sentence, if you want to add your own comment, enter it here." In the form processing script, just concatenate it with the static sentence.
Any HTML form can be changed by the user (even if it says readonly). The only way to do this safely is either server-side when they submit the form, or server-side when you get and output the information.
EDIT:
With dual languages (I'm assuming human language, not programming!), you'll definitely want to output the string with the rest of the information, and never store it in a database. That'll help you in case you ever want to change what the string is, and also will allow localisation for different languages.

Categories