Jump to content

The error is still appearing, why?


mahenda
 Share

Recommended Posts

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

 

 

Link to comment
Share on other sites

///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);

?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by phppup
Typos
Link to comment
Share on other sites

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();
    });

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
    });

 

Link to comment
Share on other sites

  • 3 weeks later...

 

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*/
  

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.