0

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
Michael Schmidt
  • 9,090
  • 13
  • 56
  • 80

5 Answers5

2

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
        )
)
Kyle
  • 4,421
  • 22
  • 32
  • And yes, i don't use IE. For IE, i want to fix it later. But it should work in webkit browsers... – Michael Schmidt Nov 28 '12 at 16:04
  • @dTDesign You didn't add any values to your `existAlbum` like I suggested. Even if you don't want them to use the option `Select Album`, you should give it a value of `value=""`. Take a look at my edit on how you should set your variables. – Kyle Nov 28 '12 at 16:05
  • @dTDesign Are the contents of $_POST still empty or is at least one of the variables set? – Kyle Nov 28 '12 at 16:10
  • @dTDesign I just posted the example I am using to test with, and am getting values (just a copy and paste of your code). Do you have any Javascript event that runs before the form is submitted? If so, would you please post that? – Kyle Nov 28 '12 at 16:20
  • Did you just upload 1 image? Because 1 image works. But i want a multiple upload script. When i use more than 1 image, there are the errors – Michael Schmidt Nov 28 '12 at 16:23
  • @dTDesign I didn't upload anything. I just clicked the submit button. I just uploaded 2 png files, and it worked as well. Nothing will show up in `$_POST['uploads']` because that will show up in `$_FILES['upload']` – Kyle Nov 28 '12 at 16:39
  • +1 for your help! Thank you. But It doesn't work... Now i just insert the scripts step by step and try to find out where the error is... – Michael Schmidt Nov 29 '12 at 07:47
  • @dTDesign You are welcome. I am sorry that it still isn't working for you. I hope you figure it out. If you think of anything else that might help solve your problem, update your question. – Kyle Nov 29 '12 at 13:45
0

If select is not picked you will not get it at all (you expect it to be empty, which is not true). You have to check first

$exist_album = isset($_POST['existAlbum']) ? $_POST['existAlbum'] : '<DEFAULT VALUE>';

and same for checkbox.

The newAlbum thing should work as text inputs are always there. See

print_r($_POST);

to see what's really in there, and in my case it is - on "empty" submit I get:

Array
(
    [existAlbum] => SELECT ALBUM
    [newAlbum] => 
)

BTW: you should use <?php rather than <?PHP.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Try if the value existAlbum get set, because it won't return any value if you there is nothing picked. You could give the existAlbum picker a default='1' or something:

 if isset($_POST['existAlbum']){
        echo 'yes';
 }
 else{
      echo 'no';
 }

I think that there is something wrong with the rule enctype="multipart/form-data". Try to just remove this, it should be set automatically by your browser.

0

You have no value for the option select album, even if you don't intend that option to be used give it a value such as 0 so that it will always be set in the POST variables.

<option value="0">SELECT ALBUM</option>
<option value="some album">Some Album</option>
...
RMcLeod
  • 2,561
  • 1
  • 22
  • 38
0

print $_POST Array using print_r($_POST); Make sure your form action is correct

<form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">
Navneet Singh
  • 1,218
  • 11
  • 17