Home > Backend Development > PHP Tutorial > 以下是几个可能的标题: * 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

以下是几个可能的标题: * 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

Linda Hamilton
Release: 2024-10-29 06:51:02
Original
1041 people have browsed it

以下是几个可能的标题:

* 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 to the Same Page?

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template