gpt4 book ai didi

javascript - PHP未定义索引(jquery文件上传导致)

转载 作者:行者123 更新时间:2023-12-01 05:32:31 25 4
gpt4 key购买 nike

我一直在尝试为我正在开发的网站提供文件上传功能。我在表单之外执行此操作,经过几天的搜索,我终于从 this question: 的答案中找到了适合我的方法的东西。

问题是,当我将代码应用到自己的脚本中时,我收到“未定义索引”错误,当我删除它时,一切正常。

这是我的代码:

HTML

<div class='error-msg'></div>
<input type='text' id='newsitem-message' />
<input type='file' id='newsitem-thumbnail' />
<div class='submit' onclick='newsItem(\"insert\");'>Post news</div>

jQuery

function newsItem(action){
var thumbnail = $('#newsitem-thumbnail')[0].files[0];
var fileReader = new FileReader();
fileReader.readAsText(thumbnail, 'UTF-8');
fileReader.onload = shipOff;

function shipOff(e){
var r = e.target.result;
if(action == "insert"){
$.post("requestPages/newsitems.php", {message:$("#newsitem-message").val(),
thumbnail:thumbnail.name,
action:action,
data:r},
function(result){
console.log(result);
console.log(r); //This freezes my console/inspector window, forcing me to restart the browser-tab
if(result == "succes"){
window.location.reload();
}else{
$(".error-msg").html(result);
}
});
}else if(action == "delete"){
//To be implemented when I get the submit working D:
}
}
}

PHP(请原谅困惑 - 需要认真清理)

<?php
include("../assets/libs/SQLLib.php");
DB_Connect("test");

echo print_r($_POST);
echo var_dump($_POST);
$message = $_POST['message'];
$action = $_POST['action'];
$thumbnail = $_POST['thumbnail'];
$data = $_POST['data'];

$serverFile = time().$thumbnail;
$fp = fopen('../assets/images/thumbnails/'.$serverFile, 'w');
fwrite($fp, $data);
fclose($fp);
$returnData = array("serverFile" => $serverFile);
echo json_encode($returnData);

if($_POST['message'] != ""){
$canPost = true;
}else{
echo "The message can not be empty.";
}

if($action == "insert" && $canPost){
$sql = "insert into newsitems
(date, message, thumbnail)
values
(NOW(),'".$message."', '".$thumbnail."')";
$result = mysql_query($sql);
if(!$result){
echo "Uh-oh! Something went wrong while posting the news! ".mysql_error();
}else{
echo "succes";
}
}else if($action == "delete"){
$sql = "";
}
?>

有人看出这里出了什么问题吗?或者有人有其他选择吗?

希望有人能帮我解决这个问题。

最佳答案

像这样改变它:

include("../assets/libs/SQLLib.php");
DB_Connect("test");

print_r($_POST); //Dont echo, what is already echoed
var_dump($_POST); //Dont echo, what is already echoed
$message = !empty($_POST['message'])?$_POST['message']:'';
$action = !empty($_POST['action'])?$_POST['action']:'';
$thumbnail = !empty($_POST['thumbnail'])?$_POST['thumbnail']:'';
$data = !empty($_POST['data'])?$_POST['data']:'';
if(!empty($thumbnail) && !empty($data)){

$serverFile = time().$thumbnail;
$fp = fopen('../assets/images/thumbnails/'.$serverFile, 'w');
fwrite($fp, $data);
fclose($fp);
$returnData = array("serverFile" => $serverFile);
echo json_encode($returnData);
} else {
echo json_encode(array('error'=>'No data and thumbnail assigned'));
}

if($message != ""){
$canPost = true;
}else{
echo "The message can not be empty.";
}

if($action == "insert" && $canPost){
$sql = "insert into newsitems
(date, message, thumbnail)
values
(NOW(),'".$message."', '".$thumbnail."')";
$result = mysql_query($sql);
if(!$result){
echo "Uh-oh! Something went wrong while posting the news! ".mysql_error();
}else{
echo "success";
}
}else if($action == "delete"){
$sql = "";
}

您只需更改 .htaccess 或 php 中的错误报告级别即可防止显示警告消息。在 .htaccess 中:

php_flag error_reporting E_ERROR

如果在 .php 文件中,则

<?php error_reporting(E_RROR); //This displays only Errors, no warning and notices.

关于javascript - PHP未定义索引(jquery文件上传导致),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398993/

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