首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改单选选项的$_POST数据

更改单选选项的$_POST数据
EN

Stack Overflow用户
提问于 2011-04-02 04:25:21
回答 3查看 304关注 0票数 0

所以我有一个单选按钮组,如下所示:

代码语言:javascript
复制
<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
        <legend>Choose a pet:</legend>
            <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
            <label for="radio-choice-1">Cat</label>

            <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2"  />
            <label for="radio-choice-2">Dog</label>

            <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3"  />
            <label for="radio-choice-3">Hamster</label>

            <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4"  />
            <label for="radio-choice-4">Lizard</label>
    </fieldset>
</div>

现在,如果我只是提交我得到了这个$_POST (注意,我有多个电台组问题)

代码语言:javascript
复制
Array
(
    [radio-choice-1] => choice-1
    [radio-choice-2] => choice-4
    [radio-choice-3] => choice-2
    [submit] => submit
    [PHPSESSID] => 11111111111111111
)

如何在提交之前重新构造HTML或$_POST数据,使其看起来像这样:

代码语言:javascript
复制
Array
(
    [type-1] => radio-choice-1
    [answer-1] => choice-1
    [type-2] => radio-choice-2
    [answer-2] => choice-4
    [type-3] => radio-choice-3
    [answer-3] => choice-2
    [submit] => submit
    [PHPSESSID] => 11111111111111111
)

也许jQuery可以作为一个选择?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-02 04:38:18

你可以在提交后这样做,如果你保持原来的命名约定,下面的代码应该可以工作。

代码语言:javascript
复制
$postValues = $_POST;
$altered = Array();
$unaltered = Array();

foreach ($postValues as $key => $val) {

  if ( FALSE !== stripos($key, 'radio-choice-') ) {

    $num = explode('-', $key);
    $num = $num[2];
    $altered['type-'.$num] = $key;
    $altered['answer-'.$num] = $value;

  } else {

    $unAltered[$key] = $value;

  }  

}

$manipulatedPOSTData = array_merge($altered, $unAltered);

// Keep doing what you intended
票数 3
EN

Stack Overflow用户

发布于 2011-04-02 04:39:03

我想你说的是你正在提交的表格吧?

如果您想对发布的内容添加更精细的控制,您可以执行以下两项操作之一:

1)添加隐藏变量

2)使用jQuery .post() (http://api.jquery.com/jQuery.post/)代替普通的表单提交。

就我个人而言,我认为第一个是最简单的:

代码语言:javascript
复制
<div data-role="fieldcontain">
    <input type="hidden" name="type-1" value="radio-choice-1" />
    <fieldset data-role="controlgroup">
        <legend>Choose a pet:</legend>
            <input type="radio" name="answer-1" id="radio-choice-1" value="choice-1" checked="checked" />
            <label for="radio-choice-1">Cat</label>

            <input type="radio" name="answer-1" id="radio-choice-2" value="choice-2"  />
            <label for="radio-choice-2">Dog</label>

            <input type="radio" name="answer-1" id="radio-choice-3" value="choice-3"  />
            <label for="radio-choice-3">Hamster</label>

            <input type="radio" name="answer-1" id="radio-choice-4" value="choice-4"  />
            <label for="radio-choice-4">Lizard</label>
    </fieldset>
</div>

通过将无线电接地的名称更改为answer-1并添加一个隐藏变量,这将满足您的要求(对其他无线电元素执行相同的操作)。

票数 1
EN

Stack Overflow用户

发布于 2011-04-02 04:39:45

如果可以,请避免客户端的详细说明。

否则,请使用.submit()并手动编写所需的代码。但这更难。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5518444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档