Muhammad Faisal
Muhammad Faisal

Reputation: 906

How to retrieve values from 2 different input forms with the dynamically same named array PHP

I have 2 types of input wrapped in a loop, the first one has a Radio button type, the other one Textarea.

For radio buttons, I give the same dynamic name to group options with one another with an array type (for example: name = 'arr[$idgabung]').

And for Textarea I also give the name name = 'arr[$idgabung]'

$idgabung contains unique id, example: U1Q1, So I can distinguish each radio button based on the value of each after the loop.

Sample code:

<div class = "row">
      <div class = "col-lg-12 col-xs-12">
          <div class="custom-controls-stacked">
              <div class="custom-control custom-radio">
                  <?php echo "<input name='arr[$idgabung]' id='$idgabung.1' type='radio' required='required' class='custom-control-input' value='1'>"; ?>
                  <?php echo "<label for='$idgabung.1' class='custom-control-label'><i class='fas fa-star'></i></label>"; ?>
              </div>
          </div>
          <div class="custom-controls-stacked">
              <div class="custom-control custom-radio">
                  <?php echo "<input name='arr[$idgabung]' id='$idgabung.2' type='radio' required='required' class='custom-control-input' value='2'>"; ?>
                  <?php echo "<label for='$idgabung.2' class='custom-control-label'><i class='fas fa-star'></i><i class='fas fa-star'></i></label>"; ?>
              </div>
          </div>
          <div class="custom-controls-stacked">
              <div class="custom-control custom-radio">
                  <?php echo "<input name='arr[$idgabung]' id='$idgabung.3' type='radio' required='required' class='custom-control-input' value='3'>"; ?>
                  <?php echo "<label for='$idgabung.3' class='custom-control-label'><i class='fas fa-star'></i><i class='fas fa-star'></i><i class='fas fa-star'></i></label>"; ?>
              </div>
          </div>
          <div class="custom-controls-stacked">
              <div class="custom-control custom-radio">
                  <?php echo "<input name='arr[$idgabung]' id='$idgabung.4' type='radio' required='required' class='custom-control-input' value='4'>"; ?>
                  <?php echo "<label for='$idgabung.4' class='custom-control-label'><i class='fas fa-star'></i><i class='fas fa-star'></i><i class='fas fa-star'></i><i class='fas fa-star'></i></label>"; ?>
              </div>
          </div>
          <div class="custom-controls-stacked">
              <div class="custom-control custom-radio">
                  <?php echo "<input name='arr[$idgabung]' id='$idgabung.5' type='radio' required='required' class='custom-control-input' value='5'>"; ?>
                  <?php echo "<label for='$idgabung.5' class='custom-control-label'><i class='fas fa-star'></i><i class='fas fa-star'></i><i class='fas fa-star'></i><i class='fas fa-star'></i><i class='fas fa-star'></i></label>"; ?>
              </div>
          </div>
      </div>
  </div>
  <br>
  
<div class = "row">
      <div class = "col-lg-12 col-xs-12">
          <label>Deskripsi</label>
          <?php echo "<textarea  class='form-control' rows='3' name='arr[$idgabung]' placeholder=''></textarea>"; ?>
      </div>
</div>

I wrap this input form in a loop and supply it with the data I have prepared, then I can retrieve the values ​​from the Radio button and Textarea in this way:

foreach($this->input->post("arr") as $arr_id => $arr_val)
{
    echo 'ID:'. $ arr_id. ", Value:". $ arr_val. "<br>";
}

But the problem is the Textarea value always overwrites the value taken from the Radio button selection result, maybe because it uses the same name?

What I want is, I can get their values ​​all at once in the same array without overlapping each other.

How do you name the correct form name? And how do you take their values without overlapping one another?

Upvotes: 2

Views: 58

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You need to change it a bit:

A) Change name of radio button to arr[$idgabung][key]

B) Change name of textArea to arr[$idgabung][value]

Now change PHP code like below:

foreach($this->input->post("arr") as $arr_val)
{
    echo 'ID:'. $arr_val['key']. ", Value:". $arr_val['value']. "<br>";
}

Upvotes: 1

Related Questions