后台 -- ajax传输数组 tp后台接收并处理数据

后台用 input("test/a") 来打散数组

前端

var test = ['1','2','3'];
$.ajax({
				type: 'POST',
				url: "{:url('index/Appointment/add')}",
				data: {
					test:test
				},
				dataType: 'json',
				success: function(data) {
					if (data == 0) {
						mui.toast("成功!!");
						window.setTimeout("window.location='{:url('index/index/time')}'",1000); 
					}else{
						mui.toast("失败!!");
					}
				},
				error: function() {
					console.log("error");
				}
			});

tp控制器

 $test = input("test/a");//将数据用/a散

tp控制器函数写法

<?php

namespace app\index\controller;

use app\common\controller\IndexBase;
use think\Db;


class Appointment extends IndexBase
{   
   public function add(){
	  
	   $test = input("test/a");//
	  
	   return json($test);
   }
}

假如我们的数组为两个,他们是对应的,比如 数组A下标0  对应着数组B下标0的信息

我们将这样的数据传入数据库

//这里是我自己假设的数据
$name = ['我是小名编号01','我是小虎编号02','我是小红编号03'];
$identity = ['01','02','03'];
//通过下面的循环编号和名字是一一对应的。

for ($i=0; $i < count($name) ; $i++) { 
		   $data['name'] = $name[$i];
		   $data['identity'] = $identity[$i];
		   $add = Db::name('order')->insert($data);
}
发布了105 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44944193/article/details/105221093
今日推荐