How PHP submits a form to itself

Source: Internet
Author: User

PHP is used for web development. The use of forms is everywhere and its attributes are used.ActionAndMethodYou can specify the URL of the form content to be sent and how to send the form data to the server. In most cases, we specify another URL for processing the form contentActionAttribute, but in some cases, you need to submit the form data to yourself. How should we specifyActionWhat about the attribute value?

<? PHP
If (isset ($ _ post ['action']) & $ _ post ['action'] = 'submitted '){
Print '<PRE> ';

Print_r ($ _ post );
Print '<a href = "'. $ _ server ['php _ Self '].'"> Please try again </a> ';

Print '</PRE> ';
} Else {
?>
<Form action = "<? PHP echo $ _ server ['php _ Self '];?> "Method =" Post ">
Name: <input type = "text" name = "personal [name]"> <br>
Email: <input type = "text" name = "personal [email]"> <br>
Beer: <br>
<Select multiple name = "beer []">
<Option value = "warthog"> warthog
<Option value = "Guinness"> Guinness
</SELECT> <br>
<Input type = "hidden" name = "action" value = "submitted">
<Input type = "Submit" name = "Submit" value = "submit me! ">
</Form>
<? PHP
}
?>

The code above implements submitting the form to yourself (Note: For versions earlier than php4.1.0, use$ _ Http_post_varsReplace$ _ PostVariable). Here, the server variable $ _ server is used to obtain the URL address of the current page and assign it toActionAttribute.A small trick is used here to determine whether a POST request or GET request is received by the server. Add a Hidden variable to the form.IssetThe function checks whether the Hidden variable is set, and determines whether the request uses the post or get method..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.