简体   繁体   中英

Why am I getting this PHP error?

So here's my full code

    <!DOCTYPE html>
<html>
<body>

<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>

And here's the error I get

Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20

Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?

As a general practice for forms in php, always check if the submit button has been clicked.

First name your submit button:

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

then further in your php:

if (isset($_POST['submit'])) {
    // do your stuff, eg...
    $encrypt = $_POST['in'];
}

EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).

If you really need 2 forms, name your submit buttons differently and then you can call them separately.

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

<!-- ... -->

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

<?php // ...

if (isset($_POST['submit-in'])) {
    // do your stuff, eg...
    $encrypt = $_POST['in'];
}

if (isset($_POST['submit-out'])) {
    // do your stuff, eg...
    $dencrypt = $_POST['out'];
}

EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).

您需要首先检查表单是否已发送,如果尚未发送,则$_POST['in']尚不存在,从而引发错误

可能什么都不是,但是在关闭表单/ form,body / body然后是HTML / html之后,您调用了php脚本

replace this code $encrypt = $_POST['in']; by this $encrypt = @$_POST['in']; this is an error on client server when you upload this file on remote server you will not saw this. use @ sign on the client server when you saw this error in future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM