getting $_POST variables from form in a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jenkins

    getting $_POST variables from form in a function

    I've loaded various thoughts I've collected into mysql. I want to write an
    app, all in one file, that has the following features:

    1. On initial entry, selects and displays the thoughts ( might add
    pagination later - there aren't alot of records, for now.)
    2. This main page includes separate forms near the top with submit buttons
    to search, add a thought, view all thoughts, etc. The action for each form
    is to call this same page. The submit buttons and thoughts are printed from
    a function.
    3. In the main logic, I am trying to do a $_POST and if isset() on each
    submit button name and then call a corresponding function, do whatever it
    has to do, unset the form variable and continue back to the main display.

    The problem is that it's not calling any functions. The error log says that
    the name of each submit button in my $_POST[] statements in the main logic
    is an undefined index. I presume that they are undefined in the main logic
    because the forms were defined in a function.

    Is there anyway to access, in the same page, form variables that were
    declared in a function on that page?

    Thanks!


  • Tom Thackrey

    #2
    Re: getting $_POST variables from form in a function


    On 26-Oct-2003, "Jenkins" <news@djenkins. nu> wrote:
    [color=blue]
    > I've loaded various thoughts I've collected into mysql. I want to write an
    > app, all in one file, that has the following features:
    >
    > 1. On initial entry, selects and displays the thoughts ( might add
    > pagination later - there aren't alot of records, for now.)
    > 2. This main page includes separate forms near the top with submit buttons
    > to search, add a thought, view all thoughts, etc. The action for each form
    > is to call this same page. The submit buttons and thoughts are printed
    > from
    > a function.
    > 3. In the main logic, I am trying to do a $_POST and if isset() on each
    > submit button name and then call a corresponding function, do whatever it
    > has to do, unset the form variable and continue back to the main display.
    >
    > The problem is that it's not calling any functions. The error log says
    > that
    > the name of each submit button in my $_POST[] statements in the main
    > logic
    > is an undefined index. I presume that they are undefined in the main logic
    > because the forms were defined in a function.
    >
    > Is there anyway to access, in the same page, form variables that were
    > declared in a function on that page?[/color]

    Barring typos I think this illustrates what you want to do:

    myscript.php:
    <?php

    if (isset($_POST['submit'))
    {
    $whichbutton = $_POST['submit'];
    $somefieldvalue = $_POST['somefield'];
    if ($whichbutton == 'Red')
    // do red things
    if ($whichbutton == 'Blue')
    // do blue things
    }
    else
    {
    $whichbutton = 'None';
    $somefieldvalue = 'Starting value';
    }
    ?>
    <HTML> ....

    <p>The last button pushed was <? echo $whichbutton; ?></p>
    <form action="myscrip t.php" method=post>
    <input type=text name=somefield value="<? echo $somefieldvalue ; ?>">
    ....
    <input type=submit name=submit value="Red">
    <input type=submit name=submit value="Blue">
    </form>
    ....

    --
    Tom Thackrey

    tom (at) creative (dash) light (dot) com
    do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

    Comment

    Working...