Connecting Tech Pros Worldwide Help | Site Map

automatically checking a checkbox

anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#1: Aug 21 '09
hi, i've written a javascript code to automatically check a checkbox if a value is true. I am trying to incorporate this into php/mysql, so when a certain cell within a db value/length is greater than 0 the checkbox is ticked.

the code i have for this is below but php doesn't accept the coding. how can i write this so that if the length of the fields are greater than 0 it will run the javascript code? or can the same thing be done with php?

Expand|Select|Wrap|Line Numbers
  1. $paySQL = "SELECT payment01,payment02 FROM savedTemps WHERE tempID = '{$random_digit}'";
  2. $payRES = mysql_query( $paySQL ) or die( mysql_error );
  3.  
  4. if ( mysql_fetch_lengths( $payRES ) > 0 )
  5. {
  6.  
  7. <script>
  8. //accesses the button called "test"
  9. document.example.test.checked=true
  10. </script>
  11.  
  12. }
  13.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Aug 21 '09

re: automatically checking a checkbox


Something like this:

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT `col1`, `col2` FROM `tbl`";
  2. $res = mysql_result($sql);
  3. // $len is an array of the fields returned by the query ($sql), that is, 'col1' and 'col2'
  4. $len = mysql_fetch_lengths($res);
  5.  
  6. // if 'col1' has a length > 1
  7. if($len[0] > 1) {
  8.     // code
  9. }
  10.  
  11. // if 'col2' has a length > 1
  12. if($len[1] > 1) {
  13.     // code
  14. }
  15.  
You'll have to adapt your code to use the method above.

Mark.
anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#3: Aug 21 '09

re: automatically checking a checkbox


thanks markus ill test this now and post my results
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: Aug 21 '09

re: automatically checking a checkbox


Quote:

Originally Posted by anfetienne View Post

thanks markus ill test this now and post my results

Okay.

Mark.
anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#5: Aug 23 '09

re: automatically checking a checkbox


ok this is how i've adapted my coding to get this to work but mysql_fetch_lengths is coming up as an error

here is the error message

Expand|Select|Wrap|Line Numbers
  1. $psql = "SELECT * FROM savedTemps WHERE tempID = '{$random_digit}'";
  2. $pres = mysql_query( $psql ) or die( mysql_error );
  3.  
  4. if ( mysql_num_rows( $pres ) > 0 ){
  5.  
  6. $prow = mysql_fetch_array( $pres );
  7.  
  8. // if 'col1' has a length > 1
  9. if( mysql_fetch_lengths ( $prow['payment01'] ) > 1) {
  10. include("payment01a.php");
  11. }
  12. else{
  13. include("payment01b.php");
  14. }
  15. // if 'col2' has a length > 1
  16. if( mysql_fetch_lengths ( $prow['payment02'] ) > 1) {
  17. include("payment02a.php");
  18. }
  19. else{
  20. include("payment02b.php");
  21. }
  22. }
  23.  
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#6: Aug 23 '09

re: automatically checking a checkbox


I don't see an error message? ;)
anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#7: Aug 23 '09

re: automatically checking a checkbox


Warning: mysql_fetch_lengths(): supplied argument is not a valid MySQL result resource in /home/theau10/public_html/resources/templateEdit.php on line 273

Warning: mysql_fetch_lengths(): supplied argument is not a valid MySQL result resource in /home/theau10/public_html/resources/templateEdit.php on line 282
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#8: Aug 23 '09

re: automatically checking a checkbox


As I showed previously, mysql_fetch_lengths() expects a resource returned by mysql_query().

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * FROM `tbl`;";
  2. $res = mysql_query($sql);
  3.  
  4. // This
  5. $lengths = mysql_fetch_lengths($res);
  6.  
  7. // Not this:
  8. $arr = mysql_fetch_array($res);
  9. $lengths = mysql_fetch_lengths($arr['col1']);
  10.  
anfetienne's Avatar
Needs Regular Fix
 
Join Date: Feb 2009
Location: UK
Posts: 356
#9: Aug 23 '09

re: automatically checking a checkbox


i've solved my problem, here is my final code....thanks for the suggestion markus!

Expand|Select|Wrap|Line Numbers
  1. <?
  2. mysql_connect($hostname,$username,$password);
  3. @mysql_select_db($database) or die( "Unable to select database");
  4.  
  5. $paysql = "SELECT * FROM savedTemps WHERE tempID = '{$random_digit}'";
  6. $payres = mysql_query( $paysql ) or die( mysql_error );
  7.  
  8. if ( mysql_num_rows( $payres ) > 0 ){
  9.  
  10. $result1 = mysql_query("SELECT payment01, payment02 FROM savedTemps WHERE tempID = '{$random_digit}'");
  11. if (!$result1) {
  12.     echo 'Could not run query: ' . mysql_error();
  13.  
  14. }
  15. $payResults = mysql_fetch_assoc($result1);
  16. $lengths = mysql_fetch_lengths($result1);
  17.  
  18. // if 'col1' has a length > 1
  19.     if( $lengths[0] > 1) {
  20.         echo '<script type="text/javascript">';
  21.         echo 'document.form1.payment01.checked=true';
  22.         echo '</script>';
  23.  
  24.         echo '<script type="text/javascript">';
  25.         echo 'document.form1.payment02.checked=true';
  26.         echo '</script>';
  27.     }
  28.  
  29.     else{
  30.         echo '<script type="text/javascript">';
  31.         echo 'document.form1.payment01.checked=false';
  32.         echo '</script>';
  33.  
  34.         echo '<script type="text/javascript">';
  35.         echo 'document.form1.payment02.checked=false';
  36.         echo '</script>';
  37.     }
  38. }
  39. mysql_close();
  40. ?>
  41.  
Reply