Notice: Undefined index: action

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…


<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">
                                    <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>

<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
                            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
                                } 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"

                        // Submit the form


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 = '' . 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>';
                } else {
                    // Handle failure, e.g., display an error message
                    echo "Error: " . mysqli_error($conn);

            } 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]);
        } else {
            // Send an error JSON response
            echo json_encode(["success" => false, "error" => mysqli_error($conn)]);

    } else {
        // Handle other actions or invalid action
        echo json_encode(["success" => false, "error" => "Invalid action"]);
        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…

What is in $_POST when you display it for debugging?

If the total size of the form data exceeds the post_max_size setting on the server, both the $_POST and $_FILES arrays will be empty. This is because the web server compares the content-length header in the request with the post_max_size setting and aborts the request if the content-length is too large.

Assuming you have a valid post method form, with at least one named post field and/or at least one named file field (and uploads are enabled on the web server), you can detect this condition by testing if both $_POST and $_FILES are empty. You can also detect this condition by testing the $_SERVER[‘CONTENT_LENGTH’] (which is in bytes) against the post_max_size setting (which can be in K/k, M/m, or G/g notation and needs to be converted to bytes for the comparison.)

Your post method form processing code should first test if a post method form has been submitted, by testing if($_SERVER['REQUEST_METHOD'] === 'POST'). It should then test for the above condition (using either method), setup a message for the user if the size of the form data is too large, and only use the form data if it actually exists.

Since you are handling a file upload, after you have determined that there is data in $_FILES, you need to test the [‘error’] element and only use the uploaded file data if there is no upload error. There’s a list of the error values in the documentation.