xianate
xianate

Reputation: 17

How to use $_POST

I have the following PHP file:

<?php
    echo <<<_END
    <form method='POST' action='index.php' enctpye='multipart/form-data'>
    New Content: <input type='text' name='content'/><br/>
    New Image: <input type="file" name='image' size='10' /><br/>
    Products:
        <select name="filename">
        <option value="product1.txt">Product 1</option>
        <option value="product2.txt">Product 2</option>
        <option value="product3.txt">Product 3</option>
        </select>
    </form> 
    _END;
    echo <<<_END
    <input type="submit" value="Upload"/>
    _END;

    echo $_POST["filename"];
?>

I am getting this error when i try to run this.

Notice: Undefined index: filename in `C:\xampp\htdocs\index.php` on line 18

Sorry, I am new to PHP but should this not echo either product1.txt, product2.txt or product3.txt?

Upvotes: 0

Views: 108

Answers (3)

Rikesh
Rikesh

Reputation: 26441

You can get post values only after submitting form,

if(isset($_POST["filename"])){
    echo $_POST["filename"];
}

Or better give name submit or something to your submit button & check,

if(isset($_POST["submit"])){
    echo isset($_POST["filename"]) ? $_POST["filename"] : '';
    echo isset($_POST["content"]) ? $_POST["content"] : '';
    .....
}

Helpful: What does this error mean in PHP?

Upvotes: 4

kelunik
kelunik

Reputation: 6928

You have to check if it's set:

if(isset($_POST['filename']))
 echo $_POST['filename'];

Upvotes: 1

PeterFour
PeterFour

Reputation: 329

You have to try first if the variable is set http://php.net/manual/fr/function.isset.php

Upvotes: 0

Related Questions