dtxpz8785 2010-12-12 09:51
浏览 78
已采纳

多个表单文本字段

How can I get only values of the form fields that have marked checkbox?

Form:

<form id="form" name="form" method="post" action="">

<input type="text" name="textfield[]"  value="textf 1"/>
<textarea name="textarea[]" cols="45" rows="5">some text 1</textarea>
<input name="check[]" type="checkbox" value="checkb 1" />

<input type="text" name="textfield[]"  value="textf 2" />
<textarea name="textarea[]" cols="45" rows="5">some text 2</textarea>
<input name="check[]" type="checkbox" value="checkb 2" />

<input type="text" name="textfield[]"   value="textf 3"/>
<textarea name="textarea[]" cols="45" rows="5">some text 3</textarea>
<input name="check[]" type="checkbox" value="checkb 3" />

</form>

I want to get form field values for specific textfield and textarea. For example if I mark the first and last checkbox in this example. How can I do that using PHP?

Then result should be:

textf 1
some text 1
checkb 1

textf 3
some text 3
checkb 3
  • 写回答

1条回答 默认 最新

  • doue1925 2010-12-12 10:02
    关注

    Give your checkboxes and textareas set indexes so you know easily which checkbox corresponds to what form.

    <input type="text" name="textfield[0]"  value="textf 1"/>
    <textarea name="textarea" id="textarea[0]" cols="45" rows="5">some text 1</textarea>
    <input name="check[0]" type="checkbox" value="checkb 1" />
    
    <input type="text" name="textfield[1]"  value="textf 2" />
    <textarea name="textarea" id="textarea[1]" cols="45" rows="5">some text 2</textarea>
    <input name="check[1]" type="checkbox" value="checkb 2" />
    

    Then you can quickly check whether a specific checkbox is set, and do something with the associated textarea.

    foreach ($_POST['textfield'] as $idx => $value) {
      echo $_POST['textfield'][$idx] . "<br />";
      echo $_POST['textarea'][$idx] . "<br />";
    }
    

    That will only print the textareas that have checked textfields, since those that weren't checked aren't in $_POST.

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

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号