1

i've recently acquired a copy of Programming PHP, 3rd edtion, and on page 10 i saw an "echo" that brought me doubt.

<html>
<head>
    <title>Personalized Greeting Form</title>
</head>
<body>
    <?php if (!empty($_POST['name'])) {
        echo "Greetings, {$_POST['name']}, and welcome.";
    } ?>

    <form action="<? echo php $_SERVER['PHP_SELF']; ?>" method="post">
        Enter your name: <input type="text" name="name" />
        <input type="submit" />
    </form>
</body>

is the "echo" on the form tag necesssary? i've tried without it and it seems to work perfectly but i'm not sure if it's really important to keep that "echo" there...

if someone knows this, i'd appreciate!

thanks guys!

vcamargo
  • 440
  • 1
  • 3
  • 11
  • This is invalid => ` echo php $_SERVER['PHP_SELF']; ?>` change it to ` echo $_SERVER['PHP_SELF']; ?>` --- better yet `` or even `action=""` does the same job. Depends which flavour you like best. – Funk Forty Niner Nov 06 '13 at 03:34
  • 1
    food for thought, quite a few things 'work', that are not a good idea :-) –  Nov 06 '13 at 03:41
  • @j08691 - he is not asking why use $_SERVER['PHP_SELF'], he is asking if echo is necessary or not. – Alpesh Trivedi Nov 06 '13 at 06:13
  • If you can run that code, you haven't configured PHP to display error messages. That's something you need to fix before you go further; it's impossible to code properly without the aid of error messages. Here's a [brief explanation](http://stackoverflow.com/a/5680885/13508). – Álvaro González Nov 06 '13 at 13:05

5 Answers5

4

It depends on HTML version that you use.

Form action defaults to current URL. According to standards:

  • in HTML 4 it's required and must be non-empty, so you need to use echo or action="#" in order for the form to be valid
  • in HTML 5, it's not required, but must be non-empty if present, so in that case you shouldn't define actin at all and let the defaults work
Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
2

if the respond script is in the same file, then it is not neccesary to declare anything in action="" parameter. So, it is not.

Nik Terentyev
  • 2,270
  • 3
  • 16
  • 23
2

Yes & no. Without the echo in the form part, the action would be as follows:

<form action="" method="post">

That works in HTML4 & below. But that is not correct in HTML5 standards & won’t validate. And depending on what browsers are used to access the page, might choke it. If you have an action= then it has to have something. In that case you might get away with adding a # in there as follows:

<form action="#" method="post">

But that seems hacky & might not be future-proof.

To avoid headaches between HTML4 & HTML5 best just let it place the full URL in there via that echo.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

Its not neccessary that you need to echo in action

The best thing you can do is leave out the action attribute altogether. If you leave it out, the form will be submitted to the document's address, i.e. the same page.

Ajith S
  • 2,907
  • 1
  • 18
  • 30
0

Thanks to everyone who answered my question, it helped me a lot to understand this issue!

Now I'd like to invite you all to answer another one :)

It's more like an opinion than a real question: https://stackoverflow.com/questions/20081548/how-would-you-use-a-book-such-as-programming-php-to-truly-learn-php-from-the-g

Community
  • 1
  • 1
vcamargo
  • 440
  • 1
  • 3
  • 11