我正在尝试使用AJAX上传图像和一些字符串/数字。除了一个非常重要的问题,代码工作正常(因为,一切都进入数据库)。我非常想要"字符"变量是唯一的,并且我用于尝试已经采取的字符名称的错误消息的每次尝试都不起作用。这是我的代码:
AJAX:
function CSubmit() {
clearInterval(MoveDrawTimer);
var a=document.forms['form-id']["thefiles"].value; if (a == "") { document.getElementById("info").innerHTML="You must upload an image."; return };
if (showFileSize() > 5000000) { document.getElementById("info").innerHTML="File is too big. Maximum size is 5,000,000 bytes."; return };
var accepted = [".gif",".png",".jpg",".jpeg"];
if (Ext in oc(accepted) == true) {
var dataURLToBlob = function(dataURL) {
var BASE64_MARKER = ';base64,';
if (dataURL.indexOf(BASE64_MARKER) == -1) {
var parts = dataURL.split(',');
var contentType = parts[0].split(':')[1];
var raw = parts[1];
return new Blob([raw], {type: contentType});
}
var parts = dataURL.split(BASE64_MARKER);
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], {type: contentType}); };
newImage = dataURLToBlob(dataURL); } else { document.getElementById("info").innerHTML="Cannot send file. Check your extensions."; return };
var canvas = document.getElementById("Cinfo");
var context = canvas.getContext("2d");
context.clearRect(0, 0, 400, 400);
var TotalSpent = StrPts + IntPts + WisPts + SpdPts + MovPts;
var theform = document.getElementById("CharName");
if (theform.value == "") { document.getElementById("info").innerHTML="Your character must have a name."; return }
if ( /[^a-zA-Z0-9]/.test(theform.value) ) {
document.getElementById("info").innerHTML="Name must contain only letters and / or numbers."; return }
if (theform.value.length > 14) { document.getElementById("info").innerHTML="Character names must be 14 characters max."; return }
if (TotalSpent !== 2) { document.getElementById("info").innerHTML="You must spend exactly 2 points."; return }
var fd = new FormData();
fd.append('data', newImage);
fd.append('character', theform.value);
fd.append('str', StrPts);
fd.append('int', IntPts);
fd.append('wis', WisPts);
fd.append('spd', SpdPts);
fd.append('mov', MovPts);
//beginning of server transmission. above is javascript front end authentication.
//sending the following information:
//theform.value // character name
//StrPts, IntPts, WisPts, SpdPts, MovPts // the character stats
var xmlhttp;
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function(){ xmlhttp.addEventListener("progress", document.getElementById("info").innerHTML="Please wait . . ." , false);
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
if (xmlhttp.responseText.length <= 14) {
document.getElementById("info").innerHTML="Congratulations, " + xmlhttp.responseText + " has been approved.";
SelectScreen();} } }
xmlhttp.open("POST","CHunique.php",true);
xmlhttp.send(fd);
};
正如您所看到的,我将所有内容附加到表单然后将其发送到CHunique.php,这是以下(目前是用于演示问题的不完整的测试页):
<?php
@$con=mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("Could not connect to the database. Please try again later.");
mysqli_set_charset($con,"UTF8");
$Err = "fine";
session_start();
$login = $_SESSION['account'];
$CHresult = mysqli_query($con,"SELECT * FROM CHstats");
while($row = mysqli_fetch_array($CHresult)) {
$thecharacters = $row['character'];
if (strtolower($character) === strtolower($thecharacters)) { $Err = " Character name already in use."; break; }; }
这里的问题是我根本没有得到任何来自服务器的回复。它保持无限的进步&#34;进步&#34;功能,也就是说&#34;请稍候。 。 。 &#34;对于我在php页面上尝试的所有while循环都是如此...服务器刚刚加载。这是一个很大的问题,因为我需要使用while循环遍历所有字符,以查看用户的名称是否唯一。
同样,一切都运行正常,包括图片上传,除此检查外。这个检查非常重要,我想知道为什么我在php页面中尝试的每个while循环都会导致无限的加载时间?请帮忙。
另请注意,我不使用JQuery。
答案 0 :(得分:1)
为什么不改变您的查询以便为您完成工作,在PHP中进行比较是否真的有意义
$query = sprintf("SELECT * FROM CHstats WHERE LOWER(character) = LOWER('%s')", $thecharacters);
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result) > 0){
// duplicate character name
}
另外,你确定while循环是导致页面挂起的原因吗?与mysql的连接很可能没有被制作或者需要很长时间。如果您还没有,请在代码周围移动一个die语句,以查看它被挂起的位置。
答案 1 :(得分:0)
YES!经过几个小时的挫折,我实际上已经解决了这一切。答案在于我的愚蠢的AJAX条件:if(xmlhttp.responseText.length&lt; = 14)。响应超过14个字符,从而导致服务器无限期加载。