Tuesday, February 7, 2023
HomeWordPress Developmentphp - Add a number of information through ajax from an HTML...

php – Add a number of information through ajax from an HTML file enter


I am making an attempt to add information from a a number of file add HTML enter through ajax on a WordPress website however I hold getting a 400 error and I am unsure why. My code is predicated on this tutorial.

jQuery:

$(doc).on('change','enter[type="file"]',perform(){

  var fd = new FormData();
  var files_data = $(this); // The <enter sort="file" /> area

  // Loop via every information and create an array file[] containing our information information.
  $.every($(files_data), perform(i, obj) {
      $.every(obj.information,perform(j,file){
          fd.append('information[' + j + ']', file);
      })
  });

  fd.append('motion', 'file_upload');

  fd.append('nonce', $('#file_upload_nonce').val());

  fd.append('application_id', $(this).closest('.application_question').attr('data-application-id'));

  $.ajax({
      sort: 'POST',
      url: '/wp-admin/admin-ajax.php',
      information: fd,
      contentType: false,
      processData: false,
      success: perform(response){
        console.log(response);
      }
  });

});

PHP:

perform file_upload(){
  // Test the nonce first
  if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'file_upload' ) ) {
    echo 'Safety validation failed.';
  } else {

    $application_id = $_POST['application_id'];

    foreach ( $_FILES['files']['name'] as $f => $title ) {

      move_uploaded_file( $_FILES["files"]["tmp_name"][$f], '/wp-content/supporting-evidence/' . $application_id . "https://wordpress.stackexchange.com/" . $_FILES["files"]["name"][$f] );

    }

  }
  wp_die();
}
add_action('wp_ajax_file_upload','file_upload');

What am I doing mistaken right here?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments