2016-08-06 5 views
-4

Ich versuche, den Namen der img-Datei aus HTML-Formular, aber Browser zeigt mir die folgende Fehlermeldung: Hinweis: Undefinierter Index: Bild in C: \ xampp \ htdocs \ login tutorial \ index.php on line 11Kann Image-Dateinamen nicht von POST-Objekt

Notice: Undefined index: Bild in C: \ xampp \ htdocs \ login index.php tutorial \ on line 12

Hier ist meine PHP-Code:

if(isset($_POST['submit'])){ 
     $firstName = $_POST['fname']; 
     $lastName = $_POST['lname']; 
     $email = $_POST['email']; 
     $password = $_POST['password']; 
     $firstName = $_POST['fname']; 
     $passwordConfirm = $_POST['confPassword']; 

     $imageName = $_FILES['image']['name']; 
     $imageSize = $_FILES['image']['size']; 

     echo $firstName . "<br>" . $lastName . "<br>" . $email . "<br>" . $password . "<br>" 
     . $passwordConfirm . "<br>" . $imageName . "<br>" . $imageSize; 
    } 

Hier ist mein HTML:

<form method="post" action="index.php" enctype="multipart/form-data "> 
       <label for="fname">First Name</label><br> 
       <input type="text" name="fname" id="fname"><br> 
       <label for="lname">Last Name</label><br> 
       <input type="text" name="lname" id="lname"><br> 
       <label for="email">Email</label><br> 
       <input type="text" name="email" id="email"><br> 
       <label for="password">Password</label><br> 
       <input type="password" name="password" id="password"><br> 
       <label for="confPassword">Confirm Password</label><br> 
       <input type="password" name="confPassword" id="confPassword"><br> 
       <input type="file" name="image"><br> 
       <input type="submit" name="submit"> 
      </form> 

UPDATE: Es sieht so aus, als hätte ich die Imagedatei nicht bekommen ... Warum?

+0

dies wie folgt vor: http://www.w3schools.com/php/php_file_upload.asp –

Antwort

0

Benutze HTML-Attribut Form enctype="multipart/form-data"

Beispiel: <form action="." method="post" enctype="multipart/form-data">

+0

getan. Funktioniert immer noch nicht. – Drevo

0

Überprüfung Versuchen Sie, wenn es zuerst Set:

if (count($_FILES['image']) > 0) { 
    if (isset($_FILES['image']['name']) { 
     // Stuff 
    } 
} 

EDIT Per Ihre HTML, in jQuery:

$("#foo").submit(function(event){ 

    // Abort any pending request 
    if (request) { 
    request.abort(); 
    } 
    // setup some local variables 
    var $form = $(this); 

    // Let's select and cache all the fields 
    var $inputs = $form.find("input, select, button, textarea"); 

    // Serialize the data in the form 
    var serializedData = $form.serialize(); 

    // Let's disable the inputs for the duration of the Ajax request. 
    // Note: we disable elements AFTER the form data has been serialized. 
    // Disabled form elements will not be serialized. 
    $inputs.prop("disabled", true); 

    // Fire off the request to /form.php 
    request = $.ajax({ 
    url: "/form.php", 
    type: "post", 
    data: serializedData 
    }); 

    // Callback handler that will be called on success 
    request.done(function (response, textStatus, jqXHR){ 
    // Log a message to the console 
    console.log("Hooray, it worked!"); 
    }); 
}); 

T Henne in PHP durch die Bild-Eingabe erhalten:

// for <input name="image"> 
$_FILES["image"]; // array of images, do checks above 

ZWEITE EDIT Serialisierung für Datei-Eingänge offenbar nicht funktioniert. So etwas verwenden this plugin, dann können Sie tun:

var options = { 
    url: "", 
    method: "POST", 
    maxFileSize: 2000000, 
    maxFiles: 5, 
    extFilter: "jpg;png", 
}; 

$(".files").dmpUploader(); 

Beispiel Markup:

<div id="drop-area-div" style="width:400px;height:300px;"> 
    Drag and Drop Files Here<br /> 
    or click to add files using the input<br /> 
    <input type="file" name="files[]" multiple="multiple" title="Click to add Files"> 
</div> 
+0

Es sieht so aus, als hätte ich die Imagedatei nicht bekommen ... Irgendeinen Grund dafür? – Drevo

+0

Wie haben Sie Ihr Formular hochgeladen? jQuery AJAX? Sende dein Formular HTML und ich sollte helfen können –

+0

Habe gerade den HTML-Code hinzugefügt – Drevo