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 如何在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