我是一名Web开发人员,但所有人都有一个他无法纠正的错误。 这是我创建新表单以向网站添加商品 我发送ajax请求,但返回空数组
我的JS
(function(a){
let req = {};
req.url = "/action/do/done";
req.type = "POST";
req.dataType = "html";
a(".file").change(()=>{
let fileList = $(".file")['0'].files, fileCount = fileList.length;
if (fileCount < 10 && fileCount > 0) {
a('.spanText').html(fileCount + " şəkil seçdiniz...");
$(fileList).each((i,el) => {
let data = new FormData();
data.append('section', 'general');
data.append('action', 'previewImg');
data.append('type', 'loadImage');
data.append('image', fileList[i]);
req.contentType = !1;
req.processData = !1;
req.data = data;
req.success = (e) => {
console.log(e);
if (i === 0) {
a(".file").attr('datasrc', e);
} else {
a(".file").attr('datasrc', a(".file").attr('datasrc')+';'+e);
}
}
a.ajax(req);
});
} else if (fileCount > 10) {
a('.spanText').html("Bir balaca çox şəkil seçdiniz.");
} else {
a('.spanText').html("Şəkil seçin...");
}
});
a(".good-colors select").change(()=>{
a(".good-colors select").css('backgroundColor', $(".good-colors select")['0'].selectedOptions['0'].attributes['2'].nodeValue);
});
a(".submit").click(() => {
let data = {};
data.type = "addNewGood";
data.goodType = $(".selected").val();
data.name = a(".gd-name").text().toLowerCase();
data.about = a(".gd-about").text().toLowerCase();
data.cost = a(".gd-cost").text().toLowerCase();
if (data.goodType === "parfumes") {
let amount = {};
let count = {};
for (var i = 0; i < $(".gd-parfume-amount").length; i++) {
amount[i] = a(`.gd-parfume-amount:nth-child(${i+1})`).text();
}
for (var i = 0; i < $(".gd-parfume-count").length; i++) {
count[i] = a(`.gd-parfume-count:nth-child(${i+1})`).text();
}
data.amount = amount;
data.count = count;
} else if (data.goodType === "dresses") {
let amount = {};
let count = {};
for (var i = 0; i < $(".gd-sc-amount").length; i++) {
amount[i] = $('.gd-sc-amount')[i].innerText;
}
for (var i = 0; i < $(".gd-sc-count").length; i++) {
count[i] = $('.gd-sc-count')[i].innerText;
}
data.amount = amount;
data.count = count;
data.color = a(".good-colors select")['0'].selectedOptions['0'].attributes['2'].nodeValue;
}
data.category = a(".good-category select")['0'].selectedOptions['0'].attributes['1'].nodeValue;
if (typeof $(".file").attr('datasrc')!=="undefined") {
data.image = $(".file").attr('datasrc');
} else {
$(".file").click();
}
req.data = data;
req.success = e => {
// console.clear();
a("._404NotFound").removeClass('_404NotFound');
e = e.split("---");
if (e['0'] === "error") {
$(`.${e['1']}`).addClass('_404NotFound').focus();
alert(e['2']);
} else if (e['0'] === 'done') {
alert("Mal Uğurla daxil edildi");
window.location.reload();
} else {
console.log(e.join('---'));
}
}
a.ajax(req);
});
})(function(a){return $||jQuery}(!1));
这是我的PHP
<?php
// Nedded libraries
require("../../const.php");
require("md6.php");
// Main variables
$h = new md6();
// Defines
define("p", $_POST);
define("t", p['type']);
// Functions
function c($t, $p = false||0) {
if ($p === true || $p === 1) {
if (strlen($t) !== 64) return false;
return true;
}
if (strlen($t) < 4) return false;
if (strlen($t) > 32) return false;
return true;
}
function d($arr) {
for ($i = 0; $i < count($arr); $i++) {
if (empty($arr[$i])) return false;
if (!is_numeric($arr[$i])) return false;
}
return true;
}
function b($t) {
if ($t === "") return false;
return true;
}
function i($t) {
if ($t === "") return false;
$t = explode(";", $t);
for ($i = 0; $i < count($t); $i++) {
$p = explode(".", $t);
if ($p!="png"&&$p!="jpg"&&$p!="jpeg"&&$p!="mp4") return false;
if (!file_exists(IMG.$t)) return false;
}
return true;
}
// newGood Page
if (t === "loadImage") {
$n = "c/".$h->hex(time().mt_rand(000,999), 64, mt_rand(000,999), 16).".".explode("/", $_FILES['image']['type'])['1'];
rename($_FILES['image']['tmp_name'], IMG.$n);
echo $n;
} elseif (t === "addNewGood") {
$gd = p['goodType'];
$nm = p['name'];
$ab = p['about'];
$cs = p['cost'];
$am = p['amount'];
$ca = p['category'];
$ct = p['count'];
$cl = p['color'];
$im = p['image'];
if (!c($nm)) {
$error[] = "gd-name---Ad daxil edilməyib və ya uzunluğu [4-32] standartına uyğun gəlmir.";
}
if (!d($am)) {
$error[] = "gd-sc-amount---Daxil etdiyiniz say ya boşdur ya da qadağan olunmuş simvollardan ([a-z][A-Z][!@#$%^&*(){}[],<>?\\|/]) istifadə edilib.";
}
if (!d($ct)) {
$error[] = "gd-sc-count---Daxil etdiyiniz say ya boşdur ya da qadağan olunmuş simvollardan ([a-z][A-Z][!@#$%^&*(){}[],<>?\\|/]) istifadə edilib.";
}
if (!c($cs)&&!is_numeric($cs)) {
$error[] = "gd-cost---Qiymət daxil edilməyib və ya da qadağan olunmuş simvollardan ([a-z][A-Z][!@#$%^&*(){}[],<>?\\|/]) istifadə edilib.";
}
if (!b($ca)) {
$error[] = "good-category select---Kateqoriya seçilməyib.";
}
if ($gd === "dresses") {
if (!b($cl)) {
$error[] = "good-colors select---Kateqoriya seçilməyib.";
}
}
if (!i($im)) {
$error[] = "good-file---Seçilmiş medianın tipi standarta [jpg,jpeg,png,mp4] uyğun deyil.";
}
if (!empty($error))
echo "error---" . $error['0'];
else
"done_";
}
print_r(p);
那是我的HTML
<?php
$x = true;
$a = array ('black', 'red', 'green', 'blue', 'white');
?>
<form enctype="multipart/form-data" method="post"><!-- #973692 -->
<section class="good-data good-type data-all hidden">
<input required type="hidden" class="page" value="<?=$_REQUEST['selectedPage']?>" />
<input required type="hidden" class="sec" value="<?=time();?>" />
<input required type="hidden" class="date" value="<?=date("Y-m-d H:i:s", time());?>" />
<input required type="hidden" class="selected" value="" />
</section>
<h1>yeni mal əlavə et</h1>
<section class="good-data good-type">
<label>
<input type="radio" class="data-area parfume" dataopenclassnames="data-parfumes;data-all" name="type" datatype="parfumes" onchange="areaFinder(this)" />
<b class="bgtext" data="Ətir"></b>
</label>
<label>
<input type="radio" class="data-area dress" dataopenclassnames="data-dresses;data-all" name="type" datatype="dresses" onchange="areaFinder(this)" />
<b class="bgtext" data="Paltar"></b>
</label>
<!-- <input type="radio" class="" name="type" /> -->
</section>
<section class="good-data good-name data-all hidden">
<h3>Malın adı <i class="fa fa-star"></i></h3>
<div contenteditable="plaintext-only" class="gd-name"></div>
</section>
<section class="good-data good-about data-all hidden">
<h3>Mal haqqında</h3>
<div contenteditable="plaintext-only" class="gd-about"></div>
</section>
<section class="good-data good-sc data-dresses hidden">
<h3>Ölçü və miqdarı <i class="fa fa-star"></i></h3>
<div colspan="2" class="afl"><span contenteditable="plaintext-only" class="gd-sc-amount"></span><span>=></span><span contenteditable="plaintext-only" class="gd-sc-count"></span></div>
<button type="button" onclick="$(this).before('<div colspan=\'2\' class=\'afl\'><span contenteditable=\'plaintext-only\' class=\'gd-sc-amount\'></span><span>=></span><span contenteditable=\'plaintext-only\' class=\'gd-sc-count\'></span></div>')" class="fa fa-plus gd-sc-add"></button>
</section>
<section class="good-data good-parfume data-parfumes hidden">
<h3>Miqdar və sayı <i class="fa fa-star"></i></h3>
<div colspan="2" class="afl"><span contenteditable="plaintext-only" class="gd-parfume-amount"></span><span>=></span><span contenteditable="plaintext-only" class="gd-parfume-count"></span></div>
<button type="button" onclick="$(this).before('<div colspan=\'2\' class=\'afl\'><span contenteditable=\'plaintext-only\' class=\'gd-parfume-amount\'></span><span>=></span><span contenteditable=\'plaintext-only\' class=\'gd-parfume-count\'></span></div>')" class="fa fa-plus gd-parfume-add"></button>
</section>
<section class="good-data good-cost data-all hidden">
<h3>Malın qiyməti <i class="fa fa-star"></i></h3>
<div contenteditable="plaintext-only" class="gd-cost"></div>
</section>
<section class="good-data good-category data-all hidden">
<select>
<option class="data-all" disabled selected value="r2124">Birini seçin</option>
<option class="data-parfumes" value="p_w">Qadın ətir</option>
<option class="data-parfumes" value="p_m">Kişi Ətir</option>
<option class="data-dresses" value="d_w">Qadın geyimi</option>
<option class="data-dresses" value="d_m">Kişi geyimi</option>
</select>
</section>
<section class="good-data good-colors data-dresses hidden">
<select>
<option class="data-all" disabled selected>Birini seçin</option>
<?php
for ($i = 0; $i < count($a); $i++) {
echo " <option class=\"data-dresses\" style=\"background-color: {$a[$i]};\" datacolor=\"{$a[$i]}\"></option>";
}
?>
</select>
</section>
<section class="good-data good-file data-all hidden">
<label class="inputfile">
<input required type="file" class="file" multiple />
<!-- Show and can be cut images -->
<i class="fa fa-upload"></i>
<span class="spanText">Şəkilləri seçin (1 - 10) arası <i class="fa fa-star"></i></span>
</label>
<div class="showImages"></div>
<div class="dropImages"></div>
</section>
<section class="submit data-all hidden">
<input required type="button" class="button" value="Göndər" />
</section>
</form>
<script src="/script/ajax.jquery.min.js"></script>
<script src="/script/p.newGood.js"></script>
我不明白为什么它返回空数组。在这里,我制作数据和req作为对象,因为当我在此处最小化代码时,最小代码是可读的。请求变量(对象)是我发送到Web服务器的请求。数据变量(对象)是我发送到后端的请求的内容。 预先安排。
答案 0 :(得分:0)
我发现了错误。我将请求开始时间设置为1次,但发送次数超过1次;