Can't get the $_POST Variable

这一生的挚爱 提交于 2020-01-16 08:38:08

问题


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

回答1:


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



回答2:


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.




回答3:


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



回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/13608845/cant-get-the-post-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!