Jump to content

Javascript & PHP Question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Hello Everyone,

I have a hopefully quick question. I am trying to add a select all checkbox to the piece of code I have below. I have looked around online, and I found a bunch of scripts that are designed to check all boxes in a form. My problem is that my checkboxes are created dynamically from a while loop (after reading from a database). None of the scripts I found have worked. I was hoping that someone here could guide me in the right direction as to how to fix this problem.

Thank you in advance.

SirBee

PS. I apologize if this is in the wrong location.


<?php




include 'connect.PHP';




print "<FORM NAME='badspirit' METHOD='POST' action='badspiritconfirm.php'>";





$query = mysql_query ("SELECT * FROM badspirit WHERE approved=0");




print "<font face=verdana size=2><table align=center width=800 cellspacing=3 cellpadding=3><tr><td colspan=6 align=center><FONT size=4 face='verdana'><b>Perpetual Motion's Spirit Score Screener</td></tr>


<tr bgcolor='#ccffff'><td><font size=2><b>Date Played</b></td><td><font size=2><b>Submitting Team</b></td><td><font size=2><b>Opponent</b></td><td><font size=2><b>Spirit Score</b></td><td><font size=2><b>Submitted By</b></td><td><font size=2><b>Submission Date</b></td></tr>";


$actualCount=0;


while($array=mysql_fetch_array($query)){


    $actualCount=$actualCount+1;


	$count=$array['report_number'];


	$dd="<OPTION VALUE=''>Select</OPTION>";


	if($array['spirit_score']=='1'){$dd.="<OPTION VALUE='1' SELECTED>1</Option>";}else{$dd.="<OPTION VALUE='1'>1</Option>";}


	if($array['spirit_score']=='1.5'){$dd.="<OPTION VALUE='1.5' SELECTED>1.5</Option>";}else{$dd.="<OPTION VALUE='1.5'>1.5</Option>";}


	if($array['spirit_score']=='2'){$dd.="<OPTION VALUE='2' SELECTED>2</Option>";}else{$dd.="<OPTION VALUE='2'>2</Option>";}


	if($array['spirit_score']=='2.5'){$dd.="<OPTION VALUE='2.5' SELECTED>2.5</Option>";}else{$dd.="<OPTION VALUE='2.5'>2.5</Option>";}


	if($array['spirit_score']=='3'){$dd.="<OPTION VALUE='3' SELECTED>3</Option>";}else{$dd.="<OPTION VALUE='3'>3</Option>";}


	if($array['spirit_score']=='3.5'){$dd.="<OPTION VALUE='3.5' SELECTED>3.5</Option>";}else{$dd.="<OPTION VALUE='3.5'>3.5</Option>";}


	if($array['spirit_score']=='4'){$dd.="<OPTION VALUE='4' SELECTED>4</Option>";}else{$dd.="<OPTION VALUE='4'>4</Option>";}


	if($array['spirit_score']=='4.5'){$dd.="<OPTION VALUE='4.5' SELECTED>4.5</Option>";}else{$dd.="<OPTION VALUE='4.5'>4.5</Option>";}


	if($array['spirit_score']=='5'){$dd.="<OPTION VALUE='5' SELECTED>5</Option>";}else{$dd.="<OPTION VALUE='5'>5</Option>";}


	


	print "<input type='hidden' name='submitting_team_identifier".$count."' value='".$array['submitting_team_identifier']."'>


	<input type='hidden' name='league".$count."' value='".$array['league']."'>


	<input type='hidden' name='opponent_name".$count."' value='".$array['opponent_name']."'>


	<input type='hidden' name='opponent_identifier".$count."' value='".$array['opponent_identifier']."'>";


	


	print "<tr><td><FONT size=2>".$array['date_played']."</td><td><FONT size=2>".$array['submitting_team']."</td><td><FONT size=2>".$array['opponent_name']."</td>


	<td><FONT size=2>


	<SELECT NAME='spirit_score".$count."'>";


	print $dd;



//This is where the checkboxes I want the checkall script to check


	print "</SELECT>


	</td><td><FONT size=2>".$array['submitted_by']."</td><td><FONT size=2>".$array['date_submitted']."</td>


	<td><INPUT TYPE='checkbox' NAME='approve[]' VALUE='".$array['report_number']."'></td></tr>


	<tr><td colspan=6><FONT size=2>".$array['comment']."</td><BR><BR></tr>";

}




if($actualCount!=0){print "<tr><td colspan=6 align=center><input type='submit' name='ApproveSpirit' value='Approve'></td></tr>";}else{


print "<tr><td colspan=6 align=center><i>No spirit scores in holding</i></td></tr>";}	


print "</table>";


?>


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
See if this works: Javascript Check and Uncheck All Checkboxes

Otherwise try:
If there are no other checkboxes on the page i think you can use the JavaScript: document.getElementsByTagName('INPUT') (not sure if it's case sensitive)
This will return an array where you can loop trough and check if the attribute 'type' == 'checkbox', then set them checked.

#3
Guest_johnny.dacu_*

Guest_johnny.dacu_*
  • Guests
Or with jquery select all checkboxes like $(".my-class"), where the class you can add it from PHP script

#4
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
@oxano: I have looked at that link. I tried it, and it did not work for me. I guessed what the problem was, and ran a test. It does not work because the name of my checkbox is 'approve[]'. If I take out the brackets, it works just fine. I need approve to be an array, so I can pull multiple entries out and have a specific value for each one. (I did not write this code, so that is a guess, but I am pretty sure it is correct). I will look into using the other method you mentioned and let you know if it worked.

Thank you.

Sir Bee

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
:confused:How can the name have any effect when i search by tag name and type :/

#6
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
There is a significant possibility I am using this script wrong. The code I used is shown below.

<SCRIPT LANGUAGE="JavaScript">

<!-- 	

// by Nannette Thacker

// http://www.shiningstar.net

// This script checks and unchecks boxes on a form

// Checks and unchecks unlimited number in the group...

// Pass the Checkbox group name...

// call buttons as so:

// <input type=button name="CheckAll"   value="Check All"

	//onClick="checkAll(document.myform.list)">

// <input type=button name="UnCheckAll" value="Uncheck All"

	//onClick="uncheckAll(document.myform.list)">

// -->





function checkAll(field)

{

for (i = 0; i < field.length; i++)

	field[i].checked = true ;

}



function uncheckAll(field)

{

for (i = 0; i < field.length; i++)

	field[i].checked = false ;

}



</script>



<?php



include 'connect.PHP';



print "<FORM NAME='badspirit' METHOD='POST' action='badspiritconfirm.php'>";





$query = mysql_query ("SELECT * FROM badspirit WHERE approved=0");





print "<font face=verdana size=2><table align=center width=800 cellspacing=3 cellpadding=3><tr><td colspan=6 align=center><FONT size=4 face='verdana'><b>Perpetual Motion's Spirit Score Screener</td></tr>

<tr bgcolor='#ccffff'><td><font size=2><b>Date Played</b></td><td><font size=2><b>Submitting Team</b></td><td><font size=2><b>Opponent</b></td><td><font size=2><b>Spirit Score</b></td><td><font size=2><b>Submitted By</b></td><td><font size=2><b>Submission Date</b></td></tr>";

$actualCount=0;

while($array=mysql_fetch_array($query)){

    $actualCount=$actualCount+1;

	$count=$array['report_number'];

	$dd="<OPTION VALUE=''>Select</OPTION>";

	if($array['spirit_score']=='1'){$dd.="<OPTION VALUE='1' SELECTED>1</Option>";}else{$dd.="<OPTION VALUE='1'>1</Option>";}

	if($array['spirit_score']=='1.5'){$dd.="<OPTION VALUE='1.5' SELECTED>1.5</Option>";}else{$dd.="<OPTION VALUE='1.5'>1.5</Option>";}

	if($array['spirit_score']=='2'){$dd.="<OPTION VALUE='2' SELECTED>2</Option>";}else{$dd.="<OPTION VALUE='2'>2</Option>";}

	if($array['spirit_score']=='2.5'){$dd.="<OPTION VALUE='2.5' SELECTED>2.5</Option>";}else{$dd.="<OPTION VALUE='2.5'>2.5</Option>";}

	if($array['spirit_score']=='3'){$dd.="<OPTION VALUE='3' SELECTED>3</Option>";}else{$dd.="<OPTION VALUE='3'>3</Option>";}

	if($array['spirit_score']=='3.5'){$dd.="<OPTION VALUE='3.5' SELECTED>3.5</Option>";}else{$dd.="<OPTION VALUE='3.5'>3.5</Option>";}

	if($array['spirit_score']=='4'){$dd.="<OPTION VALUE='4' SELECTED>4</Option>";}else{$dd.="<OPTION VALUE='4'>4</Option>";}

	if($array['spirit_score']=='4.5'){$dd.="<OPTION VALUE='4.5' SELECTED>4.5</Option>";}else{$dd.="<OPTION VALUE='4.5'>4.5</Option>";}

	if($array['spirit_score']=='5'){$dd.="<OPTION VALUE='5' SELECTED>5</Option>";}else{$dd.="<OPTION VALUE='5'>5</Option>";}

	

	print "<input type='hidden' name='submitting_team_identifier".$count."' value='".$array['submitting_team_identifier']."'>

	<input type='hidden' name='league".$count."' value='".$array['league']."'>

	<input type='hidden' name='opponent_name".$count."' value='".$array['opponent_name']."'>

	<input type='hidden' name='opponent_identifier".$count."' value='".$array['opponent_identifier']."'>";

	

	print "<tr><td><FONT size=2>".$array['date_played']."</td><td><FONT size=2>".$array['submitting_team']."</td><td><FONT size=2>".$array['opponent_name']."</td>

	<td><FONT size=2>

	<SELECT NAME='spirit_score".$count."'>";

	print $dd;



//This is where the checkboxes I want the checkall script to check



	print "</SELECT>

	</td><td><FONT size=2>".$array['submitted_by']."</td><td><FONT size=2>".$array['date_submitted']."</td>

	<td><INPUT TYPE='checkbox' NAME='approve[]' VALUE='".$array['report_number']."'></td></tr>

	<tr><td colspan=6><FONT size=2>".$array['comment']."</td><BR><BR></tr>";

}


//The checkall boxes are in this if statement.  Right now the boxes are named 'approve[]' and as the code stands right now, it does not work.  For the sake of the check all boxes, if I were to 
//rename the boxes I want checked 'approve' and change the value sent to the function to 'document.badspirit.approve' then this code would work.  I am not sure where I went wrong.

if($actualCount!=0){print "<tr><td colspan=4 align=center><input type='submit' name='ApproveSpirit' value='Approve'></td><td><input type='button' name='CheckAll' value='Check All' onClick='checkAll(document.badspirit.approve[])'><input type='button' name='UnCheckAll' value='Uncheck All' onClick='uncheckAll(document.badspirit.approve[])'></td></tr>";}else{

print "<tr><td colspan=6 align=center><i>No spirit scores in holding</i></td></tr>";}	

print "</table>";

?>


#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Could you try: onClick='checkAll( document.getElementsByName( approve[]) )
I'd actually be happier with quotes around "approve[]" but since you allrdy used double and single quotes..

Perhaps you can just call checkAll with no paramater and change the method to:

function checkAll( )
{
var field = document.getElementsByName('approve[]');
for (i = 0; i < field.length; i++)
    field[i].checked = true ;
}


#8
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Thank you oxano.

Your second suggestion worked. I passed nothing, and just defined field within the function. Could you please explain to me what was not working when I tried to pass the value? From what little I know of Javascript it made sense and should have worked.

Thank you again for your prompt response and your help.

SirBee

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Well, actually i'm not really sure what was wrong :blushing:

My first thought would be that it doesn't work well with multiple tags with the same name, thus not returning an array. But even then it would probably return only the 1st element. And i assume the checkall didn't check only 1 checkbox.

My second thought is that the "document.badspirit.approve[]" isn't handled as javascript but handled as string or something..
You can try to add 'javascript' to the onclick as test:
onlick="JavaScript:uncheckAll(document.badspirit.approve[])"

I mostly put "Javascript:" in front of onclick functions when i'm working with server side code aswell to make sure it doesn't take a function from the server side instead of javascript (happened to me before with ASP :( )