SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A function I dont understand !

    Hi, please excuse me but I'm new to php and I have been trying to teach myself by reading lots and looking at other free scripts.

    I found this function which looked like it might be handy some day but I dont know how to get the value out after it has been submitted.

    (Please excuse any errors in my code that I added):

    <?php

    $_POST['current'];
    $current = $_POST['current'];
    $_POST['submit'];
    $submit = $_POST['submit'];
    if(!$submit)
    {
    ?>
    <form action="<?php echo ($_SERVER["PHP_SELF"]); ?>" method="post">
    <?php
    function makeSelectArray($data, $name, $current = '')

    {
    $output = '<select name="'. $name .'">';

    foreach ($data as $datum) {
    $selected = ($current == $datum)?"selected " : "";
    $output .= "\n<option $selected value=\"$datum\">$datum</option>";
    }

    $output .= "\n</select>";
    return $output;
    }

    //usage:
    $position = array('programmer', 'web developer', 'other');


    echo makeSelectArray($position,'job',$job);

    ?>

    <p>

    <input type="submit" value="SUBMIT" name="submit">
    </form>

    <?php
    }
    ?>

    <?php
    if($submit)
    {
    echo "$current was selected";
    }
    ?>

    Thanking you, Jamie.

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you referring to this function?:

    Code:
    function makeSelectArray($data, $name, $current = '')
    {
        $output = '<select name="'. $name .'">';
    
        foreach ($data as $datum) 
        {
            $selected = ($current == $datum)?"selected " : "";
            $output .= "\n<option $selected value=\"$datum\">$datum</option>";
        }
        $output .= "\n</select>";
        return $output;
    }
    If so its pretty simple, you create an array contening the items you wish to place on your select menu like so:

    Code:
    $position = array('programmer', 'web developer', 'other');
    Then to generate the output you simply do the following:

    Code:
    echo makeSelectArray($position,'job', 'web developer');
    The second parameter is the form name. The third parameter passed to the script determines which menu element is selected by default, in this case 'web developer'.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  3. #3
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your time, but I have not made myself clear obviously.

    I am looking to find how I get the information that was selected **AFTER** it has been submitted, return result.

    The script works as it is, but I want it to print 'whatever' was selected.

    Thanking you again, Jamie.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If we continue on phil's example, then after submitting the form the selected 'item' is contained in $_POST['job']. So just do
    PHP Code:
    echo $_POST['job']; 
    The entire code
    PHP Code:
    <?php
    function makeSelectArray($data$name$current '')
    {
        
    $output '<select name="'$name .'">';

        foreach (
    $data as $datum) {
            
    $selected = ($current == $datum)?"selected " "";
            
    $output .= "\n<option $selected value=\"$datum\">$datum</option>";
           }

        
    $output .= "\n</select>";
        return 
    $output;
    }

    if(!
    $_POST['submit'])
    {
    ?>
        <form action="<?php echo ($_SERVER["PHP_SELF"]); ?>" method="post">
        <?php
        $job 
    'web developer';
        
    $position = array('programmer''web developer''other');
        echo 
    makeSelectArray($position,'job',$job);
        
    ?>
        <p>
        <input type="submit" value="SUBMIT" name="submit">
        </form>

    <?php
    } else {
        echo 
    "Selected job:" $_POST['job'];
    }
    ?>
    HTH

    -Helge

  5. #5
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou very much for that.

    I'm just finding these things a little bit harder than I thought they would be to understand.

    Thanking you, Jamie.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •