Print message on page after echo [closed]

1

When filling out an HTML form, I put it to appear in PHP on the other page. However, when filling out the form the other page is called and the values are only in the URL and the echo is not shown on the page. Here is the code I used in HTML and PHP

<form method="get" action="p1.php"> 
   Valor <input type="number" name="ds"/>
   <input type="submit" value="Envie"/>
</form>

<?php
  $valor = $_GET["ds"];
  echo "O valor inserido é $valor";
?>
    
asked by anonymous 18.10.2016 / 17:21

3 answers

2

I recommend using the POST method

HTML - <form method="POST" action="p1.php"> 
                Valor <input type="number" name="ds"/>
                <input type="submit" value="Envie"/>
            </form>

PHP - <?php
        $valor = $_POST["ds"];
        echo "O valor inserido é ".$valor;
        ?>
    
18.10.2016 / 17:24
2

Your code is correct, it should work perfectly.

Some common causes that can affect beginners:

  • Do not have an interpreter installed on your machine;

Solving this is simple. Just install a server emulator, such as WAMP: Wamp's official site

  • Forget to put PHP code inside the file and organize it into folders.

In your case, it is enough that your p1.php is with PHP code, and in the same folder as your index.php ;

  • Problems with CSS;

Make sure your CSS does not interfere with your page.

I recommend giving var_dump to your $valor . Just add this below your echo : var_dump($valor) . It will return the variable type and its value.

    
18.10.2016 / 19:03
2

Hello, your code is right. What you have to do is make sure you are running the page on the server, you have to be able to access your page for example http://localhost/index.php instead of file:///C:/xampp/htdocs//index.php

    
18.10.2016 / 18:46