简体   繁体   English

将JS(JQuery)生成的元素发布到PHP脚本

[英]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: 因此,我正在为团队编写此表单,并执行此“添加玩家”功能,即您在文本字段中输入玩家名称,选中一个框后,就可以了,然后单击按钮,然后将该名称附加到上方的div上,添加值到数组并清除下面的文本字段,如下所示:

<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? 将最终数组发布到php脚本的最佳方法是什么?

Thanks guys! 多谢你们!

I would use JQuery's ajax method: 我将使用JQuery的ajax方法:

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

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

What do you meen with posting the final array to a php script? 将最终数组发布到php脚本意味着什么? 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) 如果您想将信息发送到php脚本,请尝试查看有关ajax的jquery文档(将数据发送到php文件而无需重新加载页面)和json(将您的数组转换为文本形式,然后可以重新构建php文件中的数组)

Do you mean sending the array elements to a PHP script? 您的意思是将数组元素发送到PHP脚本吗? in JSON and use json_decode() in PHP. 在JSON中使用,并在PHP中使用json_decode()。 Depending on what information you need, json_decode() can either convert to an associate array, or objects. 根据您需要的信息,json_decode()可以转换为关联数组或对象。 Use JQuery-Json which will add the ability to encode JSON in your code. 使用JQuery-Json ,它将添加在您的代码中编码JSON的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM