3 Replies - 299 Views - Last Post: 09 March 2014 - 11:32 PM Rate Topic: -----

#1 ChrisNt  Icon User is offline

  • D.I.C Addict

Reputation: 203
  • View blog
  • Posts: 634
  • Joined: 31-July 13

Form file undefined-index

Posted 09 March 2014 - 04:20 PM

<form id="upload_form" name="upload_form" enctype="multipart/form-data" method="post" action="#">
                <input type="file" name="file1" id="file"><br>
                <input type="submit" value="submit"  >
                <p id="loaded_n_total"></p>
</form>




PHP


<?php

session_start();
if (!isset($_SESSION['user_id'])) {
    header("location: ../index.php");
}

include_once("C:/xampp/htdocs/PhpProject1/PHP/connect.php");



if (isset($_FILES["file1"]) && !empty($_FILES["file1"])) {

    $fileName = $_FILES["file1"]["name"]; // The file name
    $fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
    $fileType = $_FILES["file1"]["type"]; // The type of file it is
    $fileSize = $_FILES["file1"]["size"]; // File size in bytes
    $fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true

    if (!$fileTmpLoc) { // if file not chosen
        $json['status'] = "file not choosen";
        echo json_encode($json);
        exit();
    }

    $userId = $_SESSION['user_id'];
    $path = 'id' . $userId;


    // create file with users id
    if (!is_dir($path)) {
        mkdir($path);
    }

    if (move_uploaded_file($fileTmpLoc, "$path/" . $_FILES['file1']['name'])) {

        $location = "$path/" . $_FILES['file1']['name'];


        $query = "INSERT INTO video(name,location) VALUES ('$filename','$location')";


        if (!mysqli_query($connection, $query)) {
            $json['status'] = "true";
            echo json_encode($json);
        }
        } 
        else {
        $json['status'] = "false";
        echo json_encode($json);
    }
} else {
    $json['status'] = "file not isset";
    echo json_encode($json);
}
?>




JS



    $('#upload_form').on('submit', function(e) {

        $.ajax({
            url: 'file_upload_parser.php',
            data: $(this).serialize(),
            type: 'POST',
            dataType: "json",
            success: function(data) {

                console.log(data);


                if (data.status === "false") {

                    $("#loaded_n_total").html("fail");
                }
                if (data.status === "true") {
                    $("#loaded_n_total").html("Success");
                }
                if (data.status === "not isset") {
                    $("#loaded_n_total").html("data not isset");

                }
            },
            error: function(data) {
                $("#loaded_n_total").html("please try again later"); //===Show Error Message====
                console.log(data);

            }
        });
        e.preventDefault();
    });





I Always get error message from php via json which means "undefined-index for file1"


**updated code

This post has been edited by ChrisNt: 09 March 2014 - 07:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Form file undefined-index

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 620
  • View blog
  • Posts: 2,780
  • Joined: 03-December 12

Re: Form file undefined-index

Posted 09 March 2014 - 07:22 PM

Try this statment instead,


 !isset($_FILES['file1']['error']) 

Was This Post Helpful? 1
  • +
  • -

#3 ChrisNt  Icon User is offline

  • D.I.C Addict

Reputation: 203
  • View blog
  • Posts: 634
  • Joined: 31-July 13

Re: Form file undefined-index

Posted 09 March 2014 - 08:00 PM

Tried now i get the error message from error function "please try again later".I guess is something wrong with ajax..
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 620
  • View blog
  • Posts: 2,780
  • Joined: 03-December 12

Re: Form file undefined-index

Posted 09 March 2014 - 11:32 PM

What error message are you getting from the Ajax call?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1