Sian 发表于 2016-2-21 15:17:35

18、jQuery中Ajax基本使用一

1、index.html
<!DOCTYPE html>
<html>
<head>
        <title>jQuery Ajax</title>
        <meta charset="utf-8"/>
    <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
    <style>
          *{
                  margin: 10px auto;
                  padding: 0;
                  text-align: center;
                  font-size: 18px;
          }
          
          #submit{
                  display: block;
                  width: 60px;
                  margin: 20px auto;
          }
    </style>
</head>
<body>
        <form action="submit.php" method="get">
                <span>品牌:</span>
                <input id="apple" type="checkbox" name="brand[]" value="apple" />Apple
                <input id="samsung" type="checkbox" name="brand[]" value="samsung" />Samsung
                <input id="google" type="checkbox" name="brand[]" value="google" />Google
                <input id="submit" type="submit" value="提交" />
        </form>
</body>
</html>2、submit.php<?php
        header("Content-Type:text/html;charset=utf-8");
        $appleArray = array(
                array(
                        'title'=>'iPhone 4S',
                        'brand'=>'苹果',
                        'price'=>'1488',
                        'location'=>'中国大陆'),
                array(
                        'title'=>'iPhone 5',
                        'brand'=>'苹果',
                        'price'=>'2488',
                        'location'=>'中国大陆')
        );
        $samsungArray = array(
                array(
                        'title'=>'Galaxy S3',
                        'brand'=>'三星',
                        'price'=>'1488',
                        'location'=>'韩国'),
                array(
                        'title'=>'Galaxy Note II',
                        'brand'=>'三星',
                        'price'=>'3488',
                        'location'=>'韩国')
        );
        $googleArray = array(
                array(
                        'title'=>'Nexus',
                        'brand'=>'谷歌',
                        'price'=>'2500',
                        'location'=>'韩国')
        );
       
        // 获取用户选项,如果没有选项提示重新选择
        $brandChecked = isset($_GET['brand'])?$_GET['brand']:exit('请选择选项');
        // 遍历数组拼接集合
        foreach($brandChecked as $item){
                $realBrand[] = ${$item.'Array'};
        }
        // 根据用户选项合并相应数据
        $newArray = call_user_func_array('array_merge', $realBrand);
        echo json_encode($newArray);
       
       
页: [1]
查看完整版本: 18、jQuery中Ajax基本使用一