How to create a PHP form that submits to itself?
Question:
How to create a self-posting/self-submitting form, i.e. one that submits the results to itself rather than to another form ?
Answer:
The correct approach is to use $_SERVER["PHP_SELF"] (in combination with htmlspecialchars to avoid possible exploits). You can also skip the action= part if it's empty, which is invalid in the W3C but currently works in most (all?) browsers - by default, if it's empty, commit to itself.
Here is an example form that gets a name and email and then displays the value you entered after submission:
<code class="php"><?php if (!empty($_POST)): ?> Welcome, <?php echo htmlspecialchars($_POST["name"]); ?>!<br> Your email is <?php echo htmlspecialchars($_POST["email"]); ?>.<br> <?php else: ?> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> Name: <input type="text" name="name"><br> Email: <input type="text" name="email"><br> <input type="submit"> </form> <?php endif; ?></code>
The above is the detailed content of 以下是几个可能的标题: * How to Create a Self-Submitting PHP Form? * How to Make a PHP Form Submit to Itself? * How to Create a PHP Form That Posts Back to Itself? * How to Create a PHP Form That Submits Data t. For more information, please follow other related articles on the PHP Chinese website!