HTML POST提交参数给PHP并返回json,上传execl文件

需求:AJAX post带参数请求接口,PHP接收后存入数据库;然后返回json数据循环渲染到HTML

<!DOCTYPE html>
<html lang="zh">

	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
		<style type="text/css">
			button {
				width: 150px;
				height: 50px;
			}
			
			.file {
				margin: 50px;
			}
			
			.content {
				margin: 50px;
			}
			
			.btn_2 {
				bottom: 20px;
				left: 40px;
			}
		</style>
	</head>

	<body>
		<div class="file">
		<form name="myupload" id="myupload" enctype='multipart/form-data' action="http://gohosts.com/up_file.php" method="post">
			<input type="file" name="excel_file" id="excel_file">
			<input value="提交" type="submit" />
		</form>
		</div>
		<div class="content">
			标题:<input type="text" name="content" /><br> 
			<button class="btn_2" id="ajaxSubmit" />点我ajax提交表单</button>
			<div class="block" id="block"></div>
		</div>
		<script type="text/javascript">
			//给id为ajaxSubmit的按钮提交表单
			$("#ajaxSubmit").click(function() {
				var data = {
					content: $(" input[ name='content' ] ").val(),
				}
				console.log(data)
				$.ajax({
					//几个参数需要注意一下
					type: "POST", //方法类型
					dataType: "json", //预期服务器返回的数据类型
					url: "http://gohosts.com/index.php", //url
					data: data,
					success: function(res) {
						console.log(res); //打印服务端返回的数据(调试用)

						var htm = '';
						for(var i = 0; i < res.length; i++) {
								htm += '<div>content:' + res[i].content + '</div>';
						}
						$('#block').html(htm)
					},
					error: function() {
						alert("异常!");
					}
				});
			});
		</script>

	</body>

</html>

//index.php      PHP接受请求的参数并存入数据库,然后将数据库当前表里面的数据转换成JSON格式返回给前台

<?php
    header("Content-Type:text/html;charset=utf8"); 
	header("Access-Control-Allow-Origin: *"); //解决跨域
	header('Access-Control-Allow-Methods:POST');// 响应类型  
	header('Access-Control-Allow-Headers:*'); // 响应头设置 
    $link=mysql_connect("localhost","root","root"); 
    mysql_select_db("new_test", $link); //选择数据库
    mysql_query("SET NAMES utf8");//解决中文乱码问题
	
    $content = $_POST['content'];
	
	//插入数据到数据库 
	$strsql = "insert into arr_content (content) values('$content')";
	//mysql_query() 函数执行一条 MySQL 查询。SELECT,SHOW,EXPLAIN 或 DESCRIBE 都需要用这个函数执行
	$result = @mysql_query($strsql);
	
	
    $q = "SELECT * FROM arr_content"; //SQL查询语句 SELECT * FROM 表名
    $rs = mysql_query($q); //获取数据集
    if(!$rs){die("数据库没有数据!");}
	
	//循环读取数据并存入数组对象
    $dlogs;$i=0;
    while($row=mysql_fetch_array($rs))
    {
        $dlog["content"]=$row["content"];
        $dlogs[$i++]=$dlog;
    }
	
	
    //以json格式返回html页面
    echo urldecode(json_encode($dlogs));
?>

//up_file.php       实现上传excel文件并存入数据库  excel文件下载地址

<?php
    header("Content-Type:text/html;charset=utf8"); 
	header("Access-Control-Allow-Origin: *"); //解决跨域
	header('Access-Control-Allow-Methods:POST');// 响应类型  
    $link=mysql_connect("localhost","root","root"); 
    mysql_select_db("new_test", $link); //选择数据库
    mysql_query("SET NAMES utf8");//解决中文乱码问题
    
    require_once 'phpExcel/PHPExcel.php'; 
	
	 $filePath = $_FILES ['excel_file'] ['tmp_name'];
	$PHPReader = new PHPExcel_Reader_Excel2007(); 
	if(!$PHPReader->canRead($filePath)){ 
	    $PHPReader = new PHPExcel_Reader_Excel5(); 
	    if(!$PHPReader->canRead($filePath)){ 
	       echo 'no Excel'; 
	       return; 
	    }
	}  
    $PHPExcel = $PHPReader->load($filePath); 
    $currentSheet = $PHPExcel->getSheet(0);         //读取第一张工作表
    $allColumn = $currentSheet->getHighestColumn(); //取得最大的列号
    $allRow = $currentSheet->getHighestRow();       //取得一共有多少行
	$arr=[];
    for($currentRow = 1;$currentRow <= $allRow;$currentRow++){
      	for($i='A';$i<= $allColumn; $i++){ 
			$item = (string)$currentSheet->getCellByColumnAndRow(ord($i) - 65,$currentRow)->getValue();
			print_r($item);
			echo "<pre>";
			//插入数据到数据库 
			$strsql = "insert into arr_content (content) values('$item')";
			//mysql_query() 函数执行一条 MySQL 查询。SELECT,SHOW,EXPLAIN 或 DESCRIBE 都需要用这个函数执行
			$result = @mysql_query($strsql);
        } 
		$val[]=$item;
    }
    //echo "<pre>";
	// print_r(serialize($val));
  $q = "SELECT * FROM arr_content"; //SQL查询语句 SELECT * FROM 表名
    $rs = mysql_query($q); //获取数据集
    if(!$rs){die("数据库没有数据!");}
?>

猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/80356813