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>
Related
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Preserve Line Breaks From TextArea When Writing To MySQL
I have created a form that is inserting data into mysql database but I would like the form to insert line breaks (when I press the return key) into the database from the form so I can have paragraphs when displaying the information from the database. At the moment information from the database is just displaying as all in one line despite pressing the return button on the form (text area) when entering the info.how can I do this? I am using php and am quet new so please help.
You need a <textarea> div , then you will be able to capture the new lines, from there you can just get the input you need via normal $_GET or $_POST
What i am guessing is that you just echo the output from the database, use <pre></pre> for formatting your output or nl2br function of 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
I have a form that I generate new input fields to with javascript and the generated fields won't post.
I have made a demo which I stripped the database etc. because it might not have been secure (for injections) which var_dumps POST
http://resk.latvalashop.com/test.php
Try to fill in everything and press a couple times the "+" button to add a couple of rows, fill them and then press "Tallenna" which will POST the form.
the problem is the floating element to right of the main element, the first fields are not generated by javascript and POST as they should, but if you press the "+" and try to post from the newly created forms nothing will be POSTed.
Any help appriciated!
The generated input elemnts are not contained inside of an <form> element, so they are not able to be posted. You should push your <form action="result.php" ... > higher up in the DOM hierarchy.
I think your problem is in your HTML.
The level where you open your <form> element (3 <div>'s deep) isn't the same as where you close it (2 <div>'s deep). That will probably fix it.
You have to wrap those with <form></form> tags.
I have a form that has a <textarea> that I would like to replace with a <div contenteditable="true". Is there an easy way to insert that content into my MySQL database with PHP?
I just implemented this, so I don't know what bugs there are right now, but it does work. I also know that it's pretty hacked together and there is probably a more elegant solution to doing this, but this one works.
I'm making a simple assumptions here: Your textarea contains html elements in it and doesn't mess up your database (using whatever method you like best)
Community Wiki'd because it's something that other people might have better ideas on.
First convert your textareas to divs, don't add extra space between the div and the content (it should be like this: <div id="content" contenteditable="true"><p>First paragraph...
Pick an id for each div that's going to now be contenteditable, on my site I've decided on the really simple content id, and I only have one div that's editable on each page that needs editing (you might need more. Each div needs it's own id).
Make a form, AFTER (or before, just don't put your div inside of the form, actually, that probably doesn't matter, but just to be safe), put a hidden textarea (display:none; visibility:none) with no content.
Add a hidden input field that contains a unique identifier for your database's reference to the current page.
Place a submit button OUTSIDE of the form. This is done because you're going to call a javascript function to put your div's content into the hidden textarea and submit the form after that.
Add an onclick attribute to the submit button, have it call the function to update (mine's upport because it updates my portfolio)
Make the function.
Create a new variable that contains the div's content (var nt = document.getElementById('contentId').innerHTML;)
Add that content to the hidden textarea (document.forms["formId"].nameOfTextarea.value += nt;)
Submit the form (document.forms["upport"].submit();)
Now make the form processor the way you normally would.