这是我的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脚本给出并且图像错误是一个未定义的变量。
我的脚本正在做的是即使文件没有上传脚本echo file
,如果我没有上传任何图像并且只设置了文本消息,那么它回显both
,我卡住了这里过去4个小时。我不知道我的脚本是错误的还是逻辑失败了。任何人都可以帮我解释这个逻辑吗?
答案 0 :(得分:1)
试试这个。
<?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();
}
答案 1 :(得分:0)
isset($_FILES)
始终为true
empty($_FILES)
为true
,否则为false