PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] How can I "POST" any data using table and loop (foreach)

 March 05, 2022     codeigniter, codeigniter-3, html, mysql, php     No comments   

Issue

I'm so confused with this problem.

As we can see from this code, I show the symptoms_data from database using loop. But, I want user can input a certainty value in each symptoms. The result of that code is only show a table with symptoms name and also the select box but, I don't know how can we save the value of each certainty symptoms because there's no id in each value.

      <tbody>
      <?php $i = 1;
      foreach ($symptoms_data->result() as $key) : ?>
        <tr>
          <td scope="col">
            <?= $i++; ?>
          </td>
          <td scope="col">
            <?php echo $key->symptoms_name ?>
          </td>
          <td scope="col">
            <select class="form-control" aria-label="Default select example" name="" id="" placeholder="" required>
              <option value=" " selected disabled>Choose Certainty Value</option>
              <option value="0.1 ">0.1</option>
              <option value="0.5 ">0.5</option>
              <option value="0.7 ">0.7</option>
              <option value="1 ">1</option>
            </select>
          </td>
          </td>
        </tr>
        <?php endforeach ?>
      </tbody>
    </table>
    <br>
  </div>
  <Button type="submit " class="btn btn-dark btn-block "> <i class="fa fa-check "></i> DIAGNOSE </button>
</form>

Solution

You need to give the certainty element a named array value. So change the name="" part to something like name="certainty[]". If there is an id associated with each record (I assume there is), include that as a hidden value. eg. <input type="hidden" "name="id[]" value="12345" />.

Once form is submitted, in PHP cycle through each id element, using the index key as reference to the associated certainty value:

foreach($_POST['id'] as $key=>$value)
{
  $curr_record_id = $value;
  $curr_record_certainty = $_POST['certainty'][$key];
}

...then just store those details in the database table.



Answered By - James
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing