将一个php网页转换为ajax调用
我有一个php网页,可以将cvs文件上传到我的postgres数据库。它工作得很好,但是,我想将这个php网页转换成一个HTMl页面,并使用ajax调用一个单独的php脚本。在没有额外外部资源的情况下,实现这一目标的最佳方法是什么 这是我的密码将一个php网页转换为ajax调用,php,jquery,ajax,upload,Php,Jquery,Ajax,Upload,我有一个php网页,可以将cvs文件上传到我的postgres数据库。它工作得很好,但是,我想将这个php网页转换成一个HTMl页面,并使用ajax调用一个单独的php脚本。在没有额外外部资源的情况下,实现这一目标的最佳方法是什么 这是我的密码 <?php $db = "postgres"; $host = "localhost"; $port = "5432"; $user = "xxxx"; $password = "xxxx"; $timeout = "5"; $table = "m
<?php
$db = "postgres";
$host = "localhost";
$port = "5432";
$user = "xxxx";
$password = "xxxx";
$timeout = "5";
$table = "my_table";
$dbconn = pg_connect("host = $host port = $port dbname = $db user = $user password = $password connect_timeout = $timeout ")
or die("Could not connect:" . pg_last_error());
pg_query("TRUNCATE TABLE $table");
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file, "r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$queryA = "INSERT INTO $table (usb_number, affiliation, type_company, status, region, company_name, full_address, street_number, city, state, county, zip_code, cs_account, cs_store_number, c_type, fully_verified, expiring_soon, phone_number, latitude, longitude) VALUES
(
'" . addslashes($data[0]) . "',
'" . addslashes($data[1]) . "',
'" . addslashes($data[2]) . "',
'" . addslashes($data[3]) . "',
'" . addslashes($data[4]) . "',
'" . addslashes($data[5]) . "',
'" . addslashes($data[6]) . "',
'" . addslashes($data[7]) . "',
'" . addslashes($data[8]) . "',
'" . addslashes($data[9]) . "',
'" . addslashes($data[10]) . "',
'" . addslashes($data[11]) . "',
'" . addslashes($data[12]) . "',
'" . addslashes($data[13]) . "',
'" . addslashes($data[14]) . "',
'" . addslashes($data[15]) . "',
'" . addslashes($data[16]) . "',
'" . addslashes($data[17]) . "',
'" . addslashes($data[18]) . "',
'" . addslashes($data[19]) . "'
)
";
$resultA = pg_query($queryA);
}
} while ($data = fgetcsv($handle, 1000, ","));
//
pg_query("UPDATE $table SET
geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)");
if(!$resultA){
$errrormessage = pg_last_error();
echo "Error with query: ".$errormessage;
exit();
}
echo "Table updated!";
//redirect
// header('Location: import.php?success=1');
die;
}
?>
<!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=iso-8859-1" />
<title>Import a CSV File with PHP & Postgres</title>
<link href="../lib/css/datatables.min.css" rel="stylesheet" type="text/css"/>
<link href="../lib/font-awesome-4.4.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<link href="../lib/js/bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="../lib/js/jquery-2.1.4.js" type="text/javascript"></script>
<script src="../lib/js/jquery-ui-1.11.4/jquery-ui.min.js" type="text/javascript"></script>
<script src="../lib/js/bootstrap-3.3.5-dist/js/bootstrap.min.js" type="text/javascript"></script>
<script src="../lib/js/datatables.min.js" type="text/javascript"></script>
<style>
.btn-file {
position: relative;
overflow: hidden;
height: 35px;
}
.btn-file input[type=file] {
position: absolute;
top: 0;
right: 0;
min-width: 100%;
min-height: 100%;
font-size: 100px;
text-align: right;
filter: alpha(opacity=0);
opacity: 0;
outline: none;
background: white;
cursor: inherit;
display: block;
}
fieldset {
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}
</style>
</head>
<?php
$resultA;
// ?>
<body>
<div class="col-lg-12">
<br/><br/>
<div class="col-lg-3">
<form class="form-inline" role="form" action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<fieldset>
<legend>Update Client Table</legend>
<span class="btn btn-md btn-default btn-file">
<i class="fa fa-search"></i> Browse <input name="csv" type="file" id="csv" />
</span>
<!--input name="csv" type="file" id="csv" /-->
<button class="form-control btn btn-md btn-primary" type="submit" name="Submit" value="Submit">Submit</button>
</fieldset>
</form>
</div>
</div>
</body>
</html>
使用PHP和Postgres导入CSV文件
.btn文件{
位置:相对位置;
溢出:隐藏;
高度:35px;
}
.btn文件输入[类型=文件]{
位置:绝对位置;
排名:0;
右:0;
最小宽度:100%;
最小高度:100%;
字体大小:100px;
文本对齐:右对齐;
过滤器:alpha(不透明度=0);
不透明度:0;
大纲:无;
背景:白色;
游标:继承;
显示:块;
}
字段集{
-webkit边界半径:8px;
-moz边界半径:8px;
边界半径:8px;
}
更新客户端表
浏览
提交
您需要进行ajax上传,如本问题所述
首先,单击按钮制作一篇ajax文章:
<form class="form-inline" role="form" action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<fieldset>
<legend>Update Client Table</legend>
<span class="btn btn-md btn-default btn-file">
<i class="fa fa-search"></i> Browse <input name="csv" type="file" id="csv" />
</span>
<!--input name="csv" type="file" id="csv" /-->
<button class="form-control btn btn-md btn-primary" type="submit" name="Submit" value="Submit">Submit</button>
</fieldset>
</form>
<script>
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'upload.php', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
//Ajax events
beforeSend: beforeSendHandler,
success: function(retdata){
alert(retdata); // This will be the echo from the php script
}
error: function(){
alert('Error!');
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
</script>
更新客户端表
浏览
提交
$(':按钮')。单击(函数(){
var formData=新的formData($('form')[0]);
$.ajax({
url:'upload.php',//用于处理数据的服务器脚本
键入:“POST”,
xhr:function(){//自定义XMLHttpRequest
var myXhr=$.ajaxSettings.xhr();
返回myXhr;
},
//Ajax事件
beforeSend:beforeSendHandler,
成功:函数(retdata){
警报(retdata);//这将是php脚本的回音
}
错误:函数(){
警报('错误!');
},
//表单数据
数据:formData,
//告诉jQuery不要处理数据或担心内容类型的选项。
cache:false,
contentType:false,
processData:false
});
});
然后在upload.php中完成所有与postgres等相关的操作。确保在退出/退出之前回显响应,以便Javascript完整功能能够了解发生了什么
<?php
$db = "postgres";
$host = "localhost";
$port = "5432";
$user = "xxxx";
$password = "xxxx";
$timeout = "5";
$table = "my_table";
$dbconn = pg_connect("host = $host port = $port dbname = $db user = $user password = $password connect_timeout = $timeout ")
or die("Could not connect:" . pg_last_error());
pg_query("TRUNCATE TABLE $table");
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file, "r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$queryA = "INSERT INTO $table (usb_number, affiliation, type_company, status, region, company_name, full_address, street_number, city, state, county, zip_code, cs_account, cs_store_number, c_type, fully_verified, expiring_soon, phone_number, latitude, longitude) VALUES
(
'" . addslashes($data[0]) . "',
'" . addslashes($data[1]) . "',
'" . addslashes($data[2]) . "',
'" . addslashes($data[3]) . "',
'" . addslashes($data[4]) . "',
'" . addslashes($data[5]) . "',
'" . addslashes($data[6]) . "',
'" . addslashes($data[7]) . "',
'" . addslashes($data[8]) . "',
'" . addslashes($data[9]) . "',
'" . addslashes($data[10]) . "',
'" . addslashes($data[11]) . "',
'" . addslashes($data[12]) . "',
'" . addslashes($data[13]) . "',
'" . addslashes($data[14]) . "',
'" . addslashes($data[15]) . "',
'" . addslashes($data[16]) . "',
'" . addslashes($data[17]) . "',
'" . addslashes($data[18]) . "',
'" . addslashes($data[19]) . "'
)
";
$resultA = pg_query($queryA);
}
} while ($data = fgetcsv($handle, 1000, ","));
//
pg_query("UPDATE $table SET
geom = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)");
if(!$resultA){
$errrormessage = pg_last_error();
echo "Error with query: ".$errormessage;
exit();
}
echo "Table updated!";
die;
}
?>
SO不是代码转换服务这显然是请求某人为mebeforeSend引用“beforeSendHandler”执行此操作。这个函数在哪里?你可以忽略它。如果您想在发布请求之前立即执行某些操作,那么可以在这里运行一些代码。