Method POST - action.php [easy]

Hello,
in the web and only php guide contain a module form for send the input field…I try this code for example in action.php:

           <?php

           $name = $_POST["nome"];

           echo $name;

            ?>

this is the error: undefinited index…error is in the line: “$name = $_POST[“nome”];”…

How do i do…:banghead:

We can’t help much without any code to look at. Where is this $_POST["name"] coming from? Could you please include all the relevant code here?

Are you sure it should not be $_POST['name'] (not nome)?
Also it is usual to test if the form has been submitted.

if($_SERVER['REQUEST_METHOD'] == 'POST'){
     // Form processing code
}
2 Likes

ok…when i open the voto.php gives me error undefined index…:

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

            <input type="text" name="nome"><br /><br />

            <input type="submit" value="invia" name="btn">

            </form>

when i write the request_method gives me White page whitout echo variables…

I do not know what the “web and only php guide” is. An online tutorial somewhere?

You mention two files. “action.php” and “voto.php
The action attribute value of the form in the voto.php file is the voto.php file itself.
You should probably change that to the action.php file, or put the code from the action.php file into the voto.php file.
But because I do not know what the “web and only php guide” is I could very well be wrong about what you’re doing wrong.

don’t look at the names are just for example :slight_smile:

the file php is voto.php…OK.

please help me!

I don’t have enough information to be able to offer further help.
I would need to see more code than the example snippets that you have posted, preferably with consistent file names (real or otherwise)

This is very confusing for potential helpers, as having the wrong file in action would make the script fail.

ok…

this HTML code:

          <!DOCTYPE HTML>
          <html>
            <head>
           <meta http-equiv="content-type" content="text/html; charset=utf-8">
             <meta name="generator" content="PSPad editor, www.pspad.com">
           <title></title>
          </head>
          <body>

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

           <input type="text" name="nome"><br /><br />

            <input type="submit" value="invia" name="btn">

          </form>

          </body>
           </html>

this PHP code:

       <?php

        if($_SERVER['REQUEST_METHOD'] == 'POST'){

         $name = $_POST["nome"];

         echo $name;

         }

         ?>

How do i do,

thank you very much.

1 Like

That should work, if the php code is in the file mentioned in the action attribute.

the php code is in the file voto.php…thank’s

Thanks. It’s a design decision that’s up to you, but I might (albeit as messy as it is) do something like

...
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
  $name = $_POST["nome"];
  echo '<div>' . $name . '</div>';
} else {
?>
<form method="POST" action="voto.php">
<input type="text" name="nome"><br /><br />
<input type="submit" value="invia" name="btn">
</form>
<?php
}
?>
</body>
</html>

Sorry…the problem is Microsoft Edge in Windows 10…the browser don’t link the page…but says to do the download…so many excuse…

Have you tried another browser?

Is PHP running on the server?

yes, i tried it on the browser firefox…and the browser link the page…but edge i don’t know…why?

edge tells me if I want to open the file…:unhappy:

It sounds like you are trying to open the file from the OS instead of from your localhost server.

  • make sure the localhost server is running
  • open a browser
  • type the address (eg. http://localhost/voto.php) into the browsers address bar

the directory is 127.0.0.1/edsa-test/voto.php

i can open the file… but when i send the request from voto.html tells me to do the download…confused please?!

Unless you configure Apache to send .html files through the PHP engine the PHP code will be interpreted as invalid HTML tags and ignored.
The easier and typical fix would be to give the file the .php extension