0

I am using php to simply capture the value inside a textarea and print it to body of the document. Below is the code I am using:

  <!DOCTYPE html>
    <html>
    <head>
        <title>Welcome</title>
    </head>
    <body>

    <form method="post">    
    <textarea name = "ontology" value = "Copy the ontology file here...."></textarea>
    </form>
    <div id = "result">
    <?php
        echo $_POST["ontology"];
    ?>
    </div>
    </body>
    </html>

Below is the tragic output :( :

Notice: Undefined index: ontology in /var/www/html/input_onto.php on line 13

It did not work for simple <input> tag too. Can anyone tell what is happening? It's silly, but I have been breaking my head for this. Please help me out and thanks in advance :)

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Horcrux
  • 5
  • 7
  • 1
    Did you submit the form? – Louay Alakkad Jan 04 '16 at 23:05
  • http://php.net/manual/en/function.empty.php - http://php.net/manual/en/function.isset.php - http://php.net/manual/en/control-structures.if.php ought to get you started, along with its *often asked* duplicate question that yours was closed with. – Funk Forty Niner Jan 04 '16 at 23:10
  • I never though it could be that small. I added a submit button and tried redirecting to the php file having the same php script. It worked :). Just out of curiosity- what was happening if i was not submitting it? – Horcrux Jan 04 '16 at 23:12
  • 1
    Nothing... the post only works when you literaly post something ... – Angelo Berçacola Jan 04 '16 at 23:13
  • because @Horcrux PHP is loading/looking at your POST array as soon as the page is loaded. Either use conditional statements, or use 2 different files. One for the form and one for the PHP. – Funk Forty Niner Jan 04 '16 at 23:13
  • what if I try to just initialize some variable with the value inside the text area? I am not posting anything in this case. @Fred-li- do you mean i'll have to use conditional like `if(isset(var))`?? – Horcrux Jan 04 '16 at 23:16
  • @Horcrux Yes, something like that. However, it's best to use `if(!empty($_POST['var'])){...}`. `isset()` is mostly better for radio buttons/checkboxes. `empty()` checks for both conditions. The `!` is the NOT operator. Which translates to `if NOT empty` ;-) – Funk Forty Niner Jan 04 '16 at 23:18
  • thanks a lot @Fred-li- . As of now it works fine for me - posting and capturing the value. – Horcrux Jan 04 '16 at 23:26
  • you're welcome @Horcrux – Funk Forty Niner Jan 04 '16 at 23:29

0 Answers0