-1

I have a html form that is checked by PHP. When the page loads php errors stating missing variable because form has not been submitted. It works fine once I put something in the form and submit. But on first page load it errors.

Error on page load Notice: Undefined index: sk in index.php on line 65 Notice: Undefined index: sk in index.php on line 78

<form action=# method="post">
Source Key: <input type="text"  name="sk">
</br>
<input type="submit">
</form>



<?php


     if(isset($_POST['submit']))
{
        $test = $_POST['sk'];//assigning your input value

        if(isset($test))
        {
            $fr=fopen("php://stdin","r");   // open file pointer
            $input = fgets($fr,128);        // read 128 char max
            $input = rtrim($input);         // trim any trailing spaces
            fclose ($fr);                   // close the file handle
            $result =  $input;              // return the text 
        }
    } 
?>

You have entered : <?php echo $_POST["sk"]; 
echo "</br></br>";
Jay Parker
  • 47
  • 3
  • 9

2 Answers2

1

You can use isset

$value = (isset($_POST['value']) ? $_POST['value'] : 'alternative';
Lulceltech
  • 1,662
  • 11
  • 22
0

You have to name your submit

<input type="submit">

to:

<input type="submit" name="submit">

EDITED

then replace that part of ur php with:

<?php 
if(isset($_POST['submit'])) {
    echo "You have entered : ";
    echo $_POST['sk'];
    echo "</br></br>";
}
?>
Ralph
  • 337
  • 4
  • 13
  • Your submit button does not need a name... – Lulceltech Mar 28 '19 at 18:52
  • @TimHinz https://stackoverflow.com/questions/7775512/using-ifisset-postsubmit-to-not-display-echo-when-script-is-open-is-not – Ralph Mar 28 '19 at 18:54
  • 1
    @TimHinz Actually it does, because of the `if(isset($_POST['submit']))` part (Although we both know that there's also an issue in this part of the code `echo $_POST["sk"]`) – Alon Eitan Mar 28 '19 at 18:55
  • @AlonEitan Yeah idk why he downvoted me for that when it's common knowledge... – Ralph Mar 28 '19 at 18:55
  • LMAO the only reason he needs it is because @AlonEitan is right. Otherwise there is 0 reason to name a submit button. In HTML 5 you do NOT need to name a submit button under normal circumstances. – Lulceltech Mar 28 '19 at 18:56
  • @TimHinz In the code he provided, he obviously requires it which is why I mentioned it. Had it not been needed I wouldn't have replied – Ralph Mar 28 '19 at 18:57
  • And you didn't point that out because? You linked to a 7 year old question that in no way pointed out that he did that. – Lulceltech Mar 28 '19 at 18:59
  • U r right but not related to submit – devpro Mar 28 '19 at 18:59
  • @TimHinz He didn't name his submit while using isset so I told him he has to name it. I don't understand what you're trying to argue here when we both know that isset submit wont work without having named your submit button. – Ralph Mar 28 '19 at 19:09
  • Because until @AlonEitan pointed out im pretty sure you were just taking a guess... lol – Lulceltech Mar 28 '19 at 19:10