mahenda Posted April 20, 2021 Share Posted April 20, 2021 i have field like this <form method="POST" enctype="multipart/form-data"> <input type="file" name="photo" id="photo"> <button type="button" id="uploaad">upload</button> </form> <pre> <?php //my validation code $errors=[]; $data=[]; if(empty($_FILES["photo"]["name"])) { $errors['emptyphoto'] = 'please upload photo!'; } if(count($errors) !== 0) { $data['errors'] = $errors; } echo json_encode($data); ?> </pre> The Error appearing not at first, but when I submit an empty field the error appear, again even if image is selected the error still appearing why not disappearing Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/ Share on other sites More sharing options...
gw1500se Posted April 20, 2021 Share Posted April 20, 2021 First please use the code icon (<>) in the menu for your code and specify PHP. Your question is incomplete. What error? Which line of code? Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1585997 Share on other sites More sharing options...
mahenda Posted April 20, 2021 Author Share Posted April 20, 2021 ///i have field like this <form method="POST" enctype="multipart/form-data"> <input type="file" name="photo" id="photo"> <button type="button" id="uploaad">upload</button> </form> //my php code <?php //validation code $errors=[]; $data=[]; //The Error not appearing at first, but when I submit an empty field the error appear that is good, the problem start here, even if image is selected the error still appearing why not disappearing if(empty($_FILES["photo"]["name"])) { $errors['emptyphoto'] = 'please upload photo!'; } if(count($errors) !== 0) { $data['errors'] = $errors; } echo json_encode($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586005 Share on other sites More sharing options...
mahenda Posted April 20, 2021 Author Share Posted April 20, 2021 5 hours ago, gw1500se said: First please use the code icon (<>) in the menu for your code and specify PHP. Your question is incomplete. What error? Which line of code? edited Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586006 Share on other sites More sharing options...
Barand Posted April 20, 2021 Share Posted April 20, 2021 Does $_FILES['photo']['error'] give any clues? Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586007 Share on other sites More sharing options...
mac_gyver Posted April 20, 2021 Share Posted April 20, 2021 1 hour ago, mahenda said: button type="button" since a button of type = 'button' doesn't do anything by itself and you are outputting json encoded data back to the browser, what is the javascript/ajax code that's trying to upload the file? Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586009 Share on other sites More sharing options...
phppup Posted April 20, 2021 Share Posted April 20, 2021 (edited) Quote <button type="button" id="uploaad">upload</button> id = "uploaad" ???? Did you want TWO letter AAs? Easier to fix it now before problems (if it was a typo). Edited April 20, 2021 by phppup Typos Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586010 Share on other sites More sharing options...
mahenda Posted April 21, 2021 Author Share Posted April 21, 2021 22 hours ago, phppup said: id = "uploaad" ???? Did you want TWO letter AAs? Easier to fix it now before problems (if it was a typo). NO TYPO ERROR Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586042 Share on other sites More sharing options...
mahenda Posted April 21, 2021 Author Share Posted April 21, 2021 22 hours ago, mac_gyver said: since a button of type = 'button' doesn't do anything by itself and you are outputting json encoded data back to the browser, what is the javascript/ajax code that's trying to upload the file? MY ajax look like, $("#uploaad").click( function(e) { $.ajax({ url: 'add.php', type: 'POST', data: {photo: $('#photo').val()}, dataType: "JSON", encode: true, }).done( function (data) { if(data.success == false) { if(data.errors.emptyphoto) { $('input').append('<span class="text-danger">' + data.errors.emptyphoto + '</span>'); } } }); e.preventDefault(); }); Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586043 Share on other sites More sharing options...
mahenda Posted April 21, 2021 Author Share Posted April 21, 2021 23 hours ago, Barand said: Does $_FILES['photo']['error'] give any clues? it give the same issue, please refer to my ajax Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586044 Share on other sites More sharing options...
mahenda Posted April 21, 2021 Author Share Posted April 21, 2021 In short, The problem, I 'm facing here is why the error message appear even if i put an image Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586047 Share on other sites More sharing options...
gw1500se Posted April 21, 2021 Share Posted April 21, 2021 Too short. You still haven't said what the error is. We are not mind readers. See my first reply. Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586049 Share on other sites More sharing options...
Barand Posted April 21, 2021 Share Posted April 21, 2021 1 hour ago, mahenda said: data: {photo: $('#photo').val()}, All that line does in your ajax script is send the name of the file. It is NOT uploading the file. https://www.php.net/manual/en/features.file-upload.php Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586053 Share on other sites More sharing options...
mac_gyver Posted April 21, 2021 Share Posted April 21, 2021 in order to upload a file using ajax, you need to use the FormData() object (current, best method) - https://developer.mozilla.org/en-US/docs/Web/API/FormData Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586055 Share on other sites More sharing options...
mahenda Posted April 21, 2021 Author Share Posted April 21, 2021 6 minutes ago, Barand said: All that line does in your ajax script is send the name of the file. It is NOT uploading the file. https://www.php.net/manual/en/features.file-upload.php It actually sends the file name, and the php script above used to check if the file name does not exist then it will return the message in the above array. Surprisingly, when you upload a file it means that the file name is there and that message in the array is still coming back Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586056 Share on other sites More sharing options...
mahenda Posted April 21, 2021 Author Share Posted April 21, 2021 1 hour ago, mac_gyver said: in order to upload a file using ajax, you need to use the FormData() object (current, best method) - https://developer.mozilla.org/en-US/docs/Web/API/FormData I have found that when I use formdata api I do not have to enter dataType: 'JSON', and if I enter, it return undefined. now how do i get those error messages in php script since formdata api does not support JSON look $("#uploaad").click( function(e) { var formData = $(form).serialize(); $.ajax({ url: 'add.php', type: 'POST', data: formData, }).done( function (data) { // this return undrfined if(data.success == false) { if(data.errors.emptyphoto) { $('input').append('<span class="text-danger">' + data.errors.emptyphoto + '</span>'); } } }); e.preventDefault(); }); //and this return an error message as usually even if imagr name is available in the form field $("#uploaad").click( function(e) { var formData = $(form).serialize(); $.ajax({ url: 'add.php', type: 'POST', data: formData, dataType: "JSON", }).done( function (data) { if(data.success == false) { if(data.errors.emptyphoto) { $('input').append('<span class="text-danger">' + data.errors.emptyphoto + '</span>'); } } }); e.preventDefault(); }); Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586059 Share on other sites More sharing options...
mac_gyver Posted April 22, 2021 Share Posted April 22, 2021 11 hours ago, mahenda said: var formData = $(form).serialize(); that's not the FromData object, as was mentioned, and the jquery .serialize() method doesn't include the file data. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586069 Share on other sites More sharing options...
mahenda Posted May 12, 2021 Author Share Posted May 12, 2021 On 4/22/2021 at 12:36 PM, mac_gyver said: that's not the FromData object, as was mentioned, and the jquery .serialize() method doesn't include the file data. //i changed it like this and it works, the submit button look like <button type="submit"> $("form").on('submit', function(e) { $.ajax({ url: 'add.php', type: 'POST', data: new FormData(this), dataType: 'JSON', contentType: false, cache: false, processData:false, }).done( function (data) { // this return undrfined if(data.success == false) { if(data.errors.emptyphoto) { $('input').append('<span class="text-danger">' + data.errors.emptyphoto + '</span>'); } } }); e.preventDefault(); }); //but i have another issue here //my php.ini upload_max_filesize=2M post_max_size=8M //my new photo validation $ext = array('png', 'PNG'); if (in_array(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION), $ext) && ($_FILES['photo']['size'] > 102400)) { $errors['image'] = "image greater than 100kb not allowed!"; } /*But when I upload images larger than 2M they enter the server as usual while those that are larger than 100kb and less than 2M do not enter at the same time they enter those with size less than 100kb. How can I only allow less than 100kb to be uploaded to a server*/ Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586496 Share on other sites More sharing options...
mahenda Posted May 12, 2021 Author Share Posted May 12, 2021 I face this issue, the file that needs to be uploaded is the one that does not exceed 100kb. The strange thing is that when I upload a file larger than 2MB error message not showing. While those below 2MB but grater than 100KB the error are shown, why Quote Link to comment https://forums.phpfreaks.com/topic/312517-the-error-is-still-appearing-why/#findComment-1586512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.