title: sending dynamic data to PHP
body: <p>I have form and you can you're employment history to it, this is displayed in a row of three inputs: Job title, Job duration and job location. you can add employment which adds an empty set of inputs, in which you can add more data.  </p>
  
  <p>I'm sending the HTML form to a PHP script via AJAX, it is sending all the sets of inputs with the same name separately, so not in an array. on the PHP side, I'm json encoding the post variables by putting them in an array. <code>$json_data</code></p>
  
  <p>The issue is that the PHP script saves only the last set of inputs. so if you enter 3 jobs it only saves the last one. </p>
  
  <pre><code>&lt;!-- employment row --&gt;
  
      &lt;p class="mt-3 text-left"&gt; Employment history &lt;/p&gt;
      &lt;div class="row employment_block"&gt;
  
          &lt;div class="col-md-3"&gt;
              &lt;input type="text" name="job_title[]" placeholder="Job title"&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-3"&gt;
              &lt;input type="text" name="job_duration[]" placeholder="Duration (i.e. 3 months / 1 year)"&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-3"&gt;
              &lt;input type="text" name="job_location[]" placeholder="Country"&gt;
          &lt;/div&gt;
          &lt;div class="col-md-3 text-center"&gt;
          &lt;button href="#" class="able_btn m-0 w-100 disabled" disabled aria-disabled="true" style="opacity:0.5"&gt; remove &lt;/button&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
  
          &lt;div class="text-center mb-3 mt-3"&gt;
              &lt;a class="add_employment" href=""&gt; Add employment &lt;/a&gt;  
          &lt;/div&gt;
  
  
  
  &lt;!-- JS --&gt; 
  
  $(document).ready(function(){
  
  $(".account_cv_update_form").submit(function(evt){  
  evt.preventDefault();
  
        var formData = new FormData($(this)[0]);
     $.ajax({
         url: 'handlers/cv_update.php',
         type: 'POST',
         data: formData,
         async: false,
         cache:false,
         contentType: false,
         processData: false,
         dataType: "json",
      success: function (response) {
  
        switch(response.message){   
          case 'success': success();
          break;
          case 'error': error();
          break;
  
        }
  
      }
    });
  return false;
  });
  
  });
  
  
  
  $('.add_employment').click(function(evt) {
   evt.preventDefault();
  
      $('.employment_block:last').after('&lt;div class="row employment_block"&gt;&lt;div class="col-md-3"&gt;&lt;input type="text" name="job_title" placeholder="Job title"&gt;&lt;/div&gt;&lt;div class="col-md-3"&gt;&lt;input type="text" name="job_duration" placeholder="Duration (i.e. 3 months / 1 year)"&gt;&lt;/div&gt;&lt;div class="col-md-3"&gt;&lt;input type="text" name="job_location" placeholder="Country"&gt;&lt;/div&gt;&lt;div class="col-md-3 text-center"&gt;&lt;button class="able_btn m-0 w-100 remove_employment"&gt; remove &lt;/button&gt;&lt;/div&gt;&lt;/div&gt;');
  });
  
  
  
  &lt;!-- PHP code --&gt; 
  
  $json_data = array(
          'job_title' =&gt; $job_title,
          'job_location' =&gt; $job_location,
          'job_duration' =&gt; $job_duration );
  
  
  $cv_data = json_encode($json_data);
  
      $stmt = $dbh-&gt;prepare("UPDATE  table SET profile_picute=:profile_picute, first_name=:first_name, last_name=:last_name, phone_number=:phone_number, email_address=:email_address, data=:data WHERE id=:id");
  
       $stmt-&gt;bindParam(':profile_picute', $new_file_name);
       $stmt-&gt;bindParam(':first_name', $first_name);
       $stmt-&gt;bindParam(':last_name', $last_name);
       $stmt-&gt;bindParam(':phone_number', $phone_number);
       $stmt-&gt;bindParam(':email_address', $email_address);
       $stmt-&gt;bindParam(':data', $cv_data);
       $stmt-&gt;bindParam(':id', $user_id);
  
  
  if($stmt-&gt;execute()){
  $response["message"] = 'success';   
  }else{
  $response["message"] = 'error'; 
  $errors++;
  }
  
  echo json_encode($response);
  exit();
  </code></pre>