0

I have a field that can be multiple when user will add another data. Here's a screenshot:

enter image description here

Here's my html code

<tr>
                <td>経歴(学歴)</td>
                <td>
                    <div id="academic">
                        <select name="c_ac_year">
                            <option value="2019">2019</option>
                            <option value="2018">2018</option>
                            <option value="2017">2017</option>
                        </select>
                        <select name="c_ac_month">
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                        </select>
                        <input type="text" id="form-control" name="c_ac_desc">
                    </div>
                    <div id="new_chq"></div>
                    <a href="" class="add">Add</a>
                </td>
            </tr>
            <tr>

When click add, another set of fields will be added. How will I save these data in the database in array form?

2 Answers 2

1

You need to use php array[] input in HTML side as like this :

<div id="academic">
   <select name="c_ac_year[]">
      <option value="2019">2019</option>
      <option value="2018">2018</option>
      <option value="2017">2017</option>
  </select>
  <select name="c_ac_month[]">
      <option value="January">January</option>
      <option value="February">February</option>
      <option value="March">March</option>
 </select>
 <input type="text" id="form-control" name="c_ac_desc[]">

In PHP side, You can get value :

if(!empty($_POST))
{
    $years = $_POST['c_ac_year']; //or $this->input->post('c_ac_year');
    $months = $_POST['c_ac_month']; // or $this->input->post('c_ac_month');
    $descs = $_POST['c_ac_desc']; // or $this->input->post('c_ac_desc');
     $response = array();
    foreach($years as $key=> $year)
    {
       $response[$key]['year'] = $year;
       $response[$key]['month'] = $months[$key];
       $response[$key]['desc'] = $descs[$key];
    }
    echo json_encode($response); exit;
}

Output will be :

  [
{
year: "2019",
month: "January",
desc: "value 1",
},
{
year: "2018",
month: "February",
desc: "value 2",
},
{
year: "2017",
month: "March",
desc: "value 3",
},
]
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much sir. for your code. it really helps a lot.
1

Use array names in the html like name="c_ac_year[]"

Then in controller $this->input->post('c_ac_year'); will be an array and you can use indexing to get the other associated fields for that row

4 Comments

thank you for your answer sir. I'm sorry but how to use indexing sir?
For example foreach($this->input->post('c_ac_year') as $index=>$value)
okay sir. But how to get this sir {"2019" , "January", "comment 1"}, {"2018" , "February", "comment 2"}. This should be the output that can be saved in database. Sorry sir i am comfusing.
Thank you so much sir for your answer. Almost fix my code with the help of @ni3solanki and your explaination sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.