PHP array

Discussion in 'PHP' started by RAN, Jul 23, 2007.

  1. #1
    Hi,

    How can I pass the checkbox value="1" value="2"... to php script?

    If I have the html like this working ok
    input type="checkbox" name="month[]" value="january" />January

    But I want like this
    input type="checkbox" name="month[]" value="1" />January
    input type="checkbox" name="month[]" value="2" />February
    input type="checkbox" name="month[]" value="3" />March

    Php script
    $action .= (!empty($_POST['month'])? implode("",$_POST['month'])."\n" : '');



    I think I need an array but how?

    I already try this but didt work

    $month=array(
    "1" => $month{January},
    "2" => $month{February},
    "3" => $month{March },
    );
    ?>

    Can you help me please?

    Thank you
     
    RAN, Jul 23, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    input type="checkbox" name="month[1]" value="January" />
    input type="checkbox" name="month[2]" value="February" />
    ....
    
    Code (markup):
    
    foreach ($_POST['month'] AS $key => $month)
    {
        echo "{$key}: {$month}\n";
    }
    
    PHP:
    ??
     
    nico_swd, Jul 23, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    You were on the right track. Use a form like this:
    <form ...
    <input type="checkbox" name="month[]" value="January" /> January
    <input type="checkbox" name="month[]" value="February" /> February
    <input type="checkbox" name="month[]" value="March" /> March 
    ...
    </form>
    Code (markup):
    Then in your PHP script, you can show each value:
    
    foreach ($_POST['month'] as $v) {
        echo $v . "\n";
    }
    
    PHP:
    ... nico beat me :p
     
    krt, Jul 23, 2007 IP
  4. RAN

    RAN Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for you help but

    I dont want to have value="january"...

    input type="checkbox" name="month[]" value="1" />January
    input type="checkbox" name="month[]" value="2" />February
    input type="checkbox" name="month[]" value="3" />March

    I like to transport the value="1" to php script

    And in php have something like that

    $month=array(
    "1" => $month{January},
    "2" => $month{February},
    "3" => $month{March },
    );
    ?>
     
    RAN, Jul 23, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    See my code again. :)

    
    name="month[1]" value="January" />
    
    Code (markup):
    See the 1? That'll be the array key.
     
    nico_swd, Jul 23, 2007 IP
  6. RAN

    RAN Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry I did not explain well

    I dont want to see the months. January, February.... In html file. In html I want to see only numbers not the months.

    The months I like to put in php script. I dont know if is necessary arrays or not.

    I like to transport the value numbers and in php convert like this
    value="1" = January
    value="2" = February

    and so one

    Thank you bery much
    for you help
     
    RAN, Jul 23, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Okay, if I understood you right, this should work:

    
    function getmonth($id)
    {
    	return date('F', mktime(0, 0, 0, intval($id)));
    }
    
    $_POST['month'] = array();
    $_POST['month'][] = 1;
    $_POST['month'][] = 4;
    $_POST['month'][] = 6;
    $_POST['month'][] = 8;
    
    $_POST['month'] = array_map('getmonth', $_POST['month']);
    
    echo '<pre>' . print_r($_POST['month'], true) . '</pre>';
    
    
    PHP:
     
    nico_swd, Jul 23, 2007 IP
  8. Vbot

    Vbot Peon

    Messages:
    107
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $months = array(1 => "Jan",
                    2 => "Feb",
                    3 => "Mar",
                    4 => "Apr",
                    5 => "May",
                    6 => "Jun",
                    7 => "Jul",
                    8 => "Aug",
                    9 => "Sep",
                    10 => "Oct",
                    11 => "Nov",
                    12 => "Dec"
                    );
    if(isset($_POST['month']))
    {
        foreach($_POST['month'] as $key => $value)
        {
            $month[ $value ] = $months[ $value ];
        }
    }
    PHP:
    Is this what you're looking for?
     
    Vbot, Jul 23, 2007 IP
  9. RAN

    RAN Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok sir,

    Thank you very much
     
    RAN, Jul 23, 2007 IP