POST method does not work!

Asked

Viewed 2,634 times

1

Guys, I’m making a simple code... I just want to get the information typed in the form. But it doesn’t work, the GET works... the Post doesn’t.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="index.php" method="POST">
        Nome: <input type="text" name="nome" value="1">
        Idade: <input type="text" name="idade" />
        <input type="submit" value="POST"/>
    </form>
    <?php

    $nome = $_POST["nome"];
    echo $nome;
    ?>
</body>
</html>

I’ve tried doing it in a separate file, it didn’t work either, is it something in php.ini? I’m using the XAMP tbm...

3 answers

1

Use this form below and submit the form and see if there will be an error. Here at home it worked out good...

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="#" method="POST">
            Nome: <input type="text" name="nome" value="1">
            Idade: <input type="text" name="idade" />
            <input type="submit" value="POST"/>
        </form>
        <pre>
            <?php
            // Habilitar a exibição de erros em tempo de execução.
            ini_set("display_errors", "on");
            // Depurar a super global POST.
            print_r($_POST);
            // Testar a tipagem da super global POST.
            var_dump($_POST);
            ?>
        </pre>

    </body>
</html>
<?php
$nome = $_POST["nome"];
$idade = $_POST["idade"];
echo "Olá $nome, você tem $idade ano(s)... legal!!!";
?>

The Return will be:

Array ( [name] => 1 [age] => 33 ) array(2) ! ["name"]=> string(1) "1" ["age"]=> string(2) "33" } Hello 1, you have 33 year(s)... cool!!!

0

Locate in your php.ini the line enable_post_data_reading. Change to on:

enable_post_data_reading = on

PHP documentation:

Disabling this option causes $_POST and $_FILES to not be filled. The only way to read posted data will be through the stream wrapper php://input. This can be useful for intermediary requests or to process POST data in an efficient way in allocated memory terms.

-4

Hello,

If you are entering PHP on the same page of the form, you should change your form action:

OF:

<form action="index.php" method="POST">

FOR:

<form action="<?=($_SERVER['PHP_SELF'])?>" method="POST">
  • Why exactly would this solve the problem? What is the difference between the PHP_SELF and put the file name straight? What about the XSS problem?

  • Have you tried enabling php.ini errors? display_errors = on

Browser other questions tagged

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