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! Quote Link to comment Share on other sites More sharing options...
requinix Posted November 29, 2017 Share Posted November 29, 2017 Post your code. Quote 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. Quote Link to comment Share on other sites More sharing options...
BigB Posted November 29, 2017 Share Posted November 29, 2017 (edited) 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=""> } Edited November 29, 2017 by BigB Quote 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). Quote Link to comment Share on other sites More sharing options...
BigB Posted November 29, 2017 Share Posted November 29, 2017 (edited) 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> } Edited November 29, 2017 by BigB Quote 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.) Quote 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. Quote Link to comment Share on other sites More sharing options...
phpmillion Posted November 29, 2017 Share Posted November 29, 2017 Yes, a good point. Quote 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> Quote 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. Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 29, 2017 Share Posted November 29, 2017 (edited) 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> Edited November 29, 2017 by benanamen 1 Quote 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') : '';?> Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 30, 2017 Share Posted November 30, 2017 (edited) 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. Edited November 30, 2017 by benanamen Quote 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 Quote 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 1 Quote Link to comment 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.