开发者

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, lik开发者_C百科e 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!


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/


What do you meen with posting the final array to a php script? If you want to send the information to a php script try looking the jquery documentation about ajax (to send the data to the php file without reloading the page) and json (to convert your array into a text form and then beeing able to rebuild the array in the php file)


Do you mean sending the array elements to a PHP script? in JSON and use json_decode() in PHP. Depending on what information you need, json_decode() can either convert to an associate array, or objects. Use JQuery-Json which will add the ability to encode JSON in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜