Home  >  Article  >  Backend Development  >  Detailed explanation two: Example of interaction between PHP and Web pages

Detailed explanation two: Example of interaction between PHP and Web pages

coldplay.xixi
coldplay.xixiforward
2020-08-07 16:30:172677browse

Detailed explanation two: Example of interaction between PHP and Web pages

Foreword

The form is explained in the notes of "PHP Study Notes-Interaction between PHP and Web Pages 1" Some properties of the form, including how to write its input field mark, selection field mark, and text field mark. The following content is about how to obtain form data and transfer PHP data, including obtaining various control values.

Related learning recommendations: php programming (video)

Insert form

There must be a form before submitting the form. After our form is created, the form can be inserted into the Web page. The code is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>在普通的Web页中插入表单</title>
<style type="text/css">
body,td,th {
  font-size: 12px;
}
</style>
</head>
<body>
<form action="demo_1.php" method="post" name="form1" enctype="multipart/form-data">
 <table width="405" height="24" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#999999">
  <tr bgcolor="#FFCC33">
   <td width="103" height="25" align="right">商品名称:</td>
   <td height="25" align="left"><input name="product" type="text" id="user" size="20" maxlength="100"></td>
  </tr>
  <tr bgcolor="#FFCC33">
   <td height="25" align="right">市场:</td>
   <td height="25" colspan="2" align="left"><input name="from" type="radio" value="海外" checked>
    海外
    <input type="radio" name="from" value="国内">
    国内</td>
  </tr>
  <tr bgcolor="#FFCC33">
   <td width="103" height="25" align="right">编号:</td>
   <td width="289" height="25" colspan="2" align="left"><input name="code" type="text" id="code" size="20" maxlength="100"></td>
  </tr>
  <tr bgcolor="#FFCC33">
   <td height="25" align="right">种类:</td>
   <td height="25" colspan="2" align="left"><select name="select">
     <option value="电器">电器</option>
     <option value="家具">家具</option>
     <option value="化妆品">化妆品</option>
     <option value="图书" selected>图书</option>
     <option value="服饰">服饰</option>
     <option value="宠物">宠物</option>
     <option value="计算机">计算机</option>
    </select></td>
  </tr>
  <tr bgcolor="#FFCC33">
   <td height="25" align="right">商品图片: </td>
   <td height="25" colspan="2" align="left"><input name="photo" type="file" size="20" maxlength="1000" id="photo"></td>
  </tr>
  <tr bgcolor="#FFCC33">
   <td height="25" align="right">商品描述: </td>
   <td height="25" colspan="2" align="left"><textarea name="intro" cols="28" rows="3" id="info"></textarea></td>
  </tr>
  <tr align="center" bgcolor="#FFCC33">
   <td height="25" colspan="3"><input type="submit" name="submit" value="提交">
      
    <input type="reset" name="submit2" value="重置"></td>
  </tr>
 </table>
</form>
<?php
header("Content-Type:text/html;  charset=gb2312");
?>>
</body>
</html>

In the

and Add a form between body>.

Run results:

Detailed explanation two: Example of interaction between PHP and Web pages

Get form data

The main way to get form data There are two clock methods: POST() method and GET() method.

Specified by the method attribute of the

form.

Use the POST method to submit the form

When applying the POST method, you only need to set the attribute method in the

form to POST. Can. The POST method does not rely on a URL and will not be displayed in the address bar. The POST method can transfer data to the server without restrictions. All submitted information is transmitted in the background. Users cannot see this process on the browser side, so security is high. Therefore, the POST method is more suitable for sending a confidential (such as a credit card number) or large-capacity data to the server.

Example:

<!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=gb2312"
    />
  <title>PHP语言基础</title>

</head>
<body>
<form action="index.php" method="post" name="form1" enctype="multipart/form-data">
  <table width="300" border="1" cellpadding="10" cellspacing="0">
    <tr>
      <td height="30">编号:
        <input type="text" name="code" size="20"/>
        <input type="submit" name="subimt" value="提交"/>
      </td>
    </tr>
  </table>
</form>
<?php
header("Content-Type:text/html;  charset=gb2312");
?>
</body>
</html>

Running results:

Detailed explanation two: Example of interaction between PHP and Web pages

After clicking the submit button, we found that the address bar did not display the parameters we submitted.

Use the GET method to submit the form

The GET method is the default method for the method attribute in the

form. Form data submitted using the GET method is appended to the URL and sent to the server as part of the URL. During the development process of the program, since the data submitted by the GET method is attached to the URL and sent, "URL user-passed parameters" will be displayed in the address bar of the URL.

Specify the method attribute in the

form in the above example as get. The result after running the program is as follows:

Detailed explanation two: Example of interaction between PHP and Web pages

The address bar after clicking the button Will it pass'? 'Connect key-value pairs, separated by '&'.

Common methods of passing PHP parameters

Getting form data is actually getting the data of different form elements. The name in the

tag is an attribute that all form elements have, which is the name of this form element. When using it, you need to use the name attribute to get the corresponding value attribute.

There are three common ways to pass parameters in PHP:

  1. $_POST[]Global variable
  2. $_GET[]Global variable
  3. $_SESSION []Variable

$_POST[]Global variable

Use PHP's $_POST[] predefined variable to obtain form elements The value, the format is:

$_POST[name]

Example:

<!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=gb2312"
    />
  <title>PHP语言基础</title>

</head>
<body>
<form action="index.php" method="post" name="form1" enctype="multipart/form-data">
  <table width="300" border="1" cellpadding="10" cellspacing="0">
    <tr>
      <td height="30">编号:
        <input type="text" name="code" size="20"/>
        <input type="submit" name="subimt" value="提交"/>
      </td>
    </tr>
  </table>
</form>
<?php
header("Content-Type:text/html;  charset=gb2312");
$value=$_POST[&#39;code&#39;];
echo "编号:".$value;
?>
</body>
</html>

Running result:

Detailed explanation two: Example of interaction between PHP and Web pages

$ _GET[] global variable

PHP uses the $_GET[] predefined variable to obtain the value passed through the GET method. The usage format is:

   $_GET[name]

Example:

<!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=gb2312"
    />
  <title>PHP语言基础</title>

</head>
<body>
<form action="index.php" method="get" name="form1" enctype="multipart/form-data">
  <table width="300" border="1" cellpadding="10" cellspacing="0">
    <tr>
      <td height="30">编号:
        <input type="text" name="code" size="20"/>
        <input type="submit" name="subimt" value="提交"/>
      </td>
    </tr>
  </table>
</form>
<?php
header("Content-Type:text/html;  charset=gb2312");
$value=$_GET[&#39;code&#39;];
echo "编号:".$value;
?>
</body>
</html>

Run result:

Detailed explanation two: Example of interaction between PHP and Web pages

##$_SESSION[] variable

Use The $_SESSION[] variable can obtain the value of the form element. The format is:

$_SESSION[name]

The variable value obtained using the $_SESSION[] parameter passing method can be used on any page after saving. However, this method consumes system resources and readers are advised to use it with caution.

Case

Finally write a complete case based on the first demo in the notes:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <title>在普通的Web页中插入表单</title>
  <style type="text/css">
    body, td, th {
      font-size: 12px;
    }
  </style>
</head>
<body>
<form action="demo_1.php" method="post" name="form1" enctype="multipart/form-data">
  <table width="405" height="24" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#999999">
    <tr bgcolor="#FFCC33">
      <td width="103" height="25" align="right">商品名称:</td>
      <td height="25" align="left"><input name="product" type="text" id="user" size="20" maxlength="100"></td>
    </tr>
    <tr bgcolor="#FFCC33">
      <td height="25" align="right">市场:</td>
      <td height="25" colspan="2" align="left"><input name="from" type="radio" value="海外" checked>
        海外
        <input type="radio" name="from" value="国内">
        国内
      </td>
    </tr>
    <tr bgcolor="#FFCC33">
      <td width="103" height="25" align="right">编号:</td>
      <td width="289" height="25" colspan="2" align="left"><input name="code" type="text" id="code" size="20"
                                    maxlength="100"></td>
    </tr>
    <tr bgcolor="#FFCC33">
      <td height="25" align="right">种类:</td>
      <td height="25" colspan="2" align="left"><select name="select">
          <option value="电器">电器</option>
          <option value="家具">家具</option>
          <option value="化妆品">化妆品</option>
          <option value="图书" selected>图书</option>
          <option value="服饰">服饰</option>
          <option value="宠物">宠物</option>
          <option value="计算机">计算机</option>
        </select></td>
    </tr>
    <tr bgcolor="#FFCC33">
      <td height="25" align="right">商品图片:</td>
      <td height="25" colspan="2" align="left"><input name="photo" type="file" size="20" maxlength="1000"
                              id="photo"></td>
    </tr>
    <tr bgcolor="#FFCC33">
      <td height="25" align="right">商品描述:</td>
      <td height="25" colspan="2" align="left"><textarea name="intro" cols="28" rows="3" id="info"></textarea>
      </td>
    </tr>
    <tr align="center" bgcolor="#FFCC33">
      <td height="25" colspan="3"><input type="submit" name="submit" value="提交">
          
        <input type="reset" name="submit2" value="重置"></td>
    </tr>
  </table>
</form>
<?php
header("Content-Type:text/html;  charset=gb2312");

if ($_POST[submit] != "") {
  echo "商品清单:";
  echo " <br><br>商品名称:" . $_POST[&#39;product&#39;];
  echo " <br><br>  市场:" . $_POST[from];
  echo " <br><br>  编号:" . $_POST[&#39;code&#39;];
  echo " <br><br>  种类:" .$_POST[&#39;select&#39;];
  $path = &#39;./upfiles/&#39;. $_FILES[&#39;photo&#39;][&#39;name&#39;];
  move_uploaded_file($_FILES[&#39;photo&#39;][&#39;tmp_name&#39;],$path);
  echo " <br><br>商品图片:" .$path;
  echo " <br><br>商品描述:" .$_POST[&#39;intro&#39;];
}


?>
</body>
</html>

Running result:

Detailed explanation two: Example of interaction between PHP and Web pages

Upload the image to the upfiles folder under the current path through the move_uploaded_file method.

Related learning recommendations: Programming videos

The above is the detailed content of Detailed explanation two: Example of interaction between PHP and Web pages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete