我希望复选框有多个值。我怎么做? 它只添加了第一个选择的选项
<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
有两个PHP文件。我将在下面发布。我不熟悉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;
}
第二档
<?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));
}
}
感谢您的帮助!
答案 0 :(得分:5)
您的复选框名称和值已混淆。它应该是:
<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
现在你可以像这样操纵$_POST['fields']['options']
:
$options = (is_array($_POST['fields']['options'])) ? $_POST['fields']['options'] : array();
然后你可以内爆他们:
$options_string = implode(',', $options) // will return option1,option2,option3 etc
或循环播放:
foreach ($options as $option)
{
echo $option.'<br>';
}
// produces option1<br>option2<br>option3 etc
答案 1 :(得分:1)
不要在数组中为名称指定密钥 - 应将其设置为name =“fields []”。