Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:01:58+00:00 2026-06-13T15:01:58+00:00

I am having problem with my cancel button when trying to cancel my upload

  • 0

I am having problem with my cancel button when trying to cancel my upload and I needs some help in probably changing the design of my code a little.

The problem:

The "cancelaudio.php" page does not have the information it needs until after the "audioupload.php" script has been run. It’s a cart and horse situation. If the client clicks the cancel button during the HTTP request, the "audioupload.php" script (on the server side) never gets executed. However the client-side activities in jQuery would still get run.

The solution I want to acheive:

Client fills in the form and submits it, resulting an a POST request, accompanied by the file. The POST request may take several seconds to complete, depending on the size of the file, speed of the connections, etc.

Only after the HTTP upload has completed for all of the files, will PHP gain control. Your PHP "action" script on the server gets control via a POST-method request. If any errors occurred during the upload, $_FILES[‘error’] will be loaded with the right code. At this point you can check for values in $_FILES, move_uploaded_file(), load the file name into the $_SESSION array, etc.

So to sum up, if the human client clicks a "cancel button" while the POST request is in process (or before starting the upload), and this causes cancellation of the file upload, the PHP "action" script that handles the uploads never gets control. The server never has an opportunity to move the uploaded file and load the variables into the database or session array.

I just need help on coding the problem to be able to reach the solution. Can anybody help? Below are the necessary code:

AUDIOUPLOAD.PHP

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
// connect to the database
include('connect.php');

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}

$result = 0;

if( file_exists("AudioFiles/".$_FILES['fileAudio']['name'])) {
$parts = explode(".",$_FILES['fileAudio']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;

while( file_exists("AudioFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileAudio']['name'] = $base."_".$n.".".$ext;

move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
"AudioFiles/" . $_FILES["fileAudio"]["name"]);
$result = 1;

}
else
{
move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
"AudioFiles/" . $_FILES["fileAudio"]["name"]);
$result = 1;
}

$audiosql = "INSERT INTO Audio (AudioFile) 
VALUES (?)";

if (!$insert = $mysqli->prepare($audiosql)) {
// Handle errors with prepare operation here
}

//Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$aud);

//Assign the variable
$aud = 'AudioFiles/'.$_FILES['fileAudio']['name'];

$insert->execute();

if ($insert->errno) {
// Handle query error here
}

$insert->close();

$lastAudioID = $mysqli->insert_id;   

$_SESSION['lastAudioID'] = $lastAudioID; 
$_SESSION['AudioFile'] = $_FILES["fileAudio"]["name"]; 

$audioquestionsql = "INSERT INTO Audio_Question (AudioId, QuestionId)  
VALUES (?, ?)"; 

if (!$insertaudioquestion = $mysqli->prepare($audioquestionsql)) { 
// Handle errors with prepare operation here 
echo "Prepare statement err audioquestion"; 
} 

$qnum = (int)$_POST['numaudio'];

$insertaudioquestion->bind_param("ii",$lastAudioID, $qnum); 

$insertaudioquestion->execute(); 

if ($insertaudioquestion->errno) { 
// Handle query error here 
} 

$insertaudioquestion->close(); 



?>

CANCELAUDIO.PHP

<?php

 // connect to the database
 include('connect.php');

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

  unlink("AudioFiles/" . $_SESSION['AudioFile']);  
 
$delete = $mysqli->prepare('DELETE FROM Audio WHERE AudioId = ?'); 
$delete->bind_param("i",$_SESSION['lastAudioID']); 
$delete->execute(); 

$deleteaud = $mysqli->prepare('DELETE FROM Audio_Question WHERE AudioId = ?'); 
$deleteaud->bind_param("i",$_SESSION['lastAudioID']); 
$deleteaud->execute(); 
   
  
?>

HTML FORM CODE:

<form action='audioupload.php' method='post' enctype='multipart/form-data' target='upload_target_audio' onsubmit='return audioClickHandler(this);' class='audiouploadform' > 
    
Audio File: <input name='fileAudio' type='file' class='fileAudio' /></label><br/><br/><label class='audiolbl'> 
    
<input type='submit' name='submitAudioBtn' class='sbtnaudio' value='Upload' /></label>
    
<input type='hidden' class='numaudio' name='numaudio' value='" + GetFormAudioCount() + "' />
    
<input type='reset' name='audioCancel' class='audioCancel' value='Cancel' /></label>
    
<iframe class='upload_target_audio' name='upload_target_audio' src='#' style='width:300px;height:300px;border:0px;solid;#fff;'></iframe></form>

JQUERY CODE:

function startAudioUpload(audiouploadform){
  $(audiouploadform).find('.audiof1_upload_process').css('visibility','visible');
  $(audiouploadform).find('.audiof1_cancel').css('visibility','visible');
  $(audiouploadform).find('.audiof1_upload_form').css('visibility','hidden');
  sourceAudioForm = audiouploadform;
  
          $(audiouploadform).find(".audioCancel").on("click", function(event) {
              $('.upload_target_audio').get(0).contentwindow
              $("iframe[name='upload_target_audio']").attr("src", "cancelaudio.php");
          return stopAudioUpload(2);
    });
  
      return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:01:59+00:00Added an answer on June 13, 2026 at 3:01 pm

    Ok I’ll try something from what I understood.

    The user submits a file using the form. This could take some time, so when the user clicks on “Cancel”, 2 cases are possible:

    1. The form (and its data, i.e. the file content) is still being sent out by the browser and is not finished uploading. In that case, all you have to do is cancel the form submission. Ask the browser to stop it.
      That way, the audioupload.php will never execute, and you won’t need to remove it by calling cancelaudio.php.

    2. When the browser completed the file upload / form submission, there’s a short time frame where the server is still processing the data and has not responded to you yet. In that case, the uploaded file may or may not be saved (we don”t know). You need to call your cancelaudio.php page to delete it from the hard drive.

    Only one of the 2 cases will be true, but you can do both actions since none will conflict with the other one.

    The answer to first case is here (since you’re using an <iframe> to submit the form) : Is it possible to cancel file upload that uses hidden iframe?

    The anwser to second case is to always make an Ajax call to cancelaudio.php, just in case the file has been processed and saved but we haven’t been notified (well, the time frame is short, but it still can happen).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi having a problem with this code even if i clicked on cancel it
I'm having some problem handling the JS onClick event and Dojo. Waht i'm trying
I'm having problem with datagrid view. I have attached an image with the code
I am having problem with drawing multiple lines during repaint. The code is as
I am having to code up a button which, onclick, downloads a csv file
I'm having a problem stopping the 'feed'; the cancel argument doesn't seem to have
I'm having a problem with a4j:commandButton. What I'm trying to accomplish is this: I
I am trying to do some inline editing using jquery. The problem i am
I'm having a problem here. I've modified a portion of the source code to
I'm having a bit of a problem finding out how to cancel this task

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.