[php] Basic syntax and value transfer between pages

WBOY
Release: 2016-08-08 09:32:39
Original
920 people have browsed it

Let me explain it to you with an example~

1. Basic goal

Set a form in phpsend.html so that the values ​​entered above can be received by phpget.php


2. Production process

Just like JavaScript, the values ​​in PHP are not classified into types. A $ represents the value.

If you want to use this variable in the future, you must bring this $ to indicate that it is a variable.

for, switch case in each language , while, if else and other structures still exist

!=, = and other operators also exist

In the output language echo, use ${XX variable} to force the value of this variable to be output.

(1) phpsend.html

This is a static page that does not require any server language processing. As long as the action points to the processed phpget.php,

but please pay attention to the method of transmitting the value of the multi-select box.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpsend</title>
</head>
<body>
<form action="phpget.php" method="post">
<!--<fieldset>就是表单外面的那个方框,双标识标签,有</fieldset>,在结尾-->
<fieldset>
<!--这就是表单中的那个标题-->
<legend>phpsend</legend>
<!--以下4个控件与最后的多行文本框没什么好说的,注意使用name来传值,而不是id,注意下拉菜单的name属性必须在大标签select中-->
用户名:<input type="text" name="username"/><br /><br />
密码:<input type="password" name="password" /><br /><br />
学历:<select name="education">
<option value="0">小学生</option>
<option value="1">中学生</option>
<option value="2">大学生</option>
<option value="3">其他</option>
</select><br /><br />
性别:<input type="radio" name="sex" value="男" />男<input type="radio" name="sex" value="女" />女<br /><br />
<!--复选框的传值,必须传递一个数组给处理的php页面-->
爱好:<input type="checkbox" name="favor[]" value="编程" />编程<input type="checkbox" name="favor[]" value="睡觉" />睡觉<input type="checkbox" name="favor[]" value="其他" />其他<br /><br />
备注:<textarea name="ps" rows="3" wrap="virtual"></textarea><br /><br />
<input type="submit" value="提交" />
</fieldset>
</form>
</body>
</html>
Copy after login
(2)phpget.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpget</title>
</head>

<body>
<?php
//php的注释同样是双斜杠
//用$_REQUEST["表单中的name属性"]能获取表单中相应的组件中的值
$username=$_REQUEST["username"];
//${变量名}就能够输出变量的值了
echo "用户名:${username}";
echo "<br>";
echo "<br>";

$password=$_REQUEST["password"];
echo "密码:${password}";
echo "<br>";
echo "<br>";

//由于form中name=education的下拉菜单传递过来的是0,1,2,3其中之一,
//需要用switch结构处理一下
$education=$_REQUEST["education"];
switch($education){
	case 0:
		echo "学历:小学生";
		break;
	case 1:
		echo "学历:中学生";
		break;
	case 2:
		echo "学历:大学生";
		break;
	case 3:
		echo "学历:其他";
		break;						
	}
echo "<br>";
echo "<br>";

$sex=$_REQUEST["sex"];
echo "性别:${sex}";
echo "<br>";
echo "<br>";

//对于复选框的处理如下
echo "爱好:";
//此处favor是一个数组,这个数组的第一个元素就是复选框的第一个被选的元素,然后,第二个元素就是复选框的第二个被选的元素,以此类推
$favor=$_REQUEST["favor"];
//count($favor)相当于jsp里面的favor.length(),asp里面的ubound(favor),求数组长度
for($i=0;$i<count($favor);$i++){
	echo "$favor[$i]";
	//如果没遍历到最后就在输出元素之后再输出一个逗号,反之输出一个句号
	if($i!=count($favor)-1)
		echo ",";
	else
		echo "。";
	}
echo "<br>";
echo "<br>";

//以下把favor数组一次性输出的另一种方法
echo "爱好:";
foreach($favor as $a)
	echo "${a},";
echo "<br>";
echo "<br>";

$ps=$_REQUEST["ps"];
echo "备注:${ps}";
echo "<br>";
echo "<br>";
?>
</body>
</html>
Copy after login

The above introduces the basic syntax of [php] and the value transfer between pages, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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