這是我的HTML代碼:PHP代碼總是設置文件上傳標誌設置,即使沒有被上傳
<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腳本給出和圖像的錯誤是一個未定義的變量。
什麼我的劇本是做即使文件不被上傳腳本回聲file
,如果我沒有uploadeda紐約圖像和純文本郵件設置,那麼它呼應both
,因爲過去40小時內,我就在這裏停留的。我不知道我的劇本是錯誤的還是邏輯失敗。任何人都可以用這個邏輯幫助我嗎?
'print_r($ _ FILES);'輸出什麼? – bassxzero
什麼是$(「#image」).val()等於您發送AJAX請求之前?爲什麼你圖像輸入命名的圖像[]? –
@i-man'image []'用於創建一個名爲image的文件數組。 – bassxzero