Home > Backend Development > PHP Tutorial > How to submit a form to itself in PHP

How to submit a form to itself in PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:13:55
Original
1402 people have browsed it

Using PHP for web development, the use of forms can be seen everywhere. Using its attributes Action and Method, you can specify the URL to which the content of the form is to be sent for processing and how to send the form data to the server. In most cases, we specify another URL address to process the form content to the Action attribute, but in some cases, we need to submit the form data to ourselves. How should we specify the Action attribute value at this time?

How to submit a form to itself in PHPphp
How to submit a form to itself in PHP
if(isset($_POST['action'])&&$_POST['action']=='submitted'){
How to submit a form to itself in PHP
print'

</span><span>'</span><span>;<br><img  src="/static/imghw/default1.png" data-src="http://image.codes51.com/Article/image/20151009/20151009093544_9981.gif" class="lazy" alt="How to submit a form to itself in PHP" ><br><img  src="/static/imghw/default1.png" data-src="http://image.codes51.com/Article/image/20151009/20151009093545_0762.gif" class="lazy" alt="How to submit a form to itself in PHP" ></span><span>print_r</span><span>(</span><span>$_POST</span><span>);<br><img  src="/static/imghw/default1.png" data-src="http://image.codes51.com/Article/image/20151009/20151009093545_1387.gif" class="lazy" alt="How to submit a form to itself in PHP" ></span><span>print</span><span>'</span><span><ahref="</span><span >'</span><span >.</span><span >$_SERVER</span><span >[</span><span >'</span><span >PHP_SELF</span><span >'</span><span >]</span><span >.</span><span >'</span><span >">Pleasetryagain</a></span><span>'</span><span>;<br><img  src="/static/imghw/default1.png" data-src="http://image.codes51.com/Article/image/20151009/20151009093545_2012.gif" class="lazy" alt="How to submit a form to itself in PHP" ><br><img  src="/static/imghw/default1.png" data-src="http://image.codes51.com/Article/image/20151009/20151009093545_2793.gif" class="lazy" alt="How to submit a form to itself in PHP" ></span><span>print</span><span>'</span><span>
';
How to submit a form to itself in PHP}
else{
How to submit a form to itself in PHP
?>
How to submit a form to itself in PHP
<formaction=""method="POST">
How to submit a form to itself in PHPName
:<inputtype="text"name="personal[name]"><br>
How to submit a form to itself in PHPEmail
:<inputtype="text"name="personal[email]"><br>
How to submit a form to itself in PHPBeer
:<br>
How to submit a form to itself in PHP
<selectmultiplename="beer[]">
How to submit a form to itself in PHP
<optionvalue="warthog">Warthog
How to submit a form to itself in PHP
<optionvalue="guinness">Guinness
How to submit a form to itself in PHP
select><br>
How to submit a form to itself in PHP
<inputtype="hidden"name="action"value="submitted">
How to submit a form to itself in PHP
<inputtype="submit"name="submit"value="submitme!">
How to submit a form to itself in PHP
form>
How to submit a form to itself in PHP
php
How to submit a form to itself in PHP}
How to submit a form to itself in PHP
?>

上面的代码就实现了将表单提交给自己(注:在PHP4.1.0之前的版本请使用$_HTTP_POST_VARS替代下面代码中的$_Post变量)。这里使用了服务器变量$_Server获取当前页面的URL地址,并将其赋给表单的Action属性。这里使用了一个小小的技巧用以服务器在收到该URL请求是确定是POST请求还是GET请求。就是在表单中增加一个隐藏的变量,在处理请求时我们通过isset函数检测是否设置了这个隐藏变量,由此判断出该请求使用了POST还是GET方法。

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了PHP如何将表单提交给自己,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
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 Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template