Php uses forms to implement some simple functions.

Source: Internet
Author: User

Example 1 (POST submission form ): Copy codeThe Code is as follows: <Head>
<Title>
Chunkify Form
</Title>
</Head>
<Body>
<Form action = "chunkify. php" method = "POST">
Enter a word:
<Input type = "text" name = "word"/> <br/>
How long shocould be the chunks be?
<Input type = "text" name = "number"/> <br/>
<Input type = "submit" value = "Chunkify">
</Form>
</Body>
</Html> Copy codeThe Code is as follows: <Head>
<Title>
Chunkify Word
</Title>
</Head>
<? Php
$ Word = $ _ POST ['word'];
$ Number = $ _ POST ['number'];
$ Chunks = ceil (strlen ($ word)/$ number );
Echo "The $ number-letter chunks of '$ word' are: <br/> \ n ";
For ($ I = 0; $ I <$ chunks; $ I ++ ){
$ Chunk = substr ($ word, $ I * $ number, $ number );
Printf ("% d: % s <br/> \ n", $ I + 1, $ chunk );
}
?>
</Body>
</Html>

Html page.

Page processed by php after the form is submitted. In this example, I enter a word, and then give it a length, and divide the word into blocks of that length.

The POST method is used to submit a form.
Example 2 (single choice, GET accept Form ):Copy codeThe Code is as follows: <form action = "<? Php echo $ _ SERVER ['php _ SELF ']?> "Method =" GET ">
Select your personality attributes: <br/>
<Select name = "att []">
<Option value = "perky"> perky </option>
<Option value = "morese"> morose </option>
<Option value = "thinking"> thinking </option>
<Option value = "feeling"> feeling </option>
<Option value = "thrity"> speed-thrift </option>
<Option value = "prodigal"> shopper </option>
</Select>
<Br>
<Input type = "submit" name = "s" value = "Record my personality">
</Form>

<? Php
If (array_key_exists ('s ', $ _ GET )){
$ Des = implode ('', $ _ GET ['att ']);
Echo "You have a $ des personality .";
}
?>


Example 3 (multiple options, GET accept Form ):

Note that the <select name = "att []" multiple> underline tells GET that you are transmitting an array, and the black part indicates that the selection box is changed to multiple selection boxes.Copy codeThe Code is as follows: <form action = "<? Php echo $ _ SERVER ['php _ SELF ']?> "Method =" GET ">
Select your personality attributes: <br/>
<Select name = "att []" multiple>
<Option value = "perky"> perky </option>
<Option value = "morese"> morose </option>
<Option value = "thinking"> thinking </option>
<Option value = "feeling"> feeling </option>
<Option value = "thrity"> speed-thrift </option>
<Option value = "prodigal"> shopper </option>
</Select>
<Br>
<Input type = "submit" name = "s" value = "Record my personality">
</Form>

<? Php
If (array_key_exists ('s ', $ _ GET )){
$ Des = implode ('', $ _ GET ['att ']);
Echo "You have a $ des personality .";
}
?>


Example 4 (check box checkbox): the same name = "att []" indicates that GET transmits an array, and checked indicates that this option is the initial default option. In the same example, you can also add selected = "selected" to the tag.

Select multiple options by default.Copy codeThe Code is as follows: <form action = "<? Php echo $ _ SERVER ['php _ SELF ']?> "Method =" GET ">
Select your personality attributes: <br/>
Perky <input type = "checkbox" name = "att []" value = "perky" checked/> <br/>
Morose <input type = "checkbox" name = "att []" value = "morose" checked/> <br/>
Thinking <input type = "checkbox" name = "att []" value = "thinking"/> <br/>
Feeling <input type = "checkbox" name = "att []" value = "feeling"/> <br/>
<Br>
<Input type = "submit" name = "s" value = "Record my personality">
</Form>

<? Php
If (array_key_exists ('s ', $ _ GET )){
Echo "<pre> ";
Print_r ($ _ GET );
Echo "</pre> ";
If (is_null ($ _ GET ['att ']) exit;

$ Des = implode ('', $ _ GET ['att ']);
Echo "You have a $ des personality .";
}
?>


Example 5 (single answer): Note that the same option must have the same nameCopy codeThe Code is as follows: <form>
Male:
<Input type = "radio" checked = "checked" name = "Sex" value = "male"/>
<Br/>
Female:
<Input type = "radio" name = "Sex" value = "female"/>
<Br>
<Hr>
Male:
<Input type = "radio" checked = "checked" name = "Se" value = "male"/>
<Br/>
Female:
<Input type = "radio" name = "Se" value = "female"/>
</Form>
<P> when a user clicks a single-choice button, the button changes to the selected status, and all other buttons change to the unselected status. </P>


Example 6 (stick form)Copy codeThe Code is as follows: <? Php
$ F = $ _ POST ['fa '];

?>

<Form action = "<? Php echo $ _ SERVER ['php _ SELF '];?> "Method =" POST ">
Temperature:
<Input type = "text" name = "fa" value = "<? Php echo $ f;?> "/>;
<Br/>
<Input type = "submit" name = "Convert to Celsius"/>
</Form>
<? Php
If (! Is_null ($ f )){
$ C = ($ f-32) * 5/9;
Printf ("%. 2lf is %. 2lfC", $ f, $ c );
}
?>

Are some simple form processing ~

Knowledge make me stronger!

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.