慢慢但肯定地,我会得到 AJAX。我有一个将文本字段和文件上传到数据库的表单。我之前在 PHP 中使用过查询,但在 AJAX 中没有。现在 AJAX 可以工作,但 PHP 不行。而且我知道有些人会觉得将图像加载到 BLOB 是令人反感的,但查询本身是有效的,所以我想专注于让我的 javascript 与我的 PHP 对话时遇到的问题。我已经疯狂地研究了这个问题并尝试了很多东西,但我发现上传文件很复杂。
问题 1. 如果我错了,请纠正我,但是如果 javascript 和 jquery 实现“POST”调用,传递的参数不应该出现在页面的 URL 中?因为他们是。
2. 为什么我的PHP文件没有解析出发送的数据并将其发送到数据库?我可以在 URL 和 Firebug(虽然我也在慢慢学习 Firebug)中看到数据正在传递。我运行了一个测试 php 文件,我正在使用该文件连接数据库。
谢谢!
HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>
<script src="jquery.validate.js"></script>
<script src="jquery.form.js"></script>
<script>
$(document).ready(function(){
$('#addForm').validate();
function addRecord() {
$("#aTable").hide('slow', function () { //this is not working
alert('Working on it.');
});
$("#tableText").hide('slow', function() {//this is not working
alert('Working on it.');
});
var output = document.getElementById("message");
var nAname = document.getElementById("aname");
var nAInfo = new FormData(document.forms.namedItem("addForm"));
nAInfo.append('aname', nAname);
$.ajax({
type: "POST",
url: "addPhoto.php",
data: nAInfo
});
});
</script>
</head>
<body>
<form id="addForm" name="addForm" onsubmit="addRecord()" enctype="multipart/form-data">
<label>Name: </label>
<input type="text" id="aname" name="aname" class=required/>
<br>
<br>
<label>Photo: </label>
<input type="file" id="aimage" name="aimage" class="required">
<br>
<br>
<input type="submit" value="ADD" />
<br>
</form>
<div id="message" name="message"></div>
<br>
<br>
<div id="image_display"></div>
</body>
</html>
PHP
<?php
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting(E_ALL);
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
}
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $_SERVER['REQUEST_METHOD'];
$aname = $_POST['aname'];
$errorinfo = $_FILES["aimage"]["error"];
$filename = $_FILES["aimage"]["name"];
$tmpfile = $_FILES["aimage"]["tmp_name"];
$filesize = $_FILES["aimage"]["size"];
$filetype = $_FILES["aimage"]["type"];
$fp = fopen($tmpfile, 'r');
$imgContent = fread($fp, filesize($tmpfile));
fclose($fp);
if (!($filetype == "image/jpeg" && $filesize > 0)) {
echo "Import of photo failed";
}
if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) {
if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage_data) VALUES (?,?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error;
}
if (!$stmt->bind_param("ss", $aname, $imgContent)) {
echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$stmt->close();
}
else {
echo "Image must be under 1 MB";
}
echo mysqli_error();
mysqli_close($mysqli);
?>