dongmian5325 2013-11-16 16:57
浏览 43
已采纳

如何使用关联数组从复选框中获取id并从文本字段中获取值

GUI

Hi everyone, i'm new to programming. I need to develop a rating system with check boxes and text-fields where user clicks the subjects from the list and add his rating/experience in the text field. All these subjects and ratings are added to the database with the subject id and the rating. So the issue is, I don't know how to write the associate array to get the selected subjects and their appropriate rating to insert into the database. Can anyone please provide me some codes or samples similar to this. So, I can get some idea how to do this. Thanks in advance :)

This is the HTML sample code

<form action="" method="post">
<table width="372" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="24"><input type="checkbox" name="subid1" value="1" id="subid1"></td>
    <td width="203">Maths</td>`enter code here`
    <td width="145"><input type="text" name="sub1" id="sub1"></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="subid2" value="2" id="subid2" /></td>
    <td>Physics</td>
    <td><input type="text" name="subid2" id="subid2" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="subid3" value="3" id="subid3" /></td>
    <td>Computing</td>
    <td><input type="text" name="subid3" id="subid3" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="subid4" value="4" id="subid4" /></td>
    <td>Chemistry</td>
    <td><input type="text" name="subid4" id="subid4" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="subid5" value="5" id="subid5" /></td>
    <td>Biology</td>
    <td><input type="text" name="subid5" id="subid5" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="subid7" value="6" id="subid7" /></td>
    <td>Human Biology</td>
    <td><input type="text" name="subid6" id="subid6" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="Submit" /></td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
  • 写回答

1条回答 默认 最新

  • dongtao9095 2013-11-16 17:07
    关注

    This will do the job on client side:

    <input type="checkbox" name="cb[myID1]" value="1" />
    <input type="text" name="t[myID1]" value="" />
    <input type="checkbox" name="cb[myID2]" value="1" />
    <input type="text" name="t[myID2]" value="" />
    

    and this can be used on server side:

    $usedTexts = array();
    if ( array_key_exists("t", $_POST)  && is_array($_POST["t"])
      && array_key_exists("cb", $_POST) && is_array($_POST["cb"]) 
    ) 
    {
      $usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
    }
    

    see manual for server side: http://us3.php.net/array_intersect_key

    edit: fixed to POST; added array_key_exists() and is_array()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog