N
N
nanashi21602015-06-05 08:38:34
PHP
nanashi2160, 2015-06-05 08:38:34

Why doesn't the POST example from the php.net manual work?

Hello. Please help the student to understand. I enter an example using POST

<?php
echo 'Привет ' . htmlspecialchars($_POST["name"]) . '!';
?>

My answer:
Notice: Undefined index: name in /User/site.local/php/test.php on line 2
Hi!

However, the official documentation says:
It is assumed that the user sent via POST name=Hannes
The result of this example will be something like this:
Hello Hannes!

What are Hannes and htmlspecialchars and why am I getting an error in the output?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Saprin, 2015-06-05
@0crash0

the first time you open there is no post[name] only when you send something so you need to check for the post
(not sure)
Hannes is the name that was sent to the script
htmlspecialcharacters: php.net/htmlspecialchars

K
Konstantin B., 2015-06-05
@Kostik_1993

Type your site address in the line /?name=Hannes
And replace the script with
<?php
echo 'Hello ' . htmlspecialchars($_REQUEST["name"]) . '!';
?>
and see what happens.
For the post method to work, you need to submit the form.

<form>
   <input type="text" name="name">
   <button>Отправить</button>
</form>
<?php
    echo 'Привет ' . htmlspecialchars($_REQUEST["name"]) . '!';
?>

A
Alexey Yakhnenko, 2015-06-05
@ayahnenko

It is written: It is understood that the user sent via POST name=Hannes
Did you send?
For a start, read how to send this same POST in general, and how it differs from GET, for example.

F
franticstas, 2015-06-05
@franticstas

Usually POST is used to send data from a form to an html page.
Add to file

<form  method="POST">
    <input type="text" name="name"><br>
    <input type="submit" value="submit" name="submitMe">
    </form>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question