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

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

问题描述

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

解决方案正确的方法是使用 $ _ SERVER [PHP_SELF] (连同 htmlspecialchars 以避免可能的漏洞利用)。你也可以跳过 action = 部分empty,这不是W3C有效的,但目前在大多数(所有?)浏览器中都可以使用 - 默认情况下,如果它是空。



以下是一个表单,它包含名称和电子邮件,然后显示您在提交时输入的值:

 <?php if(!empty($ _ POST)):?> 
欢迎,<?php echo htmlspecialchars($ _ POST [name]); ?>!<峰; br>
您的电子邮件地址是<?php echo htmlspecialchars($ _ POST [email]); ?>。<峰; br>
<?php else:?>
< form action =<?php echo htmlspecialchars($ _ SERVER [PHP_SELF]); ?>方法= POST >
名称:< input type =textname =name>< br>
电子邮件地址:< input type =textname =email>< br>
< input type =submit>
< / form>
<?php endif; ?>


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?

解决方案

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天全站免登陆