【问题标题】:alter $_POST data of radio select option更改无线电选择选项的 $_POST 数据
【发布时间】:2011-07-27 23:14:32
【问题描述】:

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

<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(注意我有多个 Radio group 问题)

Array
(
    [radio-choice-1] => choice-1
    [radio-choice-2] => choice-4
    [radio-choice-3] => choice-2
    [submit] => submit
    [PHPSESSID] => 11111111111111111
)

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

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 作为一个选项?

【问题讨论】:

  • 为什么要在提交之前而不是之后进行重组?最好在服务器端执行此操作。为什么你使用choice-X 作为值而不仅仅是数值?

标签: php jquery html post radio-group


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我假设您在谈论您正在提交的表单?

    如果您想更好地控制发布的内容,您可以执行以下两项操作之一:

    1) 添加隐藏变量

    或

    2) 使用 jQuery .post() (http://api.jquery.com/jQuery.post/) 而不是进行正常的表单提交。

    我个人认为两者中的第一个最简单:

    <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 并添加一个隐藏变量,这应该满足您的要求(对您的其他无线电元素执行相同的操作)。

    【讨论】:

      【解决方案3】:

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

      $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
      

      【讨论】:

      • 如果他想在服务器端这样做,那也可以。 +1
      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      相关资源
      最近更新 更多