I has transfer my file from xampp to wampp and i get this error:
Notice: Undefined index: action in C:\wamp64\www\5S\ajax\video_handler.php on line 6
I not sure what is happening as this code run normally at xampp…
video.php
<h2>Video Upload</h2>
<form id="videoForm" action="ajax/video_handler.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="video">Please Choose Video To Upload:</label>
<input type="file" name="video" id="video" accept="video/*" class="form-control-file" required>
<input type="hidden" name="action" id="action" value="add">
</div>
<input type="hidden" name="video_id" id="video_id" value=""> <!-- Add a hidden input for video ID (for editing) -->
<button type="submit" name="submit" id="save" class="btn btn-primary">Upload Video</button>
</form>
<script type="text/javascript">
$(".delete_item").click(function() {
var id = $(this).attr('data-id');
var parent = $(this).closest('tr');
// Display a confirmation dialog
var confirmDelete = confirm("Are you sure you want to delete this video?");
if (confirmDelete) {
// User confirmed the deletion, proceed with the AJAX request
// Handle the delete action using AJAX
$.ajax({
type: 'POST',
url: 'ajax/video_handler.php',
dataType: 'json',
data: 'action=delete&id=' + id,
success: function(data) {
if (data.success) {
// Reload the page or perform any other necessary action
location.reload();
} else {
// Handle error if needed
alert("Error: " + data.error);
}
},
beforeSend: function() {
// Display a loading message or spinner if desired
}
});
}
// Handle the "Add" action
$("#save").click(function(e) {
e.preventDefault(); // Prevent the default form submission
// Set the "action" hidden input value to "add"
$("#action").val("add");
// Submit the form
$("#videoForm").submit();
});
});
the following is video_handler.php
if ($login) {
$action = $_POST['action'];
error_log("Received action: " . $action); // Log to the server error log
echo "Received action: " . $action; // Send to the response (for testing)
if ($action == 'add') {
//$title = mysqli_real_escape_string($conn, $_POST['title']);
if (isset($_FILES["video"])) {
$video_directory = '/uploads/general_update_video/'; // Update with the correct absolute path
$video_filename = basename($_FILES["video"]["name"]);
$video_full_path = $video_directory . $video_filename;
if (move_uploaded_file($_FILES["video"]["tmp_name"], $video_full_path)) {
// Construct the URL based on your web server's configuration
$video_url = 'http://10.16.162.58/5S/uploads/general_update_video/' . urlencode($video_filename);
// Use getID3 to retrieve the video duration
require_once('../getID3-master/getid3/getid3.php'); // Update with the correct path
$getID3 = new getID3;
$fileInfo = $getID3->analyze($video_full_path);
$duration = isset($fileInfo['playtime_seconds']) ? $fileInfo['playtime_seconds'] : 0;
// Insert video information into the database using prepared statements
$sql = "INSERT INTO videos (title, description, video_path, upload_time)
VALUES (?, ?, ?, NOW())";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "sss", $title, $duration, $video_filename);
if (mysqli_stmt_execute($stmt)) {
// Handle success, e.g., redirect to a success page
// Handle success by displaying a JavaScript pop-up alert
echo '<script>alert("Success to upload video file."); window.location = "/5S/video.php";</script>';
exit();
} else {
// Handle failure, e.g., display an error message
echo "Error: " . mysqli_error($conn);
}
mysqli_stmt_close($stmt);
} else {
// Handle failure, e.g., display an error message
echo "Failed to upload video file.";
}
} else {
// Handle failure, e.g., display an error message
echo "No video file was uploaded.";
}
} elseif ($action === 'delete') {
// Handle video deletion in the database
$videoId = $_POST['id'];
// Delete the video from the database
$sql = "DELETE FROM videos WHERE id = ?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "i", $videoId);
if (mysqli_stmt_execute($stmt)) {
// Send a success JSON response
echo json_encode(["success" => true]);
exit();
} else {
// Send an error JSON response
echo json_encode(["success" => false, "error" => mysqli_error($conn)]);
}
mysqli_stmt_close($stmt);
} else {
// Handle other actions or invalid action
echo json_encode(["success" => false, "error" => "Invalid action"]);
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
} else {
// Handle unauthorized access
echo json_encode(["success" => false, "error" => "Unauthorized access"]);
}
?>
this error appear when I try to upload video by clicking the “upload video” button…