PHP - How to post relationships based on dynamic content generated with javascript












2


















 /*On call - Add person fields dynamically*/
var i=1;

$("#add_row").click(function(){

$('#addr').append("<tr class='row_"+i+"'><td>Person ID:"+i+"</td><td><input name='Person["+i+"][Name]' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='Person["+i+"][Email]' type='text' placeholder='Email' class='form-control input-md'></td><td><input name='Person["+i+"][Mobile]' type='text' placeholder='Mobile' class='form-control input-md'></td><td><button type='button' id='"+i+"' onclick='add_vehicle(this.id);'>Add Vehicle</button></td></tr>");
i++;
});


/*On call - Add hidden input value to section requesting to add vehicle license plate number and create plate field*/
var vehicle = 0;
function add_vehicle(id){

$('<td><input type="hidden" name="Person['+id+'][hasVehicle]" value="1"></td>').appendTo('.row_'+id);

var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+vehicle+'][PlateNumber]" placeholder="Plate Number" /></div><hr>';

$(section).appendTo(".vehicle");

vehicle++;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" action="server.php">
<table id="tab_logic">
<tbody>
<tr id='addr'></tr>
</tbody>
</table>
<div class="vehicle"></div>
<a id="add_row" class="btn btn-default pull-left">Add Person </a><hr>

<input type="submit" value="submit">
</form>





<?php
/* Posted array values*/
$people = $_POST['Person'];
$people_vehicle = $_POST['Vehicle'];

//check for each person
foreach ($people as $person) {

//check if person has a vehicle
if($person['hasVehicle'] == 1){

//Owner Name:
echo $person['Name'];

//get all the vehicle plate numbers specified for current person
foreach($people_vehicle as $person_vehicle) {

//output plate numbers
echo
'<pre>',print_r($person_vehicle['PlateNumber'],1),'</pre>';

}
}
}


I have created this code that dynamically allows a "person" to add as many "plate numbers" as needed. In the backend(using PHP) I would like to capture this relationship. I wrote some PHP code that actually works when ONLY one person is added and multiple plate numbers are printed the way I want to accomplish however when I added multiple people and multiple license plates it output double the values not sure why. If someone can please assist i would appreciated.



AGAIN: The goal is to ->
A. Add as many people as needed and plate numbers dynamically and then output what plate numbers belong to who.



Results: When adding multiple people and plate numbers.



Adam Joe
E1
P1
P2
Doe John
E1
P1
P2



Results: When adding one person and multiple plate numbers.(This is what i want even when generating multiple people).



Adam Joe
P1
P2
P3










share|improve this question





























    2


















     /*On call - Add person fields dynamically*/
    var i=1;

    $("#add_row").click(function(){

    $('#addr').append("<tr class='row_"+i+"'><td>Person ID:"+i+"</td><td><input name='Person["+i+"][Name]' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='Person["+i+"][Email]' type='text' placeholder='Email' class='form-control input-md'></td><td><input name='Person["+i+"][Mobile]' type='text' placeholder='Mobile' class='form-control input-md'></td><td><button type='button' id='"+i+"' onclick='add_vehicle(this.id);'>Add Vehicle</button></td></tr>");
    i++;
    });


    /*On call - Add hidden input value to section requesting to add vehicle license plate number and create plate field*/
    var vehicle = 0;
    function add_vehicle(id){

    $('<td><input type="hidden" name="Person['+id+'][hasVehicle]" value="1"></td>').appendTo('.row_'+id);

    var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+vehicle+'][PlateNumber]" placeholder="Plate Number" /></div><hr>';

    $(section).appendTo(".vehicle");

    vehicle++;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <form method="post" action="server.php">
    <table id="tab_logic">
    <tbody>
    <tr id='addr'></tr>
    </tbody>
    </table>
    <div class="vehicle"></div>
    <a id="add_row" class="btn btn-default pull-left">Add Person </a><hr>

    <input type="submit" value="submit">
    </form>





    <?php
    /* Posted array values*/
    $people = $_POST['Person'];
    $people_vehicle = $_POST['Vehicle'];

    //check for each person
    foreach ($people as $person) {

    //check if person has a vehicle
    if($person['hasVehicle'] == 1){

    //Owner Name:
    echo $person['Name'];

    //get all the vehicle plate numbers specified for current person
    foreach($people_vehicle as $person_vehicle) {

    //output plate numbers
    echo
    '<pre>',print_r($person_vehicle['PlateNumber'],1),'</pre>';

    }
    }
    }


    I have created this code that dynamically allows a "person" to add as many "plate numbers" as needed. In the backend(using PHP) I would like to capture this relationship. I wrote some PHP code that actually works when ONLY one person is added and multiple plate numbers are printed the way I want to accomplish however when I added multiple people and multiple license plates it output double the values not sure why. If someone can please assist i would appreciated.



    AGAIN: The goal is to ->
    A. Add as many people as needed and plate numbers dynamically and then output what plate numbers belong to who.



    Results: When adding multiple people and plate numbers.



    Adam Joe
    E1
    P1
    P2
    Doe John
    E1
    P1
    P2



    Results: When adding one person and multiple plate numbers.(This is what i want even when generating multiple people).



    Adam Joe
    P1
    P2
    P3










    share|improve this question



























      2












      2








      2











       /*On call - Add person fields dynamically*/
      var i=1;

      $("#add_row").click(function(){

      $('#addr').append("<tr class='row_"+i+"'><td>Person ID:"+i+"</td><td><input name='Person["+i+"][Name]' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='Person["+i+"][Email]' type='text' placeholder='Email' class='form-control input-md'></td><td><input name='Person["+i+"][Mobile]' type='text' placeholder='Mobile' class='form-control input-md'></td><td><button type='button' id='"+i+"' onclick='add_vehicle(this.id);'>Add Vehicle</button></td></tr>");
      i++;
      });


      /*On call - Add hidden input value to section requesting to add vehicle license plate number and create plate field*/
      var vehicle = 0;
      function add_vehicle(id){

      $('<td><input type="hidden" name="Person['+id+'][hasVehicle]" value="1"></td>').appendTo('.row_'+id);

      var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+vehicle+'][PlateNumber]" placeholder="Plate Number" /></div><hr>';

      $(section).appendTo(".vehicle");

      vehicle++;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <form method="post" action="server.php">
      <table id="tab_logic">
      <tbody>
      <tr id='addr'></tr>
      </tbody>
      </table>
      <div class="vehicle"></div>
      <a id="add_row" class="btn btn-default pull-left">Add Person </a><hr>

      <input type="submit" value="submit">
      </form>





      <?php
      /* Posted array values*/
      $people = $_POST['Person'];
      $people_vehicle = $_POST['Vehicle'];

      //check for each person
      foreach ($people as $person) {

      //check if person has a vehicle
      if($person['hasVehicle'] == 1){

      //Owner Name:
      echo $person['Name'];

      //get all the vehicle plate numbers specified for current person
      foreach($people_vehicle as $person_vehicle) {

      //output plate numbers
      echo
      '<pre>',print_r($person_vehicle['PlateNumber'],1),'</pre>';

      }
      }
      }


      I have created this code that dynamically allows a "person" to add as many "plate numbers" as needed. In the backend(using PHP) I would like to capture this relationship. I wrote some PHP code that actually works when ONLY one person is added and multiple plate numbers are printed the way I want to accomplish however when I added multiple people and multiple license plates it output double the values not sure why. If someone can please assist i would appreciated.



      AGAIN: The goal is to ->
      A. Add as many people as needed and plate numbers dynamically and then output what plate numbers belong to who.



      Results: When adding multiple people and plate numbers.



      Adam Joe
      E1
      P1
      P2
      Doe John
      E1
      P1
      P2



      Results: When adding one person and multiple plate numbers.(This is what i want even when generating multiple people).



      Adam Joe
      P1
      P2
      P3










      share|improve this question



















       /*On call - Add person fields dynamically*/
      var i=1;

      $("#add_row").click(function(){

      $('#addr').append("<tr class='row_"+i+"'><td>Person ID:"+i+"</td><td><input name='Person["+i+"][Name]' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='Person["+i+"][Email]' type='text' placeholder='Email' class='form-control input-md'></td><td><input name='Person["+i+"][Mobile]' type='text' placeholder='Mobile' class='form-control input-md'></td><td><button type='button' id='"+i+"' onclick='add_vehicle(this.id);'>Add Vehicle</button></td></tr>");
      i++;
      });


      /*On call - Add hidden input value to section requesting to add vehicle license plate number and create plate field*/
      var vehicle = 0;
      function add_vehicle(id){

      $('<td><input type="hidden" name="Person['+id+'][hasVehicle]" value="1"></td>').appendTo('.row_'+id);

      var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+vehicle+'][PlateNumber]" placeholder="Plate Number" /></div><hr>';

      $(section).appendTo(".vehicle");

      vehicle++;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <form method="post" action="server.php">
      <table id="tab_logic">
      <tbody>
      <tr id='addr'></tr>
      </tbody>
      </table>
      <div class="vehicle"></div>
      <a id="add_row" class="btn btn-default pull-left">Add Person </a><hr>

      <input type="submit" value="submit">
      </form>





      <?php
      /* Posted array values*/
      $people = $_POST['Person'];
      $people_vehicle = $_POST['Vehicle'];

      //check for each person
      foreach ($people as $person) {

      //check if person has a vehicle
      if($person['hasVehicle'] == 1){

      //Owner Name:
      echo $person['Name'];

      //get all the vehicle plate numbers specified for current person
      foreach($people_vehicle as $person_vehicle) {

      //output plate numbers
      echo
      '<pre>',print_r($person_vehicle['PlateNumber'],1),'</pre>';

      }
      }
      }


      I have created this code that dynamically allows a "person" to add as many "plate numbers" as needed. In the backend(using PHP) I would like to capture this relationship. I wrote some PHP code that actually works when ONLY one person is added and multiple plate numbers are printed the way I want to accomplish however when I added multiple people and multiple license plates it output double the values not sure why. If someone can please assist i would appreciated.



      AGAIN: The goal is to ->
      A. Add as many people as needed and plate numbers dynamically and then output what plate numbers belong to who.



      Results: When adding multiple people and plate numbers.



      Adam Joe
      E1
      P1
      P2
      Doe John
      E1
      P1
      P2



      Results: When adding one person and multiple plate numbers.(This is what i want even when generating multiple people).



      Adam Joe
      P1
      P2
      P3






       /*On call - Add person fields dynamically*/
      var i=1;

      $("#add_row").click(function(){

      $('#addr').append("<tr class='row_"+i+"'><td>Person ID:"+i+"</td><td><input name='Person["+i+"][Name]' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='Person["+i+"][Email]' type='text' placeholder='Email' class='form-control input-md'></td><td><input name='Person["+i+"][Mobile]' type='text' placeholder='Mobile' class='form-control input-md'></td><td><button type='button' id='"+i+"' onclick='add_vehicle(this.id);'>Add Vehicle</button></td></tr>");
      i++;
      });


      /*On call - Add hidden input value to section requesting to add vehicle license plate number and create plate field*/
      var vehicle = 0;
      function add_vehicle(id){

      $('<td><input type="hidden" name="Person['+id+'][hasVehicle]" value="1"></td>').appendTo('.row_'+id);

      var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+vehicle+'][PlateNumber]" placeholder="Plate Number" /></div><hr>';

      $(section).appendTo(".vehicle");

      vehicle++;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <form method="post" action="server.php">
      <table id="tab_logic">
      <tbody>
      <tr id='addr'></tr>
      </tbody>
      </table>
      <div class="vehicle"></div>
      <a id="add_row" class="btn btn-default pull-left">Add Person </a><hr>

      <input type="submit" value="submit">
      </form>





       /*On call - Add person fields dynamically*/
      var i=1;

      $("#add_row").click(function(){

      $('#addr').append("<tr class='row_"+i+"'><td>Person ID:"+i+"</td><td><input name='Person["+i+"][Name]' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='Person["+i+"][Email]' type='text' placeholder='Email' class='form-control input-md'></td><td><input name='Person["+i+"][Mobile]' type='text' placeholder='Mobile' class='form-control input-md'></td><td><button type='button' id='"+i+"' onclick='add_vehicle(this.id);'>Add Vehicle</button></td></tr>");
      i++;
      });


      /*On call - Add hidden input value to section requesting to add vehicle license plate number and create plate field*/
      var vehicle = 0;
      function add_vehicle(id){

      $('<td><input type="hidden" name="Person['+id+'][hasVehicle]" value="1"></td>').appendTo('.row_'+id);

      var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+vehicle+'][PlateNumber]" placeholder="Plate Number" /></div><hr>';

      $(section).appendTo(".vehicle");

      vehicle++;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

      <form method="post" action="server.php">
      <table id="tab_logic">
      <tbody>
      <tr id='addr'></tr>
      </tbody>
      </table>
      <div class="vehicle"></div>
      <a id="add_row" class="btn btn-default pull-left">Add Person </a><hr>

      <input type="submit" value="submit">
      </form>






      javascript php jquery loops






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 11:25







      Alex Garcia

















      asked Nov 22 '18 at 0:56









      Alex GarciaAlex Garcia

      177




      177
























          1 Answer
          1






          active

          oldest

          votes


















          1















          when I added multiple people and multiple license plates it output double the values not sure why.




          This is because you print all of the plate numbers for each person in the list. (inner loop is unnecessary)



          I suggest you do this:




          1. change your js so that you relate plates with one specific person (name="Vehicle['+id+']")





          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';







          1. when printing out, use only one for loop and make use of that id index we just used



            for ($i = 1; $i <= count($people); $i++) {

            //check if person has a vehicle
            if ($people[$i]['hasVehicle'] == 1) {

            //Owner Name:
            echo $people[$i]['Name'];

            //output plate numbers
            echo
            '<pre>', print_r($people_vehicle[$i], 1), '</pre>';
            }
            }



          So, you loop through all people, you get the person with number $i and check if that has a vehicle; if so, then print out list of vehicles which are also named with number $i.



          The result should be like this:



          A
          Array
          (
          [0] => A1
          [1] => A2
          )
          B
          Array
          (
          [0] => B1
          [1] => B2
          )
          C
          Array
          (
          [0] => C1
          [1] => C2
          )


          I hope this helps.






          share|improve this answer


























          • It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

            – Alex Garcia
            Nov 22 '18 at 13:52













          • I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

            – Adem Tepe
            Nov 22 '18 at 15:52






          • 1





            Yes I checked again and it works! Thank you!!!

            – Alex Garcia
            Nov 22 '18 at 16:01













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422496%2fphp-how-to-post-relationships-based-on-dynamic-content-generated-with-javascri%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          when I added multiple people and multiple license plates it output double the values not sure why.




          This is because you print all of the plate numbers for each person in the list. (inner loop is unnecessary)



          I suggest you do this:




          1. change your js so that you relate plates with one specific person (name="Vehicle['+id+']")





          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';







          1. when printing out, use only one for loop and make use of that id index we just used



            for ($i = 1; $i <= count($people); $i++) {

            //check if person has a vehicle
            if ($people[$i]['hasVehicle'] == 1) {

            //Owner Name:
            echo $people[$i]['Name'];

            //output plate numbers
            echo
            '<pre>', print_r($people_vehicle[$i], 1), '</pre>';
            }
            }



          So, you loop through all people, you get the person with number $i and check if that has a vehicle; if so, then print out list of vehicles which are also named with number $i.



          The result should be like this:



          A
          Array
          (
          [0] => A1
          [1] => A2
          )
          B
          Array
          (
          [0] => B1
          [1] => B2
          )
          C
          Array
          (
          [0] => C1
          [1] => C2
          )


          I hope this helps.






          share|improve this answer


























          • It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

            – Alex Garcia
            Nov 22 '18 at 13:52













          • I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

            – Adem Tepe
            Nov 22 '18 at 15:52






          • 1





            Yes I checked again and it works! Thank you!!!

            – Alex Garcia
            Nov 22 '18 at 16:01


















          1















          when I added multiple people and multiple license plates it output double the values not sure why.




          This is because you print all of the plate numbers for each person in the list. (inner loop is unnecessary)



          I suggest you do this:




          1. change your js so that you relate plates with one specific person (name="Vehicle['+id+']")





          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';







          1. when printing out, use only one for loop and make use of that id index we just used



            for ($i = 1; $i <= count($people); $i++) {

            //check if person has a vehicle
            if ($people[$i]['hasVehicle'] == 1) {

            //Owner Name:
            echo $people[$i]['Name'];

            //output plate numbers
            echo
            '<pre>', print_r($people_vehicle[$i], 1), '</pre>';
            }
            }



          So, you loop through all people, you get the person with number $i and check if that has a vehicle; if so, then print out list of vehicles which are also named with number $i.



          The result should be like this:



          A
          Array
          (
          [0] => A1
          [1] => A2
          )
          B
          Array
          (
          [0] => B1
          [1] => B2
          )
          C
          Array
          (
          [0] => C1
          [1] => C2
          )


          I hope this helps.






          share|improve this answer


























          • It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

            – Alex Garcia
            Nov 22 '18 at 13:52













          • I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

            – Adem Tepe
            Nov 22 '18 at 15:52






          • 1





            Yes I checked again and it works! Thank you!!!

            – Alex Garcia
            Nov 22 '18 at 16:01
















          1












          1








          1








          when I added multiple people and multiple license plates it output double the values not sure why.




          This is because you print all of the plate numbers for each person in the list. (inner loop is unnecessary)



          I suggest you do this:




          1. change your js so that you relate plates with one specific person (name="Vehicle['+id+']")





          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';







          1. when printing out, use only one for loop and make use of that id index we just used



            for ($i = 1; $i <= count($people); $i++) {

            //check if person has a vehicle
            if ($people[$i]['hasVehicle'] == 1) {

            //Owner Name:
            echo $people[$i]['Name'];

            //output plate numbers
            echo
            '<pre>', print_r($people_vehicle[$i], 1), '</pre>';
            }
            }



          So, you loop through all people, you get the person with number $i and check if that has a vehicle; if so, then print out list of vehicles which are also named with number $i.



          The result should be like this:



          A
          Array
          (
          [0] => A1
          [1] => A2
          )
          B
          Array
          (
          [0] => B1
          [1] => B2
          )
          C
          Array
          (
          [0] => C1
          [1] => C2
          )


          I hope this helps.






          share|improve this answer
















          when I added multiple people and multiple license plates it output double the values not sure why.




          This is because you print all of the plate numbers for each person in the list. (inner loop is unnecessary)



          I suggest you do this:




          1. change your js so that you relate plates with one specific person (name="Vehicle['+id+']")





          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';







          1. when printing out, use only one for loop and make use of that id index we just used



            for ($i = 1; $i <= count($people); $i++) {

            //check if person has a vehicle
            if ($people[$i]['hasVehicle'] == 1) {

            //Owner Name:
            echo $people[$i]['Name'];

            //output plate numbers
            echo
            '<pre>', print_r($people_vehicle[$i], 1), '</pre>';
            }
            }



          So, you loop through all people, you get the person with number $i and check if that has a vehicle; if so, then print out list of vehicles which are also named with number $i.



          The result should be like this:



          A
          Array
          (
          [0] => A1
          [1] => A2
          )
          B
          Array
          (
          [0] => B1
          [1] => B2
          )
          C
          Array
          (
          [0] => C1
          [1] => C2
          )


          I hope this helps.






          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';





          var section = '<hr><div class"section_'+vehicle+'">Belongs To Person With ID: '+id+' <input type="text" name="Vehicle['+id+']" placeholder="Plate Number" /></div><hr>';






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 12:23

























          answered Nov 22 '18 at 12:07









          Adem TepeAdem Tepe

          936




          936













          • It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

            – Alex Garcia
            Nov 22 '18 at 13:52













          • I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

            – Adem Tepe
            Nov 22 '18 at 15:52






          • 1





            Yes I checked again and it works! Thank you!!!

            – Alex Garcia
            Nov 22 '18 at 16:01





















          • It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

            – Alex Garcia
            Nov 22 '18 at 13:52













          • I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

            – Adem Tepe
            Nov 22 '18 at 15:52






          • 1





            Yes I checked again and it works! Thank you!!!

            – Alex Garcia
            Nov 22 '18 at 16:01



















          It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

          – Alex Garcia
          Nov 22 '18 at 13:52







          It doesn't work. I tried adding 2 people where person 1 has 2 license plate number and the second person number only has one license plate number. I get the following:Mike Array ( [0] => Vehicle 2 ) Ana Array ( [0] => Vehicle 1 )

          – Alex Garcia
          Nov 22 '18 at 13:52















          I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

          – Adem Tepe
          Nov 22 '18 at 15:52





          I have tried this. You must be missing something, please check, it works and gives this result: Mike Array ( [0] => Vehicle 1 [1] => Vehicle 2 ) Ana Array ( [0] => Vehicle 3 )

          – Adem Tepe
          Nov 22 '18 at 15:52




          1




          1





          Yes I checked again and it works! Thank you!!!

          – Alex Garcia
          Nov 22 '18 at 16:01







          Yes I checked again and it works! Thank you!!!

          – Alex Garcia
          Nov 22 '18 at 16:01






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422496%2fphp-how-to-post-relationships-based-on-dynamic-content-generated-with-javascri%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]