By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

how to obtain checkbox values that are in associate array form ?

P: 25
my checkbox values are in a associate values
MY AIM IS:
I want to get the check box value and refresh the selected item from a database;
THE problem:
My php scripts is not able to fetch chk[] values from form via POST


Expand|Select|Wrap|Line Numbers
  1.  <input type="checkbox" name="chk[]" value="<?=$results_array[$i]['ID'];?>">
  2.  

part of my code
-- FORM PART + query
Expand|Select|Wrap|Line Numbers
  1. <table border=1>
  2.  
  3. <tr>
  4. <th>ID</th>
  5. <th>ICAO</th>
  6. <th>QNH</th>
  7. <th>METAR</th>
  8. <th>
  9.  
  10. <form method="post" action="">
  11.  
  12. <input type="submit" name="refsel" value="Refresh selected">
  13.  
  14. </th>
  15.  
  16.  
  17.  
  18.  
  19.  
  20. </tr>
  21. <? 
  22. // disaply metar from DB 
  23. $selectsql= "SELECT* FROM vatjpn";
  24. $select=mysql_query($selectsql);
  25. $counter=0;
  26. $i=0;
  27. $results_array=array();
  28.  
  29.     //while($results=mysql_fetch_array($select)){
  30.         while($results = mysql_fetch_assoc($select)){
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. //$ID=$results['ID'];
  38. //$results_array[$i]['ICAO']=$results['ICAO'];
  39. //$results_array[$i]['QNH']=$results['QNH'];
  40. //$results_array[$i]['METAR']=$results['METAR'];
  41. $results_array[$i]=$results;
  42. //print_r($results_array);
  43. //echo "<BR> I=". $i;
  44. echo "<BR> RESLULT ARRAY LAST ITEM  ".$results_array[$i]['ID'];
  45.       $counter++;
  46.       if(!is_null($results)){
  47.         //echo "<BR> RESULTS IS ".$results=mysql_fetch_array($select);
  48.         $i++;
  49.  
  50.  
  51.  
  52.  
  53.       ?>
  54.  
  55.  <tr>
  56.  <td>
  57. <? echo $results['ID'];?>
  58.   </td>
  59.  <td>
  60.  
  61.   <? echo $results['ICAO'];?>
  62.   </td>
  63.  
  64.  <td>
  65.  
  66.  <? echo $results['QNH']; ?>
  67.   </td>
  68.  
  69.  <td>
  70.  
  71.  <? echo $results['METAR']; ?>
  72.  </td>
  73.  <td>
  74.  
  75.  
  76. <?echo $results['ID'];?>
  77. <?echo "current results array is ".$results_array[$i]['ID'];?>
  78. <?echo "i is" .$i;?>
  79. <input type="hidden" name="chkicao[]" value="<?=$results_array[$i]['ICAO'];?>">
  80. <input type="hidden" name="chkid[]" value="<?=$results_array[$i]['ID'];?>">
  81.  <input type="checkbox" name="chk[]" value="<?=$results_array[$i]['ID'];?>">
  82. </form>
  83.  

--THE PART my PHP FETCHES THE CHECKBOX VALUES
Expand|Select|Wrap|Line Numbers
  1. // if checkbox is ticked and refresh select button is pressed
  2.  
  3.     if(isset($_POST['refsel'])){
  4.         echo "<BR>refresh selected";
  5.         $chk=$_POST['chk'];
  6.         $chki=implode(",",$chk);
  7.  
  8.         print_r($_POST);
  9.         print_r($chki);
  10.  
  11.         foreach($chk as $value){
  12.             echo "<BR>";
  13.             echo "chk val".$value;
  14.  
  15.         }
  16. echo "<BR>";
  17.         echo "chki is ".$chki;
  18.  
  19.  
  20.         }
  21.  
  22.  
I found that if I put $results_array[0]['ID']
in the values of the check box, the value can be obtain, but once I change the 0 to $i, it does not grab anything

THE results of print_r($_POST) with regards to
$results_array[$i]['ID'] in the checkbox value
are is follows
Array ( [refsel] => Refresh selected [chkicao] => Array ( [0] => ) [chkid] => Array ( [0] => ) [chk] => Array ( [0] => ) )


note: I have attached a photo how how it looks
the results array last item refers to the loop
$result[0]['ID'] and so on which is normal

The 95 current results in the results selected row
is $result value

the $i you see in the same row is printed correctly
but it would not fetch the value of
$results_array[$i]['ID']

Any advices are appreciated
Attached Images
File Type: jpg 20120507php-chkbox.jpg (29.5 KB, 149 views)
May 7 '12 #1

✓ answered by issactang

I found out an interesting part, is that
It only gives out the first value of my record
that is 95 which can be found in the attachment for the illustration
after struggling
I found out that the form which includes my BUTTON-refresh selected
cannot put in the table
once i put it outside the table it worked

Expand|Select|Wrap|Line Numbers
  1. <p align="right"><form method="post" action="">
  2. <input type="submit" name="refreshall" value="Refresh ALL" align="left">
  3. <input type="submit" name="deleteall" value="Delete All" align="left">
  4. <input type="submit" name="refsel" value="Refresh selected"></p>
  5.  
  6. <div style=" border: 3px solid black;">
  7. <td><tr>
  8. METAR</tr>
  9.  
  10.  
  11.  
  12.  
  13.  
  14. <table border=1>
  15.  
  16. <tr>
  17. <th>ID</th>
  18. <th>ICAO</th>
  19. <th>QNH</th>
  20. <th>METAR</th>
  21.  
  22. </tr>
  23.  
  24.  
  25. <? php goes here and bla...
  26.  
the check box is here
Expand|Select|Wrap|Line Numbers
  1. <td>
  2. <?echo "current results array is ".$results_array[$key]['ID'];?>
  3.  
  4. <?echo $key;?>
  5.  
  6.  <input type="checkbox" name="chkbox[]" value="<?echo $results_array[$key]['ID'];?>" >
  7. </form>
  8.  
  9.  </td>
  10.  
Just for consolidation of my knowledge, can anyone suggest or explain why I cannot put the button inside a table in <th>
but outside ?
thanks ?

Share this Question
Share on Google+
1 Reply


P: 25
I found out an interesting part, is that
It only gives out the first value of my record
that is 95 which can be found in the attachment for the illustration
after struggling
I found out that the form which includes my BUTTON-refresh selected
cannot put in the table
once i put it outside the table it worked

Expand|Select|Wrap|Line Numbers
  1. <p align="right"><form method="post" action="">
  2. <input type="submit" name="refreshall" value="Refresh ALL" align="left">
  3. <input type="submit" name="deleteall" value="Delete All" align="left">
  4. <input type="submit" name="refsel" value="Refresh selected"></p>
  5.  
  6. <div style=" border: 3px solid black;">
  7. <td><tr>
  8. METAR</tr>
  9.  
  10.  
  11.  
  12.  
  13.  
  14. <table border=1>
  15.  
  16. <tr>
  17. <th>ID</th>
  18. <th>ICAO</th>
  19. <th>QNH</th>
  20. <th>METAR</th>
  21.  
  22. </tr>
  23.  
  24.  
  25. <? php goes here and bla...
  26.  
the check box is here
Expand|Select|Wrap|Line Numbers
  1. <td>
  2. <?echo "current results array is ".$results_array[$key]['ID'];?>
  3.  
  4. <?echo $key;?>
  5.  
  6.  <input type="checkbox" name="chkbox[]" value="<?echo $results_array[$key]['ID'];?>" >
  7. </form>
  8.  
  9.  </td>
  10.  
Just for consolidation of my knowledge, can anyone suggest or explain why I cannot put the button inside a table in <th>
but outside ?
thanks ?
May 8 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.