I'm new with this and i don't know how to implement this PHP script into jQuery to submit my form and output the accept and decline message. Can someone help me with this?
PHP SCRIPT:
<?php
if (isset($_POST['file']) && !empty($_POST['file']))
{
$mp3file= new MP3File($_FILES['file']['name']);
$duration = $mp3file->getDuration();
$time_limit = "02:10"; // Time limit
$accepted = "Your music has: ".MP3File::formatTime($duration)." seconds.</br> And is accepted by our terms ";
$declined = "Your music has: ".MP3File::formatTime($duration)." seconds and is not accepted by our terms";
if(MP3File::formatTime($duration) <= $time_limit)
{
$audio=basename($_FILES['file']['name']);
$audio=str_replace(' ','',$audio);
$tmppath1="music/".$audio;
move_uploaded_file($_FILES['file']['tmp_name'],$tmppath1);
$link = mysqli_connect("localhost", "root", "", "");
$save=mysqli_query($link,"INSERT INTO music (location, userid) VALUES ('$tmppath1','1')");
echo $accepted;
} else {
echo $declined;
}
}
?>
HTML FORM:
<form action="" method="POST" enctype="multipart/form-data" id="upload" name="upload">
<div class="bs-example">
<label class="btn-bs-file btn btn-success">SELECT MUSIC</label>
<input id="input-1a" type="file" class="file" name="file" data-provides="file">
<button type="submit" name="submit" class="btn-bs-file btn btn-sm btn-danger" value="SUBMIT MUSIC"></button>
</div>
</form>
Update improved:
JQUERY Script based @Jude Fernandes response:
<script type="text/javascript">
"use strict";
$("#upload").submit(function (e) {
e.preventDefault();
var form_data = new FormData();
form_data.append('file', $('#input-1a').prop('files'));
$.ajax({
url: "upload-music.php",
type: "POST",
data: form_data,
contentType: false,
cache: false,
processData: false,
success: function (data) {
console.log(data);
},
error: function () {
}
});
});
</script>
$("#upload").submit();{success:true, message:'My accepted/declined message'}or however you want to structure the array.echo $acceptedwithreturn $accepted. Same thing withecho $declined