Я пытаюсь извлечь данные JSON с помощью php и проанализировать их с помощью AJAX, чтобы не загружалась моя веб-страница и продолжался весь процесс.
Как у меня есть два массива.
$errorMessage[];
and
$uploadedFiles[];
если возникает какая-либо ошибка, поле Div в HTML должно отображать значения $errorMessage
и если мой файл загружен, в поле div должно отображаться значение $uploadedFiles
.
прямо сейчас, независимо от того, что условие ... оно показывает оба значения массива.
Как применить условие в AJAX, чтобы оно показывало только одно значение массива.
Вот копия кода PHP.
if ($uploadOk == 0) {
$errorMessage['error_file'] = "Sorry, your file was not uploaded.";
$errorMessage['errormessage'] = "Error in input Image";
echo json_encode($errorMessage);
// if everything is ok, try to upload file
}
else
{
if(move_uploaded_file($sourcePath,$targetPath)) {
// copy($targetPath, $outputImage);
$uploadedFiles['input_file'] = '<img class="image-preview" src="'.$targetPath.'" class="upload-preview" />';
$uploadedFiles['path'] = $targetPath ;
// $uploadedFiles['output_file'] = '<img style="filter: grayscale() !important;" class="image-preview" src="'.$outputImage.'" class="upload-preview" />';
echo json_encode($uploadedFiles);
session_start() ;
$_SESSION['file_path'] = $targetPath;
}
}
и вот мой AJAX:
<script type="text/javascript">
$(document).ready(function (e) {
$("#uploadForm").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
dataType: "json",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$("#targetLayer").html(data.error_file);
$("#targetLayer").html(data.input_file);
$(".outputDiv").html(data.errormessage);
$(document).ready(function(){$(".outputDiv").load("python.php");});
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}));
});
</script>
Надеюсь, мой вопрос ясен. Заранее спасибо