Problem to get Hidden Input values in PHP

Asked

Viewed 74 times

-1

First, I made two forms with an input of type Hidden each:

<form action="painel.php" method="POST">
    <input type="hidden" value="aluno" name = "auth">
    <input type="submit" value="Aluno">
</form>
<br>
<form action="painel.php" method="POST">
    <input type="hidden" value="adm" name = "auth">
    <input type="submit" value="Adm">
</form>

Afterwards, I tried to get the value of a Hidden input at the beginning of the page "dashboard.php":

<?php
    session_start();
    $auth = $_POST['auth'];
?>

But this problem arises:

Warning: Undefined array key "auth" in C: xampp htdocs sites panel work.php on line 3

What I do?

  • If you take the hidden, he sends right?

  • The error message itself is telling you what to do. Give an echo '<pre>'; print_r($_POST); or a var_dump($_POST); in.php panel and see what you’re submitting in the form.

  • Thrnk, I don’t know how to do without Hidden, because I need this information to check the login information in the database, if it’s student it checks in the student table and if it’s Adm it checks in the Adm table.

  • Marcos Xavier, I gave an echo and a print_r but gave the same problem.

  • What if you leave together the same (=)? What is so name = "auth" so that way name="auth"?

  • Adventistaam, it worked kkkkk I don’t even know how

  • Thank you to everyone who tried to help me, have a good day.

  • 1

    This does not interfere with anything in php, I believe it is some other change you have made in your code. Take the test as it was before and put it here. I am using php 7.3

  • @Thauanfernandesaraújo I’m glad it worked out

  • I use php 8.0.2, I put it the way it was before and it worked. I don’t know what it was

  • Beware of rsrs caches

Show 6 more comments

1 answer

1


Thauan,

$_POST['auth'] only comes into existence after you first click on one of the two buttons (send the form). The first time you open the page it doesn’t exist and gives the alert.

If you sent the form once and kept pressing F5 to check the changes in PHP, the browser forwards the form data and it seems that solved.

To avoid the alert check if the parameter has been passed. Change your line $auth = $_POST['auth'] for:

  • $auth = (isset($_POST['auth']) ? $_POST['auth'] : "nenhum");
  • or: $auth = $_POST['auth'] ?? "nenhum";

Important remark:

If you are using input hidden To define the assignment, you put all the security in the user’s hand. Check the PHP side with the database itself informing the role of the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.