I read a book about PHP and write the code exactly as in the book and the result is different. More precisely does not work.

The case looks very simple. There is a name entry field, you press the button and it should display the type: "Hello" This and that ".

Here is the form itself:

<form action="scripts/sayHelloWeb.php" method="POST"> <p> <i>Введите своё имя: </i><input type="text" name="name" size="20" /> </p> <p><input type="submit" value="Поприветствуйте меня" /></p> </form> 

And here is the second code directly from the sayHelloWeb.php file:

 <div id="content"> <h1>Привет, <?php echo &_REQUEST['name']; ?></h1> <p>Рады приветствовать вас. Добро пожаловать в начало нашей одиссеи в мире PHP программирования!</p> </div> 

In the end, does not display the name. Page two appears but no name.

Here is the first screen:

Screenshot 1

But the result after pressing the button:

Screenshot 2

And the most incomprehensible to me. Why does the browser see it this way? :

Screenshots3

Thanks in advance to the sages)

  • instead of & _REQUEST ['name']; Try writing this: $ _REQUEST ['name']; - Edward
  • It doesn't work that way either. Produces: <h1> Hello, <! -? Php echo $ _REQUEST ['name']; ? -> </ h1> - Vladimir
  • `$ _POST ['name'];` - Mykola Veriga
  • And so does not work either. For some reason, incomprehensible to me, the browser sees this script as a commented HTML attribute - Vladimir
  • Wrote the answer below - Mykola Veriga

1 answer 1

You have a file in html format, try changing the format in php. Or create a .htaccess file in the root of the site and add the following

 AddType application/x-httpd-php .php .htm .html .phtml 

There was a similar problem: when inserting the php code into the html file and then loading the page, all the code turned into commented out text in html.

The solution was found quickly enough: I just changed the encoding of files with php-code to utf-8. Perhaps this is due to the encodings inside Apache.

https://toster.ru/q/221960 here is the answer, and a description of why

And maybe php just doesn't work for you. Create a simple test.php file

 <?php phpinfo(); 

Show here what you have in the settings.

You can also look at apache2 configs in apache2.conf file. I have it along the path /etc/apache2/apache2.conf. You will have it in the build files.

  • Saved the file with utf-8 encoding, the encoding is also registered in the file itself and there is still no result (( - Vladimir
  • just create the test.php file and output php_info () there. Throw off that there - Mykola Veriga
  • I found my mistake. Because of his carelessness, he created a problem for himself. I started the local server, and the file opened "Open with ..." and I had to open it as a normal web page. As they say - Learning is light, and unlearned darkness. Thank you all for your attention. - Vladimir
  • one
    It happens) I think the question can be deleted - Mykola Veriga
  • It remains only to figure out how to remove the question - Vladimir