gpt4 book ai didi

javascript - PHP 代码总是设置文件上传标志,即使没有上传任何内容

转载 作者:行者123 更新时间:2023-12-03 03:17:51 25 4
gpt4 key购买 nike

这是我的 HTML 代码:

<form id="broadcastForm" onSubmit="return false;" enctype="multipart/form-data">
<div>
<label for="image">Select an Image to Broadcast (Optional) <span class="text-danger">[Make sure image size is less than 500Kb]</span></label>
<input type="file" accept="image/*" class="btn btn-primary" id="image" name="image[]" class="fileinput" style="margin-top: 10px;" />
<div class="preview_box">
<img id="preview_img" src=""/>
</div>
</div>
<textarea class="textarea form-control" name="message" id="message" placeholder="Enter Message To Broadcast.." style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>
</form>

这是我的 JQuery 代码:

$("button#brodcastMessage").click(function()
{
var proceed = true, image = $("#image").val(), message = $("#message").val(), type = "", dataSend = "";
var formData = new FormData();

if(image == "" && message == "" || image == null && message == null)
{
proceed = false;
swal({
title: "Error Sending Email!",
text: "To send Email Broadcast to your subscribers you need to either select an image or enter the broadcast message. Both the fields are blank!",
icon: "error",
});
}

else if(image == "" || image == null && message != "")
{
proceed = true;
formData.append('message', message);
formData.append('image', "");
}

else if(message == "" || message == null && image != "")
{
proceed = true;
formData.append('image', $('#image')[0].files[0]);
}

else if(message != "" && image != "")
{
proceed = true;
formData.append('message', message);
formData.append('image', $('#image')[0].files[0]);
}

if(proceed)
{
$("button#brodcastMessage").addClass('disabled').attr('disabled', 'true').html("Sending Mail Please Wait...");

$.ajax({
url: '/ajax?id=broadcast',
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
success:function(response)
{
resp = response.slice(-3); //read last three characters of a string

if(resp == "ack")
{
$("form#upload_form").trigger('reset');
$("#image").val("");
$('#preview_img').attr('src', '');
$("button#brodcastMessage").removeClass('disabled').html('Send <i class="fa fa-arrow-circle-right"></i>').removeAttr('disabled');
$("form#emailFormSend").trigger('reset');
swal({
title: "Message Sent!",
icon: "success",
});
}
else if(resp == "nak")
{
$("button#brodcastMessage").removeClass('disabled').html('Send <i class="fa fa-arrow-circle-right"></i>').removeAttr('disabled');
swal({
title: "Error Sending Email!",
text: "Please Try Again!",
icon: "error",
});
}
else
{
$("button#brodcastMessage").removeClass('disabled').html('Send <i class="fa fa-arrow-circle-right"></i>').removeAttr('disabled');
swal({
title: "Error Log!",
text: response,
icon: "error",
});
}
},
error:function(response)
{
$("button#brodcastMessage").removeClass('disabled').html('Send <i class="fa fa-arrow-circle-right"></i>').removeAttr('disabled');
swal({
title: "Network Error!",
text: "There was an Error Processing your request! Please try again after some time!",
icon: "error",
});
}
});
}
});

这是我的 PHP 代码:

if($_GET['id'] == "broadcast")
{
if($_POST['message'] == "" || $_POST['message'] == null && isset($_FILES) && !empty($_FILES))
{
//file variable is set
$counter = "file";
}

if(isset($_FILES) && empty($_FILES) && $_POST['message'] != "" || $_POST['message'] != null)
{
//message varibale is set
$counter = "message";
}

if(isset($_FILES) && !empty($_FILES) && $_POST['message'] != "" || $_POST['message'] != null)
{
//both are present
$counter = "both";
}

echo $counter;
exit();
}

您好,我正在尝试进行ajax文件上传。现在我正在创建一条广播消息,其中我提供了 2 个输入

1.) Select image &
2.) Enter message

现在可以有 4 个条件

1.) Either both of them are blank (i have handled this on client side)
2.) Either I only want to broadcast message but not image
3.) Either I want to broadcast image but not message
4.) Either I want to broadcast both image and message

所以我在 PHP 和 JQuery 端验证上设置了这个,问题是当有消息集和文件注释集时,php 脚本会给出图像的错误是一个 undefined variable 。

我的脚本正在做的是,即使文件没有上传,脚本也会回显文件,如果我没有上传任何图像并且只设置了文本消息,那么它会回显两者,过去 4 个小时以来我一直被困在这里。我不知道我的脚本是错误的还是逻辑失败的。谁能帮我解释一下这个逻辑?

最佳答案

试试这个。

<?php
if($_GET['id'] == "broadcast")
{
$has_message = (isset($_POST['message']) && (strlen(trim($_POST['message'] )) >0));
$has_file = (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK );

if( !$has_message && $has_file)
{
//file variable is set
$counter = "file";
}
else if( $has_message && !$has_file)
{
//message varibale is set
$counter = "message";
}
else if( $has_message && $has_file)
{
//both are present
$counter = "both";
}
else {
$counter = "neither";
}

echo $counter;
exit();
}

关于javascript - PHP 代码总是设置文件上传标志,即使没有上传任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46692072/

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