Hi,
anyone can help me with getting the check box value?
this is my code.

Thank you

<HEAD>
<SCRIPT LANGUAGE="<strong class="highlight">JavaScript</strong>">

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function checkbox_checker() {
var checkBoxArr = getSelectedCheckbox(document.form1.chkid[$i]);
if (checkBoxArr.length == 0) { alert("No check boxes selected"); }
else {alert(getSelectedCheckboxValue(document.form1.chkid[$i]))}
}
</script>
</HEAD>
 <p align="center"><strong><br>
  THESE CASES  MATCH YOUR QUERY. CLICK ON THE CASE NUMBER TO VIEW THE CASES.</strong></p>
 <blockquote>
   <p>
<FORM METHOD=POST ACTION="" name=form1 type="submit" >
<?php

$con = mysql_connect( "localhost", "root", "rootroot" ); //
$db = "x";
$TABLENAME = "lesson";
$primarykey = "lessonID";
mysql_select_db( $db );

// Get the search variable from URL
$query = "select * from $TABLENAME " ;
$result = mysql_query( $query, $con );
$fields = mysql_num_fields( $result );
for ( $i = 0; $i < $fields; $i++ )
{
    $name = mysql_field_name( $result, $i );
    // echo "$name = $row[$name]<BR>\n";
    $field_names[] = $name;
    if ( !empty( $_REQUEST["q"] ) )
        $condition[] = sprintf( "`%s` like '%%%s%%'" , $name , mysql_real_escape_string( $_REQUEST["q"] ) );
    // 'like' search condition to search in this field
}

echo  "</br> Search word=".$_REQUEST["q"];
// i'm inserting this condition to the conditions array.

$query .= "WHERE verified='yes'" ;
if ( isset( $condition ) )
    $query .= " AND " . implode( ' OR ', $condition );
// echo out the code  generated..

$result_results = mysql_query( $query ) or die( mysql_error() );
$rows = mysql_num_rows($result_results); 
echo "</br> Number of searched result=".$rows;



$query_with_max=$query.$max;
$results_with_max=mysql_query($query_with_max);
//echo "</br>".$query_with_max;

if ( mysql_num_rows( $results_with_max ) > 0 )
{
    echo "<table border=\"1\">";
    echo "<tr>";
	echo "<th>Result NO</th>";
    echo "<th>Select</th>";
	$counter = 0;
    foreach( $field_names AS $field_title )
    {
        echo "<th>" . ucwords( $field_title ) . "</th>";
        if ( $counter >= 8 )
            break;
        $counter++;
    }

    echo "</tr>";
    $i = 1;
    while ( $rows_results = mysql_fetch_assoc( $results_with_max ) )
    {
		echo "<tr>";
	    echo "<td>$i.</td>";
	    echo "<td><input type=\"checkbox\" name=\"chkid[$i]\" ></td>";


        $counter = 0;
        foreach( $field_names AS $fn )
        {
            if ( $fn != $primarykey )
				echo "<td>" . $rows_results[$fn] . "</td>";

			
			else
              echo '<td><a href="related.php?lessonID=' . $rows_results[$primarykey] . '">' . $rows_results[$primarykey] . '</a></td>';
				 
			if ( $counter >= 8 )
                break;
            $counter++;
        }
        // this cell holds then an URL to another page , and the primary key's value embedded into the url by a GET values...
        $i++;
        echo "<tr>";
    }
    echo "</table>";
	
 
}

else
    echo "<br>No result in $TABLENAME table ...";

?>
     <input type="submit" name="Submit" value="Submit" onClick="checkbox_checker()">
</form>

    
 </p>
 </blockquote>
Member Avatar for rajarajan2017

buttonGroup.value [value is the property to get the value of checkbox]

thank you rajarajan
I forgot the value function.

I added this function, but nothing changed.
any help?
thank you

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function
Member Avatar for diafol

Your html looks a bit messy - no quotes around some of the attributes, e.g.

<FORM METHOD=POST ACTION="" name=form1 type="submit" >

Is the 'type' attribute really supposed to be there?

In addition, you've mixed up the php and html to an extent where it's difficult to follow. $_REQUEST really shouldn't be used any more. Use $_POST if that's what you're using.

I would suggest placing all your pHp above the DTD - it doesn't make any difference to the page, just makes the code easier to read and debug. I apologise if this sounds like a dig - it's not. There are a few things you can do to tidy it up, so there's a better chance of catching the problem.

OK:

function checkbox_checker() {
var checkBoxArr = getSelectedCheckbox(document.form1.chkid[$i]);
if (checkBoxArr.length == 0) { alert("No check boxes selected"); }
else {alert(getSelectedCheckboxValue(document.form1.chkid[$i]))}
}

This is supposed to be javascript right? So what's with the $i? I can't see how this function will pass a valid value to the getSelectedCheckbox function. Do you need to leave out the $i?

Thanks Ardav

<FORM METHOD=POST ACTION="" name=form1 type="submit" >
Is the 'type' attribute really supposed to be there?

I changed it now.

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

Initially I want each check box to be listed at the bottom when they are checked.
But later I changed, after check all related result, users need to click submit button. ( I forgot to changed the code there)

In addition, you've mixed up the php and html to an extent where it's difficult to follow. $_REQUEST really shouldn't be used any more. Use $_POST if that's what you're using.

I use $_REQUEST to pass searched value from the search page to this page.
This page displays result in table.
Previously I do not have any $_POST because I just list the result.
Now I add the check box, when users select the check boxes, the selected check boxes will be listed at the bottom of the page.

I would suggest placing all your pHp above the DTD -

tq
OK:

function checkbox_checker() {
var checkBoxArr = getSelectedCheckbox(document.form1.chkid[$i]);
if (checkBoxArr.length == 0) { alert("No check boxes selected"); }
else {alert(getSelectedCheckboxValue(document.form1.chkid[$i]))}
}

Do you need to leave out the $i?

yes thanks.

this is the updated code.
Any comment? it still not return the check box values.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<HEAD>

<SCRIPT LANGUAGE="<strong class="highlight">JavaScript</strong>">

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function checkbox_checker() {
var checkBoxArr = getSelectedCheckbox(document.form1.chkid[]);
if (checkBoxArr.length == 0) { alert("No check boxes selected"); }
else {alert(getSelectedCheckboxValue(document.form1.chkid[]))}
}
</script>
</HEAD>
 <p align="center"><strong><br>
  THESE CASES  MATCH YOUR QUERY. CLICK ON THE CASE NUMBER TO VIEW THE CASES.</strong></p>
 <blockquote>
   <p>
 <form name="form1" method="post" action="">
<?php

$con = mysql_connect( "localhost", "root", "rootroot" ); //
$db = "x";
$TABLENAME = "lesson";
$primarykey = "lessonID";
mysql_select_db( $db );

// Get the search variable from URL
$query = "select * from $TABLENAME " ;
$result = mysql_query( $query, $con );
$fields = mysql_num_fields( $result );
for ( $i = 0; $i < $fields; $i++ )
{
    $name = mysql_field_name( $result, $i );
    // echo "$name = $row[$name]<BR>\n";
    $field_names[] = $name;
    if ( !empty( $_REQUEST["q"] ) )
        $condition[] = sprintf( "`%s` like '%%%s%%'" , $name , mysql_real_escape_string( $_REQUEST["q"] ) );
    // 'like' search condition to search in this field
}

echo  "</br> Search word=".$_REQUEST["q"];
// i'm inserting this condition to the conditions array.

$query .= "WHERE verified='yes'" ;
if ( isset( $condition ) )
    $query .= " AND " . implode( ' OR ', $condition );
// echo out the code  generated..

$result_results = mysql_query( $query ) or die( mysql_error() );
$rows = mysql_num_rows($result_results); 
echo "</br> Number of searched result=".$rows;

$query_with_max=$query.$max;
$results_with_max=mysql_query($query_with_max);
//echo "</br>".$query_with_max;

if ( mysql_num_rows( $results_with_max ) > 0 )
{
    echo "<table border=\"1\">";
    echo "<tr>";
    echo "<th>Result NO</th>";
    echo "<th>Select</th>";
    $counter = 0;
    foreach( $field_names AS $field_title )
    {
        echo "<th>" . ucwords( $field_title ) . "</th>";
        if ( $counter >= 8 )
            break;
        $counter++;
    }

    echo "</tr>";
    $j = 1;
    while ( $rows_results = mysql_fetch_assoc( $results_with_max ) )
    {
        echo "<tr>";
        echo "<td>$j.</td>";
        echo "<td><input type=\"checkbox\" name=\"chkid[$i]\" ></td>";


        $counter = 0;
        foreach( $field_names AS $fn )
        {
            if ( $fn != $primarykey )
                echo "<td>" . $rows_results[$fn] . "</td>";


            else
              echo '<td><a href="related.php?lessonID=' . $rows_results[$primarykey] . '">' . $rows_results[$primarykey] . '</a></td>';

            if ( $counter >= 8 )
                break;
            $counter++;
        }
        // this cell holds then an URL to another page , and the primary key's value embedded into the url by a GET values...
        $j++;
        echo "<tr>";
    }
    echo "</table>";


}

else
    echo "<br>No result in $TABLENAME table ...";

?>
     <input type="submit" name="Submit" value="Submit" onClick="checkbox_checker()">
</form>


 </p>
 </blockquote>
Member Avatar for diafol

Sorry, I can't wade through the code. How about filling your javascript with some alert() commands to see where the js is stopping/going wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.