Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to access the value of the text field with respect to the respective checkboxes

0 like 0 dislike
1,170 views

 

I've 3 text fields and I have 3 checkboxes attached with it. I want to store the values of only those textboxes in one varible whose respective checkboxes is checked. with the help of following codes I can access the values of all the textboxes but for that I've to check all the checkboxes, where as I want to be selective with it. If only 2 checkboxes are checked then I want to have only 2 respective textboxes values stored in a varible. Below is my HTML and PHP codes:

 [source language='html']
<html>
    <head></head>
    <body>

    <form action="access.php" method="POST">
    <tr>
    <td>Address:</td><td> <input type="text" name="address" value="<?php echo "$address"?>" disabled="disabled" id="field1"  /></td>
                          <td><input id="CheckBox[]" type="checkbox" onClick="enableText1(this.checked, 'field1');" name="CheckBox1" /></td>
    </tr>

    <tr>
    <td>Source: </td><td><input type="text" name="source" value="<?php echo "$source"?>"  disabled="disabled" id="field2"  /></td>
                          <td><input id="CheckBox[]" type="checkbox" onClick="enableText2(this.checked, 'field2');" name="CheckBox2" /></td>
    </tr>
            <tr>
    <td>Mobile:</td><td> <input type="text" name="mobile" value="<?php echo "$mobile"?>" disabled="disabled" id="field3"  /></td>
                          <td><input id="CheckBox[]" type="checkbox" onClick="enableText3(this.checked, 'field3');" name="CheckBox3" /></td>></td>
    </tr>
    <tr style="border:#FFF";>
    <td><input type="Submit" name="submit" value="Update"></td>
    </tr>
    </form>
    </table>
    </body>
    </html>
[/source]

[source language='php']
 access.php
  <?php
  if(isset($_POST['CheckBox1']))
 {
     if(isset($_POST['CheckBox2']))
 {
    if(isset($_POST['CheckBox3']))
   {
    $b=array($address,$source,$mobile);
    $a=implode('', $b);
    print $a;
   }
 }
 }   
 ?>[/source]

with the help of the above codes if I check all the checkboxes then I am able to print all the 3 textbox's values but if I do not check all the checkboxes then it does not show the value of the respective textboxes. I do not want to change the name of the textboxes name as m going to accept these values to insert into the database. can anyone please sort out this problem? cheers..

asked Jan 23, 2012 by anonymous  
edited Jan 23, 2012

1 Answer

0 like 0 dislike
 
Best answer

 

[source language='php']access.php

 <?php
  $b=array();
  if(isset($_POST['CheckBox1']))
 {
   $b['address'] = $address;
}
     if(isset($_POST['CheckBox2']))
 {
  $b['source'] = $source;
}
    if(isset($_POST['CheckBox3']))
   {
   $b['mobile'] = $mobile;
   }
$a=implode('', $b);
print $a;
 ?>[/source]

use it.

answered Jan 23, 2012 by anonymous  
...