Jump to content

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="">

}
Edited by BigB

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 by BigB

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.)

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>

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 by benanamen
  • Like 1

@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') : '';?>

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 by benanamen

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

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.