Home Backend Development PHP Tutorial Examples of php combining forms to implement some simple functions_PHP tutorial

Examples of php combining forms to implement some simple functions_PHP tutorial

Jul 21, 2016 pm 03:29 PM
html php post one code example Function copy accomplish submit of Simple combine form

例子一(POST提交表单):

复制代码 代码如下:

<html>
<head>
<title>
Chunkify Form
</title>
</head>
<body>
<form action="chunkify.php" method="POST">
Enter a word:
<input type="text" name="word"/><br/>
How long should be the chunks be?
<input type="text" name="number"/><br />
<input type="submit" value="Chunkify">
</form>
</body>
</html>


复制代码 代码如下:

<html>
<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>

Examples of php combining forms to implement some simple functions_PHP tutorial

html显示出来的页面。

Examples of php combining forms to implement some simple functions_PHP tutorial

提交表单后php处理出来的页面。在这个例子中,我输入一个单词,然后给定一个长度,将单词等分成该长度的块。

演示了通过POST方法提交表单。
例子二(单选,GET接受表单):

复制代码 代码如下:

<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="thrifty">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.";
}
?>

Examples of php combining forms to implement some simple functions_PHP tutorial
例子三(多选,GET接受表单):

注意到此时<select name="att[]" multiple> 下划线告诉GET你传输的是个数组,黑体字部分则是表示改选择框为多选框
复制代码 代码如下:

<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="thrifty">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.";
}
?>

Examples of php combining forms to implement some simple functions_PHP tutorial
Example 4 (checkbox): Similarly name="att[]" tells GET that what you transmit is an array, checked means that the option is the initial default Selection, the same as the above example, adding selected="selected" in the tag can also

make the initial default selection for multiple selections.
Copy code The 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" /> &lt ;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.";
}
?>

Examples of php combining forms to implement some simple functions_PHP tutorial
Example 5 (radio button): Note that the same option can be used The selected names must be equal
Copy code The 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 the user clicks a radio button, the button becomes selected and all other buttons become unselected. </p>

Examples of php combining forms to implement some simple functions_PHP tutorial
Example 6 (stick form): How can a form realize that the previously entered values ​​still exist after the page is refreshed? As follows
Copy the code The 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);
}
?>

Examples of php combining forms to implement some simple functions_PHP tutorial

Examples of php combining forms to implement some simple functions_PHP tutorial

It’s all simple form processing~

Knowledge make me stronger!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323405.htmlTechArticleExample 1 (POST submission form): Copy the code as follows: html head title Chunkify Form /title /head body form action="chunkify.php" method="POST" Enter a word: input type="text...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles