Forums

How to get CheckBox state

amsharma 04 Jul, 2009
Hello,

I have created a form which takes inputs from a user for data entry. A kind of address list. I have to allow the user to generate a CSV file from this data based on some criteria. I have created another form where we have some Checkboxes for user to specify choices.

I have a very basic problem, we are unable to get the CheckBox state. Firstly we tried using PHP but have been unsuccessful, we can probably do it using JavaScript. We would like to know which option is the right approach. We need to do all this processing in OnSubmit button. It would be nice if you could provide a sample code to do this. I searched these forums but am still not sure how to do this.

Thanks
Amal
Max_admin 04 Jul, 2009
Hi Amal,

if your checkbox code in HTML looks like this:

<input type="checkbox" name="mycheck" value="1" />


then to check if its checked or not after the submission you need:

<?php
if($_POST['mycheck'] == '1'){
//checked
}else{
//not
}


your check box name may has [] and so the $_POST data for it will be of type array!

Regards
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
amsharma 05 Jul, 2009
hello max,
Thanks for reply,but its not working as i think in the my first post my problem is not clear..
here i am sending you my all code and screen shot of page...see this and told me where am i wrong...

here the my whole html code..
<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: ;">Categeory</label>

<?php
    $db=& JFactory::getDBO();
    $query = "SELECT cat_name From `jos_chronoforms_Categeory_master`";
    $db->setQuery($query);
    $rows = $db->loadResultArray();
?>

    <select class="cf_inputbox validate-selection" id="select_0" size="1" {cf_multiple} name="select_0">
    <option value="">---Select---</option>

<?php
     foreach($rows as $row) 
   {
     echo"You are Here";
     print_r("<option value='$row'>$row</option>");
    }
?>
      
    </select>
    
  </div>
  <div class="clear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: ;">Sub Categeory</label>

<?php
    $db=& JFactory::getDBO();
    $query = "SELECT subcat_name From `jos_chronoforms_subcategeory_master`";
    $db->setQuery($query);
    $rows = $db->loadResultArray();
?>

    <select class="cf_inputbox validate-selection" id="select_1" size="1" {cf_multiple} name="select_1">
    <option value="">---Select---</option>

<?php
     foreach($rows as $row) 
   {
     echo"You are Here";
     print_r("<option value='$row'>$row</option>");
    }
?>
      
    </select>
    
  </div>
  <div class="clear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: ;">Districts</label>

<?php
    $db=& JFactory::getDBO();
    $query = "SELECT text_5 From `jos_chronoforms_AddressBook`";
    $db->setQuery($query);
    $rows = $db->loadResultArray();
?>

    <select class="cf_inputbox validate-selection" id="select_2" size="1" {cf_multiple} name="select_2">
    <option value="">---Select---</option>

<?php
     foreach($rows as $row) 
   {
     echo"You are Here";
     print_r("<option value='$row'>$row</option>");
    }
?>
      
    </select>
    
  </div>
  <div class="clear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: ;">City</label>

<?php
    $db=& JFactory::getDBO();
    $query = "SELECT text_7 From `jos_chronoforms_AddressBook`";
    $db->setQuery($query);
    $rows = $db->loadResultArray();
?>

    <select class="cf_inputbox validate-selection" id="select_4" size="1" {cf_multiple} name="select_4">
    <option value="">---Select---</option>

<?php
     foreach($rows as $row) 
   {
     echo"You are Here";
     print_r("<option value='$row'>$row</option>");
    }
?>
      
    </select>
    
  </div>
  <div class="clear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: ;">State</label>

<?php
    $db=& JFactory::getDBO();
    $query = "SELECT text_9 From `jos_chronoforms_AddressBook`";
    $db->setQuery($query);
    $rows = $db->loadResultArray();
?>

    <select class="cf_inputbox validate-selection" id="select_3" size="1" {cf_multiple} name="select_3">
    <option value="">---Select---</option>

<?php
     foreach($rows as $row) 
   {
     echo"You are Here";
     print_r("<option value='$row'>$row</option>");
    }
?>
      
    </select>
    
  </div>
  <div class="clear"> </div>
</div>
<div id = 'form_type'>

     <input type = "checkbox" name="checkbox" id="Name" value="Name" onclick = "alart("cheched")">Name<br>
     <input type = "checkbox" name="checkbox1" id="Address" value="Address">Address<br>  
<?php
echo "1";
echo $Address;  ?>
     <input type = "checkbox" name="checkbox2" id="Phone" value="Phone">Phone<br>    
     <input type = "checkbox" name="checkbox3" id="Email" value="Email">Email<br>
     <input type = "checkbox" name="checkbox4" id="Website" value="Website">Website<br>
     <input type = "checkbox" name="checkbox5" id=" Organization Detailes
 " value=" Organization Detailes"> Organization Detailes
 <br>
</div>
<?php
$some_fields = "";

    if($_POST['checkbox']=='Name')
      {
         $some_fields = $some_fields + text_0 + ',';
          echo "checked";
      }
      if($_POST['checkbox1']=='Address')
      {
         $some_fields = $some_fields + text_4 + ',';
      }
      if($_POST['checkbox2']=='Phone')
      {
         $some_fields = $some_fields + text_15 + ',';
      }
      if($_POST['checkbox3']=='Email')
      {
         $some_fields = $some_fields + text_13 + ',';
      }
      if($_POST['checkbox4']=='Website')
      {
         $some_fields = $some_fields + text_14  + ',';
      }


?>
<div class="form_item">
  <div class="form_element cf_button">
    <input value="Genrate CSV" type="submit"/>
  </div>
  <div class="clear"> </div>

</div>


and here the my onsubmitcode-after sending mail.
<?php
    global $mainframe;
    $db =& JFactory::getDBO();
    $tablename = 'jos_chronoforms_AddressBook';
    $tables = array( $tablename );
    $result = $db->getTableFields( $tables );
    $table_fields = array_keys($result[$tablename]);
$skip_fields = array('cf_id', 'uid', 'recordtime', 'ipaddress','cf_user_id');
$table_fields = array_diff($table_fields, $skip_fields);
    

    $db->setQuery("SELECT $some_fields FROM ".$tablename."");


    $datarows = $db->loadObjectList();

    $titcol = 0;
    foreach($table_fields as $table_field)
        {
           if($titcol){$csvline .=",";}
           $csvline .= $table_field;
           $titcol++;
        }
     $csvline .="\n";
    
     $datacol = 0;
     $rowcount = 1;
     foreach($datarows as $datarow)
        {
          foreach($table_fields as $table_field)
             {
                if($datacol){$csvline .=",";}
                $csvline .= '"'.addslashes($datarow->$table_field).'"';
                $datacol++;
              }
           $csvline .="\n";
           $datacol = 0;
           $rowcount++;
        }
           
   
   if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
      $UserBrowser = "Opera";
   }
   elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
      $UserBrowser = "IE";
   } else {
      $UserBrowser = '';
   }
   $mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';
   @ob_end_clean();
   ob_start();

   header('Content-Type: ' . $mime_type);
   header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');

   if ($UserBrowser == 'IE') {
      header('Content-Disposition: inline; filename="' . "ChronoForms - ".$tablename." - ".date("j_n_Y").'.csv"');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
   }
   else {
      header('Content-Disposition: attachment; filename="' . "ChronoForms - ".$tablename." - ".date("j_n_Y").'.csv"');
      header('Pragma: no-cache');
   }
   print $csvline;
   exit();
?>


and url of my page is...http://www.abhikalak.com/experiment/index.php?option=com_chronocontact&chronoformname=GenrateCSV

see this page,...here the 5 dropdown box and some check boxes...but only my problem is related to checkbox...here user checked the checkbox and whose checkbox is checked (suppose i checked the name,address,phone (i have a one table where all the value is saved)so genratecsv file is having only selected checkbox field data means that my csv file having only three fiels and it's data), i think u little bit understood my problem plz help me where m i wrong..??


Thanks in advance!!!
Max_admin 06 Jul, 2009
Hi Amal,

it looks like you have disabled the form and I can't see it anymore!

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
amsharma 07 Jul, 2009
hi max,

for some problem from is disable....
the new link is...http://www.abhikalak.com/experiment/index.php?option=com_chronocontact&chronoformname=GenerateCSV

but my check box problem is solved.

thanx for this max...

but in the form u see there is dropdownbox,in this case i don't know how to do this..
my problem is that the user is selected a single item from the dropdownbox in the basis of selection data is filter from the table...(suppose i selected a one item name is "noida" which is city and i check on name, adress and phone no (on checkbox ).. so csv is genrated all the person who belongs the noida and his data is name,address,phone no.)this condition is applied on all the dropdownbox simultiously at a time or single one.

i thim u little bit understood my problem....

plz help me !!!!

Thanks in advance!!!

Amal
GreyHead 07 Jul, 2009
Hi Amal,

If I understand correctly you'll need to build a WHERE clause for the SQL query
$query = "SELECT $some_fields FROM `$tablename` WHERE `text_x` = 'Noida' AND . . . ";
$db->setQuery($query);

If I remember correctly there's an example of a complex WHERE clause in the ChronoConnectivity forum. (This actually looks more like a ChronoConnectivity app than a ChronoForms one.)

Bob
amsharma 07 Jul, 2009
hi Bob,
yes we use here WHERE condition...
but whatever u write in a code it's only for a 'noida' but my requriment is that user select any city so according to the selection where condition run...means dynamicaly..

Plz tell me how to initialize and what i write a code for that...


Thanks!!

Amal
GreyHead 07 Jul, 2009
Hi Amal,

Yes, you will need to code the WHERE clause using the results from your form.
<?php
$where = array();
$city = JRequest::getString('city', '', 'post');
if ( $city ) {
  $where[] = `city` = '".$city."';
}
. . . // add more where clauses here 
$where = "WHERE ".explode(' AND ', $where);
$query = "SELECT $some_fields FROM `$tablename` $where ";
. . .

Bob
amsharma 08 Jul, 2009
Hi Bob,

Here city is the fields name..m i right...
but in this code error is showing in the line
$where[] = `city` = '".$city."';


and i write the code
<?php    
     
$where = array();
$select_4 = JRequest::getString('text_7', '', 'post');
if ( $select_4 ) {
  $where[] = `text_7`.'".$select_4."';//here use i '.' sign instead of '=' then error is not showing but also not giving desirable output...
}
$where = "WHERE ".explode(' AND ', $where);
?>


tell me where m i wrong?

help me plzz...

Thanks!!!

Amal
GreyHead 08 Jul, 2009
Hi Amal,

You are missing some quotes:
$where[] = "`city` = '".$city."'";

Bob
amsharma 09 Jul, 2009
Hi Bob,
The above code is runninng fine means that not giving error but not getting desirable result..The code is below...

<?php

$where = array();
$select_0 = JRequest::getString('select_0', '', 'post');
echo $select_0;
echo '<br>';
if ( $select_0 ) {
  $where[] = `select_0` = '".$select_0."';
}
echo $where;
echo '<br>';
$where = "WHERE ".explode('AND', $where);
echo $where;
echo '<br>';
?>


but in this line
if ( $city ) {
  $where[] = `city` = '".$city."';
}


is not working properly ...means selected value not getting here....

plz tell me where the fault..

Thanks

Amal
GreyHead 09 Jul, 2009
Hi amal,

The line with $where[] in it is not correctly quoted - there need to be double quotes "" around the string, please see my last post.

Bob
amsharma 09 Jul, 2009
Hi Bob,

Sorry for doing silly mistake...
i told u finally my code is..

<?php
$where = array("");
$select_0 = JRequest::getString('select_0', '', 'post');
echo $select_0;
echo '<br>';
if ( select_0 ) {
  $where[] = "`select_0` = '".$select_0."'";
}
echo $where;
echo '<br>';
if ( !empty($where) ) {
echo "WHERE ".explode(' AND ', $where);
echo $where;
echo '<br>';
global $mainframe;
    $mainframe->enqueuemessage("WHERE ".implode(' AND ', $where));
echo $where;
}

echo '<br>';
?>


and i m getting output is....

Manager(selected value)
Array
WHERE ArrayArray
Array


Plz tell me i m in problem for that..

Thanks !
Amal
GreyHead 11 Jul, 2009
Hi Amal,

My turn for a silly mistake,

Please replace 'explode' with 'implode' I always get them the wrong way round 😶

Let's see how that works.

Bob
This topic is locked and no more replies can be posted.