Leanneschlueter Posted November 29, 2017 Share Posted November 29, 2017 HI All, How to hold the values in text area using PHP validation. Can anyone please help? Thanks! Link to comment Share on other sites More sharing options...
requinix Posted November 29, 2017 Share Posted November 29, 2017 Post your code. Link to comment Share on other sites More sharing options...
phpmillion Posted November 29, 2017 Share Posted November 29, 2017 Usually it's simple: <textarea><?php echo $your_variable_name; ?></textarea> Of course, if you use templating system, it will look a slightly different, but I hope you get the main idea. Link to comment Share on other sites More sharing options...
BigB Posted November 29, 2017 Share Posted November 29, 2017 Yeah, I am curious on this one, not sure I've been doing it right, it is long-winded if it is big form, all I've been doing is catching the value with $_POST OR $_GET. something like... if (isset($_POST["name"])) { <input name="name" type="text" value="<?php echo $_POST["name"] ?>"> }else{ <input name="name" type="text" value=""> } Link to comment Share on other sites More sharing options...
phpmillion Posted November 29, 2017 Share Posted November 29, 2017 BigB, textarea is a different type compared to text input field (you use the second in your example). Link to comment Share on other sites More sharing options...
BigB Posted November 29, 2017 Share Posted November 29, 2017 Hi & Thanks @phpmillion. My bad, oops. And forgot semicolon ; Correction: if (isset($_POST["myTextArea"])) { <textarea name="myTextArea"><?php echo $_POST["myTextArea"]; ?></textarea> }else{ <textarea name="myTextArea">Enter text here...</textarea> } Link to comment Share on other sites More sharing options...
phpmillion Posted November 29, 2017 Share Posted November 29, 2017 In short, your code is correct. Of course, you may want to validate/escape variable submitted by user before displaying it, but it might not be needed in some situation (if text area is used to display some type of raw code, for example.) Link to comment Share on other sites More sharing options...
requinix Posted November 29, 2017 Share Posted November 29, 2017 it might not be needed in some situationIt's always needed. Consider if the submitted value contained a "". And HTML markup in a can still be interpreted. Link to comment Share on other sites More sharing options...
phpmillion Posted November 29, 2017 Share Posted November 29, 2017 Yes, a good point. Link to comment Share on other sites More sharing options...
BigB Posted November 29, 2017 Share Posted November 29, 2017 This what you guys mean? <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <?php if (isset($_POST["myTextArea"])) { <textarea name="myTextArea"><?php echo $_POST["myTextArea"]; ?></textarea> }else{ <textarea name="myTextArea">Enter text here...</textarea> } ?> <input type="submit" name="submit" value="&Submit"> </form> Link to comment Share on other sites More sharing options...
phpmillion Posted November 29, 2017 Share Posted November 29, 2017 No, we mean you should validate/escape variables submitted by user. It's myTextArea in your case. Link to comment Share on other sites More sharing options...
benanamen Posted November 29, 2017 Share Posted November 29, 2017 OP, Naw that's no good. You are needlessly duplicating code. The if/else is completely unnecessary. And get rid of the form action completely. Also take note, I removed the name attribute from the submit button. If your script is depending on the name of a button to be submitted in order for your script to work, then you are doing it wrong and it will completely fail in certain cases. You would need to check the REQUEST method. Also take note I used !empty instead of isset. Take some time and read the manual and see if you can learn on your own why. <form method="post" > <textarea name="myTextArea"> <?= !empty($_POST["myTextArea"]) ? htmlspecialchars($_POST["myTextArea"], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';?> </textarea> <input type="submit" value="Submit"> </form> Link to comment Share on other sites More sharing options...
BigB Posted November 30, 2017 Share Posted November 30, 2017 @benanamen ps. I am not original poster... Also take note I used !empty instead of isset. Take some time and read the manual and see if you can learn on your own why. isset(), empty() and is_null() Maybe you can enlighten us why empty over isset or is_null? Shorthand, nice. <?= !empty($_POST["myTextArea"]) ? htmlspecialchars($_POST["myTextArea"], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : '';?> Link to comment Share on other sites More sharing options...
benanamen Posted November 30, 2017 Share Posted November 30, 2017 Ahh, yes BigB, I didn't notice you are not the OP. Nevertheless, my post was directed at the code you posted. I never mentioned anything about is_null. I don't mind enlightening about anything but I have to wonder if you even made an attempt to understand it on your own. The functions (isset & empty) are the most basic of basic Php. Do you really need enlightening about them and when to use what? By the way, what you refer to as "Shorthand" is called the Ternary Operator. Link to comment Share on other sites More sharing options...
BigB Posted November 30, 2017 Share Posted November 30, 2017 @benanamen Thank's mate, feel enlightened, been a few years since I've been on here or hit the old code, came on here to brush up, thumbs up on your straight out attitude, looking forward to reading your future posts Link to comment Share on other sites More sharing options...
benanamen Posted November 30, 2017 Share Posted November 30, 2017 If you would like to talk about isset, empty and is_null please start your own thread so we don't hijack this one and get off-topic. In the mean time, this chart may help you out if you need it. https://www.virendrachandak.com/demos/php-isset-vs-empty-vs-is_null.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.