Blogger Information
Blog 10
fans 0
comment 0
visits 5116
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php中foreach的用法及表单提交中GET方法和POST方法 2019年7月22日 第七期第二部分PHP学习第一天
Johnson的博客
Original
620 people have browsed it

今天是PHP第七期学习第二部分PHP学习的第一天,今天主要讲了PHP的基本原理,php和html的差别,如何进行php和html的混编,以下为php和html的混编的案例,还包含php中foreach的用法:代码如下:

实例

<?php
$title = '成绩公布表';
$grade = ['语文:80','数学:95','英语:66','物理:88','化学:50'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>foreach</title>
</head>
<body>
<div style="width: 200px; height: 300px; border: 1px solid black">
<h2 style="margin: 10px auto; text-align: center">成绩公布表</h2>
<hr>
    <ul>
        <li>语文:80</li>
        <li>数学:95</li>
        <li>英语:66</li>
        <li>物理:88</li>
        <li>化学:50</li>
    </ul>
</div>
<br>

<div style="width: 200px; height: 300px; border: 1px solid black">
    <h2 style="margin: 10px auto; text-align: center"><?php echo $title ?></h2>
    <hr>
    <ul>
        <?php
        foreach ($grade as $value) {
            echo '<li>' . $value . '</li>'; //  JS里面使用 "+"来连接两个变量,而php 里面用"."  来连接两个变量
        }
        ?>
    </ul>
</div>

<br>

<div style="width: 200px; height: 300px; border: 1px solid black">
    <h2 style="margin: 10px auto; text-align: center"><?php echo $title ?></h2>
    <hr>
    <ul>
        <?php
        foreach ($grade as $key=>$value) {
            echo '<li>' . ($key + 1) . ': ' . $value . '</li>';

        }
        ?>
    </ul>
</div>
<script>
    // array1.forEach(function(element) {
    //     console.log(element);
    // // });


    // var grade = ['语文:80','数学:95','英语:66','物理:88','化学:50'];
    // grade.forEach(function (element) {
    //     console.log (element);
    // })
</script>

</body>
</html>

<?php
foreach ($grade as $value) {
    echo $value . '<br>' ;
}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

由此案例,我们可以总结出以下几个知识点:

   1. php中代码的输出与html中代码输出一样,需要一个标签进行包装<?php    ......   ?>

   2. php中变量不需要进行声明,直接赋值即可,方法为   $  +   字母/_(字母分大小写,不能直接跟数字)

   3. php中许多方法与JS中类似,如foreach

  1. JS中 foreach的用法为:

         array1.forEach(function(element) {
                console.log(element);
           });

    php中forcase的用法为:

        <?php
                foreach ($grade as $value) {
                   echo $value . '<br>' ;                              //   增加一个br标签将遍历出的数组分行排列
                 }
        ?>

    4.    JS里面使用 "+"来连接两个变量,而php 里面用"."来连接两个变量


本节课还讲了表单数据提交的方法,get方法和post方法,   php获取表单数据使用    $_GET和   $_POST  ,这两个变量为超全局变量,不许赋值,可直接使用,此外还讲了一个增加用户体验的技巧:粘性表单

具体实例如下:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>get和post</title>
</head>
<body>
<div style="width: 300px; height: 300px ; border: 1px solid blue; margin: 10px ; padding: 10px" >
    <h2 style="text-align: center; line-height: 70px">用户登录</h2>

<form action="" method="get" style="width: 300px; height: 200px">

    <label for="username">用户名:</label><input type="text" name="username" id="username" placeholder="手机号/邮箱地址"  value="<?php echo isset($_GET['username']) ? $_GET['username'] : ''; ?>"    >
    <hr>
    <label for="password">密   码:</label><input type="password" name="password" id="password" placeholder="8-16位数字和字母" value="<?php echo isset($_GET['password']) ? $_GET['password'] : ''; ?>"  >
    <hr>
    <input value="登录" type="submit" style="width: 50px; height: 30px; position: relative; bottom:-40px" >

</form>
</div>
</body>
</html>

<?php

echo '<pre>';

print_r($_GET);

?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


其中:php代码中使用pre用于代码输出打印数组输出的时候,保持格式的统一性


Correction status:qualified

Teacher's comments:表单写得很完整, 这是亮点
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!