1

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!

3 Answers 3

2

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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)

2 Comments

nothing like .load or some, its a html form and I want to be able to do a foreach on the array one a user submits
To send the data, you should use an ajax request, documented in api.jquery.com/jQuery.post , using the POST method and a JSON encode library (example: openjs.com/scripts/data/json_encode.php), you cand send the entire array to php in a nice format: $.ajax({ type: 'POST', url: url, data: 'playerData'+array2json(playerDataVar) success: function (data) {} }); And retrieve it using php on the server side, php.net/manual/es/function.json-decode.php
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.