gpt4 book ai didi

javascript - 使用ajax形式发布数据id

转载 作者:太空宇宙 更新时间:2023-11-04 15:47:45 26 4
gpt4 key购买 nike

我有一个关于 ajax 表单的问题。所以,你可以看到我下面的ajax代码有 data-id 。我在 .pmsc div 中获取此 id。但是当我按 #upmsc 上传图像时,我没有收到任何错误,只是从 chrome 开发人员控制台获取此结果,没有上传,也没有在数据库中插入图像名称。我在这里做错了什么。

enter image description here

然后ajax提交表单是这样的

$('body').on('change', '#upmsc', function(e) {
e.preventDefault();
var id = $(".pmsc").attr("data-id");
var data = 'id=' + id;
$("#musicform").ajaxForm({
type: "POST",
data: data,
cache: false,
target: '.appended',
beforeSubmit: function() {
// Do Something
},
success: function(response) {
// Do Something
$(".showecho").html(response);
},
error: function() {}
}).submit();
});

我的 html 表单是这样的:

    <form id="cfrm" class="options-form" method="post" enctype="multipart/form-data" action="'.$base_url.'upload.php">
<label class="mcup" for="upcmsc"></label>
<input type="file" name="musicCover" id="upcmsc" />
</form>
<div class="pmsc" id="56">
</div>

PHP 代码是这样的

<?php
include_once 'inc/inc.php';
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['id'])) {
$id = mysqli_real_escape_string($db, $_POST['id']);
$name = $_FILES['musicCover']['name'];
$size = $_FILES['musicCover']['size'];
if(strlen($name)) {
$ext = getExtension($name);
if(in_array($ext,$valid_formats)) {
if($size<(50024*50024)) {
$actual_image_name = time().$uid.".".$ext;
$tmp = $_FILES['musicCover']['tmp_name'];
if(move_uploaded_file($tmp, $mcoverpath.$actual_image_name)) {
$data=$HuHu->Music_Cover_Image_Upload($id,$actual_image_name);
if($data){
echo '<img class="cover covermsc" src="'.$base_url.$mcoverpath.$actual_image_name.'">';
}
} else {
echo "Fail upload folder with read access.";
}
} else
echo "Image file size max 1 MB";
} else
echo "Invalid file format.";
} else
echo "Please select image..!";
exit;
}


?>

最佳答案

var data = 'id=' + id;
$("#musicform").ajaxForm({
type: "POST",
data: data,

您将附加数据设置为字符串,而它应该是对象。那么 PHP 代码中的检查就会失败。所以试试这个:

var data = {id: id};

发现 PHP 代码大括号还有一些问题,试试这个:

<?php
include_once 'inc/inc.php';
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['id'])) {
$id = mysqli_real_escape_string($db, $_POST['id']);
$name = $_FILES['musicCover']['name'];
$size = $_FILES['musicCover']['size'];

if(strlen($name)) {
$ext = getExtension($name);
if(in_array($ext,$valid_formats)) {
if($size<(50024*50024)) {
$actual_image_name = time().$uid.".".$ext;
$tmp = $_FILES['musicCover']['tmp_name'];

if(move_uploaded_file($tmp, $mcoverpath.$actual_image_name)) {
$data=$HuHu->Music_Cover_Image_Upload($id,$actual_image_name);
if($newdata){
echo '<img class="cover covermsc" src="'.$base_url.$mcoverpath.$actual_image_name.'">';
}
} else {
echo "Fail upload folder with read access.";
}
} else {
echo "Image file size max 1 MB";
}
} else {
echo "Invalid file format.";
}
} else {
echo "Please select image..!";
exit;
}
} else {
echo '1';
}
?>

关于javascript - 使用ajax形式发布数据id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43413677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com