Jump to content

Holding values in textarea PHP


Leanneschlueter

Recommended Posts

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

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

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

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

@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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.