0

I have two divs that have the same purpose but different values

<div id="tracks-1">
     <div>
        <label>Song Title</label>
         <input type="text" name="tracks[song_title]" value="">
         <input type="text" name="tracks[price]" value="">

     </div>
</div>

<div id="tracks-2">
     <div>
        <label>Song Title</label>
         <input type="text" name="tracks[song_title]" value="">
         <input type="text" name="tracks[price]" value="">
     </div>
</div>

when I submit it via post request Laravel I get the tracks data something like this

"tracks" => [
    "song_title" => "some title",
    "price" => "23"
    "song_title" => "some title 2",
    "price" => "25"
]

I want it Like this

"tracks" => [
   0 => ["song_title" => "some title", "price" => "23"],
   1 => ["song_title" => "some title 2", "price" => "25"]
]

Is there a way to do without javascript and JQuery

2 Answers2

0

If you remove the song_title ref from the name attributes in the form inputs to get an integer indexed array...


    <div id="tracks-1">
        <div>
            <label>Song Title</label>
            <input type="text" name="tracks[]" value="">
        </div>
    </div>

    <div id="tracks-2">
        <div>
            <label>Song Title</label>
            <input type="text" name="tracks[]" value="">
        </div>
    </div>

Then you could use an array map on the posted form data like this...


    $tracks = array_map ( function ( $trackTitle ) {
      return ["song_title" => $trackTitle];
    }, $_POST['tracks'] );

Noel H
  • 1
  • 1
-1

Try something like :

if $q = 1" { echo "song title }
Littm
  • 4,923
  • 4
  • 30
  • 38