Search code examples
phpjqueryformsposting

Posting elements generated by JS (JQuery) to a PHP script


so I'm writing this form for teams and doing this 'add player' function where you enter a player name in a textfield, check a box for medically ok and click a button which then appends the name to a div above, adds the value to an array and clears the textfield below, like so:

<script>
     var players = New Array();
     function addPlayer(){
          $('#players').append($('#addPlayerField').val());
          var playerInfo = New Array($('#addTermCheckbox').prop('checked'),$('#addPlayersField').val());
          addingTerms.push(playerInfo);
          $('#addPlayerField').val('');
       }
</script>

Whats the best way to post the final array to a php script?

Thanks guys!


Solution

  • I would use JQuery's ajax method:

    $.ajax({
      type: 'POST',
      url: url,
      data: 'players='+$.toJSON(players),
      success: function (data) {}
    });
    

    http://api.jquery.com/jQuery.post/