慢慢地但肯定地,我将获得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);
?>
$("#aTable").hide('slow', function () { //this is not working
alert('Working on it.');
});
$("#tableText").hide('slow', function() {//this is not working
alert('Working on it.');
});
上面的代码不起作用,因为您的html代码中没有任何id为"#aTable"或"#tableText"的元素。
尝试print_r($_POST)查看是否所有值都到达服务器。
此外,使用JQuery的Ajax函数将使您的代码更容易。。。下面是的样本
$.ajax({
url : $domain + "/index/email/" + "?" + $arguments, //add your args here...
cache : false,
beforeSend : function (){
alert('sending....');
},
complete : function($response, $status){
if ($status != "error" && $status != "timeout") {
if($response.responseText == "200"){
alert('done');
} else {
alert($response.responseText);
}
}
},
error : function ($responseObj){
alert("Something went wrong while processing your request.nnError => "
+ $responseObj.responseText);
}
});