Print message on page after echo

Asked

Viewed 514 times

1

When filling out an HTML form, I put it to appear in PHP on the other page. But when filling the form is called the other page and the values are only in the URL and the "echo" is not shown on the page. Below 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";
?>
  • 1

    That’s all you have in your code ?

  • Yeah, that’s right, that’s right.

  • This P1.php page is the same as the one you put in this code?

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;
        ?>
  • Nothing appears yet. Even using the POST method.

  • if you have printed only the text it appears???

  • Nothing comes up either.

  • as you are only having the text printed???

  • Just by "echo "phrase";"

  • check if you are editing the correct file, in case P1.PHP, see if it is in the same folder as the form and also check your PHP interpreter

  • They are in the same folder. Is it a mistake with my machine?

  • Check the PHP interpreter, is using xampp or something? see if they are initialized and the site should be in the htdocs installation folder.

  • I am using Xampp itself, APACHE and Mysql are loaded. The folder is in htdocs.

  • You wouldn’t happen to have any css indicating that the body’s text color is white ? :P

  • Ygor Anjos' answer works correctly. The problem has to be with your server. I copied the code that Ygor put in here and tested it, and it worked perfectly. :/

  • Also enable PHP error display.

  • Place the code snippet error_reporting(E_ALL); in your php code, following @Marcos Xavier’s idea.

  • I did everything and nothing, the value goes to the url, but on the page does not print anything. I will test with Easyphp.

Show 9 more comments

2

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

  • I copied your code and it worked smoothly

  • Dude, now printed echo on the screen, but with error. It appears like this

  • 1

    Notice: Undefined index: v in C: xampp htdocs Phpproject5 go.php on line 14

  • Line 14 has the following remark: "Do not access Superglobal Array $_GET Directly" . Line 14 is the get "$value = $_GET["v"];". What can it be?

  • 1

    It worked, I had changed to see if it worked. It was the same server access location. Thanks, man.

  • The problem was in the url I was searching the file, I switched to localhost and it worked. Thank you very much, guys.

  • A coreta way of impremir variable values in php is to check if the varial exites. Ex: if isset( $_GET["v"]){ echo $_GET["v"]; } http://www.w3resource.com/php/function-reference/isset.php

Show 2 more comments

2

Your code is correct, it should work perfectly.

Some common causes that may affect beginners:

  • Not having an interpreter installed on your machine;

Solving this is simple. Simply install a server emulator such as WAMP: Wamp’s official website

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

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

  • Problems with the CSS;

Make sure CSS doesn’t interfere with your page.

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

Browser other questions tagged

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