简体   繁体   中英

How to submit multiple checkbox values?

I want to have multiple values for checkboxes. How do I do that? It only adds the first option that is selected

    <input type="checkbox" name="fields[options]" value="option1[]">option1
<input type="checkbox" name="fields[options]" value="option2[]">option2 
<input type="checkbox" name="fields[options]" value="option3[]">option3

There are two PHP files. I will post them below. I am not that experienced with php.

    /**************** GOOGLE.DOCS method ************************/
if($_SETTINGS['store_to_gdocs'])
{
    include_once("GoogleSpreadsheet/Google_Spreadsheet.php");

    $ss = new Google_Spreadsheet($_SETTINGS['gdocs']['user'],$_SETTINGS['gdocs']['password']);
    $ss->useSpreadsheet($_SETTINGS['gdocs']['spreadsheet_name']);
    $ss->useWorksheet($_SETTINGS['gdocs']['worksheet_name']);

    $row = array 
    (
        "Date" => date("m/d/Y H:i"),
    );

    foreach($_POST['fields'] as $k=>$v)
        $row[$k]=$v;

    if(!$ss->addRow($row))
        $ret['error']=1;
}

Second file

<?php    
class Google_Spreadsheet
{
    private $client;

    private $spreadsheet;
    private $spreadsheet_id;

    private $worksheet = "Sheet1";
    private $worksheet_id;

    function __construct($user,$pass,$ss=FALSE,$ws=FALSE)
    {
        $this->login($user,$pass);
        if ($ss) $this->useSpreadsheet($ss);
        if ($ws) $this->useWorksheet($ws);
    }

    function useSpreadsheet($ss,$ws=FALSE)
    {
        $this->spreadsheet = $ss;
        $this->spreadsheet_id = NULL;
        if ($ws) $this->useWorksheet($ws);
    }

    function useWorksheet($ws)
    {
        $this->worksheet = $ws;
        $this->worksheet_id = NULL;
    }

    function addRow($row)
    {
        if ($this->client instanceof Zend_Gdata_Spreadsheets)
        {
            $ss_id = $this->getSpreadsheetId($this->spreadsheet);

            if (!$ss_id) throw new Exception('Unable to find spreadsheet by name: "' . $this->spreadsheet . '", confirm the name of the spreadsheet');

            $ws_id = $this->getWorksheetId($ss_id,$this->worksheet);

            if (!$ws_id) throw new Exception('Unable to find worksheet by name: "' . $this->worksheet . '", confirm the name of the worksheet');

            $insert_row = array();

            foreach ($row as $k => $v) $insert_row[$this->cleanKey($k)] = $v;

            $entry = $this->client->insertRow($insert_row,$ss_id,$ws_id);

            if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) return TRUE;
        }

        throw new Exception('Unable to add row to the spreadsheet');
    }

    // http://code.google.com/apis/spreadsheets/docs/2.0/reference.html#ListParameters
    function updateRow($row,$search)
    {
        if ($this->client instanceof Zend_Gdata_Spreadsheets AND $search)
        {
            $feed = $this->findRows($search);

            if ($feed->entries)
            {
                foreach($feed->entries as $entry) 
                {
                    if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
                    {
                        $update_row = array();

                        $customRow = $entry->getCustom();
                        foreach ($customRow as $customCol) 
                        {
                            $update_row[$customCol->getColumnName()] = $customCol->getText();
                        }

                        // overwrite with new values
                        foreach ($row as $k => $v) 
                        {
                            $update_row[$this->cleanKey($k)] = $v;
                        }

                        // update row data, then save
                        $entry = $this->client->updateRow($entry,$update_row);
                        if ( ! ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)) return FALSE;
                    }
                }

                return TRUE;
            }
        }

        return FALSE;
    }

    // http://code.google.com/apis/spreadsheets/docs/2.0/reference.html#ListParameters
    function getRows($search=FALSE)
    {
        $rows = array();

        if ($this->client instanceof Zend_Gdata_Spreadsheets)
        {
            $feed = $this->findRows($search);

            if ($feed->entries)
            {
                foreach($feed->entries as $entry) 
                {
                    if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
                    {
                        $row = array();

                        $customRow = $entry->getCustom();
                        foreach ($customRow as $customCol) 
                        {
                            $row[$customCol->getColumnName()] = $customCol->getText();
                        }

                        $rows[] = $row;
                    }
                }
            }
        }

        return $rows;
    }

    // user contribution by dmon (6/10/2009)
    function deleteRow($search)
    {
        if ($this->client instanceof Zend_Gdata_Spreadsheets AND $search)
        {
            $feed = $this->findRows($search);

            if ($feed->entries)
            {
                foreach($feed->entries as $entry)
                {
                    if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
                    {
                        $this->client->deleteRow($entry);

                        if ( ! ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)) return FALSE;
                    }
                }

                return TRUE;
            }
        }

        return FALSE;
    }

    function getColumnNames()
    {
        $query = new Zend_Gdata_Spreadsheets_ListQuery();
        $query->setSpreadsheetKey($this->getSpreadsheetId());
        $query->setWorksheetId($this->getWorksheetId());
        $query->setMaxResults(1);
        $query->setStartIndex(1);

        $feed = $this->client->getListFeed($query);

        $data = array();

        if ($feed->entries)
        {
            foreach($feed->entries as $entry) 
            {
                if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
                {
                    $customRow = $entry->getCustom();

                    foreach ($customRow as $customCol) 
                    {
                        array_push($data,$customCol->getColumnName());
                    }
                }
            }
        }

        return $data;
    }

    private function login($user,$pass)
    {
        // Zend Gdata package required
        // http://framework.zend.com/download/gdata

        require_once 'Zend/Loader.php';
        Zend_Loader::loadClass('Zend_Http_Client');
        Zend_Loader::loadClass('Zend_Gdata');
        Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
        Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');

        $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
        $http = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);
        $this->client = new Zend_Gdata_Spreadsheets($http);

        if ($this->client instanceof Zend_Gdata_Spreadsheets) return TRUE;

        return FALSE;
    }

    private function findRows($search=FALSE)
    {
        $query = new Zend_Gdata_Spreadsheets_ListQuery();
        $query->setSpreadsheetKey($this->getSpreadsheetId());
        $query->setWorksheetId($this->getWorksheetId());

        if ($search) $query->setSpreadsheetQuery($search);

        $feed = $this->client->getListFeed($query);

        return $feed;
    }

    private function getSpreadsheetId($ss=FALSE)
    {
        if ($this->spreadsheet_id) return $this->spreadsheet_id;

        $ss = $ss?$ss:$this->spreadsheet;

        $ss_id = FALSE;

        $feed = $this->client->getSpreadsheetFeed();

        foreach($feed->entries as $entry) 
        {
            if ($entry->title->text == $ss)
            {
                $ss_id = array_pop(explode("/",$entry->id->text));

                $this->spreadsheet_id = $ss_id;

                break;
            }
        }

        return $ss_id;
    }

    private function getWorksheetId($ss_id=FALSE,$ws=FALSE)
    {
        if ($this->worksheet_id) return $this->worksheet_id;

        $ss_id = $ss_id?$ss_id:$this->spreadsheet_id;

        $ws = $ws?$ws:$this->worksheet;

        $wk_id = FALSE;

        if ($ss_id AND $ws)
        {
            $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
            $query->setSpreadsheetKey($ss_id);
            $feed = $this->client->getWorksheetFeed($query);

            foreach($feed->entries as $entry) 
            {
                if ($entry->title->text == $ws)
                {
                    $wk_id = array_pop(explode("/",$entry->id->text));

                    $this->worksheet_id = $wk_id;

                    break;
                }
            }
        }

        return $wk_id;
    }

    function cleanKey($k)
    {
        return strtolower(preg_replace('/[^A-Za-z0-9\-\.]+/','',$k));
    }
}

Thank you for helping!

You have your checkbox names and values mixed up. It should be:

<input type="checkbox" name="fields[options][]" value="option1">option1
<input type="checkbox" name="fields[options][]" value="option2">option2 
<input type="checkbox" name="fields[options][]" value="option3">option3

Now you can manipulate $_POST['fields']['options'] like so:

$options = (is_array($_POST['fields']['options'])) ? $_POST['fields']['options'] : array();

Then you can implode them:

$options_string = implode(',', $options) // will return option1,option2,option3 etc

or loop through them:

foreach ($options as $option)
{
  echo $option.'<br>';
}

// produces option1<br>option2<br>option3 etc

Don't give the name a key in the array - it should be set to name="fields[]".

See this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM