简体   繁体   中英

submit checkbox value for the dynamic elements on html form

I have check box which will be dynamically added along with the textfields.

<input type="checkbox" name="namechkbox[]">
<input type="text" name="nametxt[]">

I will need to map the checkbox value with the text field. I found from other questions that after adding the hidden element over the input element checkbox.

<input type="hidden" name=namechkbox[]" value=0> 

Since it's dynamic, it will add the index because of name[] in the name.

What is the way to handle checkbox with value submit for the dynamic elements?

Try this:

<?php
if(isset($_POST)){ 
  $invite = $_POST;
  echo '<pre>'; 
  print_r($invite);  
} 


?>


<form method="post" action="">
 <?php  for($i=0;$i<4;$i++) 
   { 
   ?>          

    <input  value="chkbob1"  name="invite['id<?php echo $i;?>']" type="checkbox">
    <input  value=""  name="name['id<?php echo $i;?>']" type="text">  
<?php    
}

 ?>   <input type="submit">
</form> 

if user checks any checkbox change the values off to on and on to off viceversa.

for ex:

<input type="checkbox" name="namechkbox[0]" value="on" />
<input type="hidden" name="nametxt[0]" value="This my data string 1" />

<input type="checkbox" name="namechkbox[1]" value="off" />
<input type="hidden" name="nametxt[1]" value="This my data string 2" />

now you submits the from then check loop through array to check 
if namechkbox[0] value is on 
then take the value of nametxt[0] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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