Can't get the $_POST Variable

Michael Schmidt picture Michael Schmidt · Nov 28, 2012 · Viewed 12k times · Source

It should be a multiple upload form for pictures

I get the HTML Code for a Upload-Form:

<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">  
        <label id="filelabel" for="fileselect">Choose the Pictures</label>
        <input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
        <span class="text">Exist Album</span><br />
        <select id="existAlbum" name="existAlbum" size="1">
            <option value="noAlbum">SELECT ALBUM</option>       
        </select>
        <span class="text">OR</span>
        <span class="text">New Album</span><br />
        <input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
        <input type="submit">
    </form> 

The form link to the uploaded.php. But there i get:

Notice: Undefined index: existAlbum in E:\xampp\htdocs\fotokurs\upload\upload.php on line 11

Notice: Undefined index: newAlbum in E:\xampp\htdocs\fotokurs\upload\upload.php on line 12

Here's the upload.php:

<?PHP  
$allowedExtensions = array('png', 'jpg', 'jpeg'); 

$maxSize = 20971520;  

$i = 0;  

$first = 0;

$exist_album = $_POST['existAlbum']; 
$new_album = $_POST['newAlbum'];

Where is my fault? I can't find it...

EDIT Add following to my code:

if( isset( $_POST['existAlbum'] ) or isset( $_POST['newAlbum'] ) ){
    $exist_album = $_POST['existAlbum']; 
    $new_album = $_POST['newAlbum'];
}else{
    echo 'no album <br />';
}
print_r($_POST);

new output:

no album 
Array ( ) 
Notice: Undefined variable: new_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 20

Notice: Undefined variable: exist_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 21

Notice: Undefined variable: new_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 22

Notice: Undefined variable: exist_album in E:\xampp\htdocs\fotokurs\upload\upload.php on line 23

Answer

Kyle picture Kyle · Nov 28, 2012

One of your issues is that existAlbum has no actual values associated with it.

You have <option>Select Album</option> which has no value associated with the option element. If there is no value associated, the select element is not posted to the server. You should change it to be:

<option value="">Select Album</option>

EDIT

Since the user only has to supply one or the other, you should use the following to set your variables:

$existsAlbum = (isset($_POST['existAlbum']) && !empty($_POST['existAlbum'])) ? $_POST['existAlbum'] : 'defaultValue';
$newAlbum = (isset($_POST['newAlbum']) && !empty($_POST['newAlbum'])) ? $_POST['newAlbum'] : 'defaultValue';

One important thing to note is that Internet Explorer does not support the placeholder attribute.

EDIT 2

Here is my quick test page that worked test.php:

  <form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">  
    <label id="filelabel" for="fileselect">Choose the Pictures</label>
    <input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
    <span class="text">Exist Album</span><br />
    <select id="existAlbum" name="existAlbum" size="1">
      <option value="noAlbum">SELECT ALBUM</option>       
    </select>
    <span class="text">OR</span>
    <span class="text">New Album</span><br />
    <input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
    <input type="submit" value="Submit">
  </form> 

upload.php

    <pre>
<?php print_r($_POST); ?>
<?php print_r($_FILES); ?>
    </pre>

results

Array
(
    [existAlbum] => noAlbum
    [newAlbum] => 
)
Array
(
    [uploads] => Array
        (
            //Contents here
        )
)