如何制作一个提交给自己的 PHP 表单? [英] How do I make a PHP form that submits to self?

查看:24
本文介绍了如何制作一个提交给自己的 PHP 表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作一个自我发布/自我提交的表单,即一个将结果提交给自己的表单,而不是提交给另一个表单?

How do I make a self-posting/self-submitting form, i.e. a form that submits the results to itself, instead of submitting to another form?

推荐答案

正确的方法是使用 $_SERVER["PHP_SELF"] (结合 htmlspecialchars以避免可能的漏洞).您也可以跳过 action= 部分为空,这不是 W3C 有效的,但目前适用于大多数(所有?)浏览器 - 如果为空,默认是提交给 self.

The proper way would be to use $_SERVER["PHP_SELF"] (in conjunction with htmlspecialchars to avoid possible exploits). You can also just skip the action= part empty, which is not W3C valid, but currently works in most (all?) browsers - the default is to submit to self if it's empty.

这是一个示例表单,它采用姓名和电子邮件,然后显示您在提交时输入的值:

Here is an example form that takes a name and email, and then displays the values you have entered upon submit:

<?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; ?>

这篇关于如何制作一个提交给自己的 PHP 表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆