创建最佳表单,向challonge api发布批量添加信息

trnvg8h3  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(547)

我正在使用此文档:https://api.challonge.com/v1/documents/participants/bulk_add
我已经知道如何通过使用多行textarea来利用批量添加,但是我不知道除了名称之外还必须添加其他信息。例如,我还想添加其他信息。
这是可行的,但我无法确定如何收集其他信息:

  1. <form action="bulkadd.php" method="post">
  2. <textarea id='name' name='name' rows='64' cols='25'></textarea>
  3. </form>
  4. <input type="submit" value="Submit">
  5. <input type="reset" value="Reset">

我希望生成的数组为:

  1. {"participants": [
  2. {"name": "John Doe", "misc": null},
  3. {"name": "Jane Doe", "misc": "*"},
  4. {"name": "Jenn Doe", "misc": "*"}]
  5. }

我尝试添加一个复选框来设置“杂项”,如下所示: <input type="checkbox" name="sidepot" id="misc" value="*"> ,但我不知道如何将其同步到正确的“名称”。关于如何实现这一点有什么想法吗?

frebpwbc

frebpwbc1#

要关联表单中的字段,可以使用 [] 元素名称上的语法-名称中带有方括号的多个同名元素的行为类似于数组并具有索引。如果其他字段也有方括号,则可以使用相同的索引链接,例如, name[1]misc[1]
可以使用一些非常简单的javascript来克隆相关的html,这样当表单实际提交时,每个部分中的字段都可以相互关联。底部的代码片段显示了正在进行的克隆过程。注:我用过 username 在这里而不是 name 对于输入元素,但这是我的疏忽。
例如:

  1. <!DOCTYPE html>
  2. <html lang='en'>
  3. <head>
  4. <meta charset='utf-8' />
  5. <title>Challonge</title>
  6. <style>
  7. body,body *{box-sizing:border-box;font-family:verdana;}
  8. form{display:block;width:40%;padding:1rem;border:1px solid black;margin:auto;float:left;background:whitesmoke;clear:both;}
  9. section{margin:1rem auto;padding:0.25rem;border:1px dotted gray;background:white;}
  10. label{display:block;margin:0.25rem auto;padding:0.25rem;}
  11. label > input{float:right;padding:0.25rem;}
  12. label:before{content:attr(for);text-transform:capitalize}
  13. [type='submit'],
  14. [type='reset'],
  15. [name='add']{padding:0.5rem;height:3rem}
  16. input[name='add']{font-weight:bold;float:right}
  17. pre{display:block;margin:2rem auto;float:left;clear:both; width:40%;font-size:smaller;}
  18. </style>
  19. </head>
  20. <body>
  21. <form name='challonge' method='post'>
  22. <section>
  23. <label for='username'><input type='text' name='username[]' value='john doe' /></label>
  24. <label for='misc'><input type='text' name='misc[]' value='abc-1' /></label>
  25. </section>
  26. <div>
  27. <input type='submit' />
  28. <input type='reset' />
  29. <input type='button' name='add' value='+' />
  30. </div>
  31. </form>
  32. <script>
  33. document.querySelector('input[name="add"]').addEventListener('click',e=>{
  34. let form=document.forms.challonge;
  35. form.insertBefore( form.querySelector('section').cloneNode( true ), form.querySelector('div') );
  36. });
  37. </script>
  38. <?php
  39. /*
  40. The following PHP emulates what you would put within bulkadd.php
  41. before sending the json data to the challonge api. In effect this
  42. is acting as the form target here for demo.
  43. */
  44. if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['username'] ) ){
  45. // to show raw post data is related by index
  46. printf('<pre>%s</pre>',print_r( $_POST, true ) );
  47. // process the POST data to form desired output
  48. $data=array();
  49. foreach( $_POST['username'] as $index => $name ){
  50. $data[]=array(
  51. 'name' => $name,
  52. 'misc' => $_POST['misc'][$index]
  53. );
  54. }
  55. $json=json_encode( array( 'participants'=>$data ) );
  56. // to show processed json data
  57. printf('<pre>%s</pre>',print_r( $json, true ) );
  58. }
  59. ?>
  60. </body>
  61. </html>


上述结果产生以下输出:

  1. Array
  2. (
  3. [username] => Array
  4. (
  5. [0] => john doe
  6. [1] => jane doe
  7. [2] => jenn doe
  8. )
  9. [misc] => Array
  10. (
  11. [0] => abc-1
  12. [1] => abc-2
  13. [2] => abc-3
  14. )
  15. )
  16. {
  17. "participants":[
  18. {"name":"john doe","misc":"abc-1"},
  19. {"name":"jane doe","misc":"abc-2"},
  20. {"name":"jenn doe","misc":"abc-3"}
  21. ]
  22. }
  1. /*
  2. Clone the portion of the form that contains the fields we wish to
  3. include in the final json data. No element should have an ID assigned
  4. unless other steps are taken to account for and prevent duplicate IDs.
  5. * /
  6. document.querySelector('input[name="add"]').addEventListener('click',e=>{
  7. let form=document.forms.challonge;
  8. form.insertBefore( form.querySelector('section').cloneNode( true ), form.querySelector('div') );
  9. });
  1. body,body *{box-sizing:border-box;font-family:verdana;}
  2. form{display:block;width:80%;padding:1rem;border:1px solid black;margin:auto;float:left;background:whitesmoke;clear:both;}
  3. section{margin:1rem auto;padding:0.25rem;border:1px dotted gray;background:white;}
  4. label{display:block;margin:0.25rem auto;padding:0.25rem;}
  5. label > input{float:right;padding:0.25rem;}
  6. label:before{content:attr(for);text-transform:capitalize}
  7. [type='submit'],
  8. [type='reset'],
  9. [name='add']{padding:0.5rem;height:3rem}
  10. input[name='add']{font-weight:bold;float:right}
  11. pre{display:block;margin:2rem auto;float:left;clear:both; width:80%;font-size:smaller;}
  1. <form name='challonge' method='post'>
  2. <section>
  3. <label for='username'><input type='text' name='username[]' value='john doe' /></label>
  4. <label for='misc'><input type='text' name='misc[]' value='abc-1' /></label>
  5. </section>
  6. <div>
  7. <input type='submit' />
  8. <input type='reset' />
  9. <input type='button' name='add' value='+' />
  10. </div>
  11. </form>
展开查看全部

相关问题