提交表单后,我无法获取发布数据,不知道问题出在哪里,但我认为这是JS文件。我不擅长使用Ajax,所以我想这就是问题所在。

我犯了一个不可能的错误

$name, $projectFile

等。

那么我应该重写JS还是PHP?没有语法错误,我希望使用Ajax动态提交此表单。

HTML

Project Name

Project Link (Gitlab/Github)

Project Upload

Project Website

Add Project

JS

$('#upload-form').on('submit', (e) => {

e.preventDefault();

let $this = $(this);

let $name = $('#projectname');

let $url = $('#projecturl');

let $file = $('#projectpreview');

let $web = $('#projectwebsite');

$.ajax({

url: '../assets/upload.php',

type: 'POST',

data: new FormData($(this)),

contentType: false,

cache: false,

processData: false,

success: (response) => {

console.log(response);

}

});

// VALIDATION

$('#file').change(() => {

let file = this.files[0];

let imageFile = file.type;

let match = ['image/jpeg', 'image/png', 'image/jpg'];

if (!((imageFile == match[0]) || (imageFile == match[1]) || (imageFile == match[2]))) {

alert('Pleas select valid file: JPEG, PNG, JPG');

$('#file').val('');

return false;

}

});

});

PHP

include('db.php');

$name = $_POST['projectname'];

$projectUrl = $_POST['projecturl'];

$projectFile = $_FILES['projectpreview'];

$projectWebsite = $_POST['projectwebsite'];

$date = date('Y-m-d H:i:s');

if (!empty($name) || !empty($projectUrl) || !empty($projectFile['name']) || !empty($projectWebsite)) {

$upFile = '';

if (!empty($projectFile['type'])) {

$fileName = time().'_'.$projectFile['name'];

$valid_extensions = array('jpeg', 'jpg', 'png');

$temporary = explode('.', $projectFile['name']);

$file_extension = end($temporary);

if ((($_FILES['hard_file']['type'] == 'image/png') || ($projectFile['type'] == 'image/jpeg') || ($projectFile['type'] == 'image/jpg')) && in_array($file_extension, $valid_extensions)) {

$sourcePath = $projectFile['tmp_name'];

$targetPath = 'img/photos/'.$fileName;

if (move_uploaded_file($sourcePath, $targetPath)) {

$uploadedFile = $fileName;

}

}

}

if ($connection->query("INSERT INTO projects VALUES('', '$name', '$projectUrl', '$uploadedFile', '$projectWebsite', '$date')")) {

exit('success');

} else {

exit('fail');

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐