login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Loop werkt niet goed met checkbox (Opgelost)

Offline GroundZero - 10/02/2012 10:13
Avatar van GroundZeroLid Beste,

ik weergeef een aantal verzend opties. Het werkt prima als ik de prijs aanpas, dit slaat hij ook keurig netjes op maar wanneer ik de checkbox wil aanpassen dus uit of aan vinken dan slaat hij dit niet op. Hij slaat het wel op maar als ik bijvoorbeeld A aanvink, dan slaat hij dit voor B of C op.

Kan iemand mij vertellen wat ik verkeerd doe?
Mijn code onderstaand:

  1. <h1><?=SHPMENT_H1?></h1>
  2. <h2><?=SHPMENT_H2?></h2>
  3. <br />
  4.  
  5. <?php
  6. if($_SERVER['REQUEST_METHOD'] == 'POST')
  7. {
  8. // upload a new shipment method if filled in
  9. if($_POST['name'] != '' && $_POST['price'] != '')
  10. {
  11. // upload the picture
  12. if(!move_uploaded_file($_FILES['logo']['tmp_name'], 'uploads'.DIRECTORY_SEPARATOR.$_FILES['logo']['name']))
  13. {
  14. // upload failed, give the user a message about this
  15. echo '<div class="error">'.QUERY_ERROR.'</div>';
  16. }
  17. else
  18. {
  19. // add to the database
  20. if(!mysql_query('INSERT INTO shipping_options
  21. (name, price, logo)
  22. VALUES
  23. ("'.mysql_real_escape_string($_POST['name']).'",
  24. "'.mysql_real_escape_string($_POST['price']).'",
  25. "'.mysql_real_escape_string($_FILES['logo']['name']).'")'))
  26. {
  27. echo '<div class="error">'.QUERY_ERROR.'</div>';
  28. }
  29. else
  30. {
  31. echo '<div class="success">'.USER_ADDED_QUERY.'</div>';
  32. }
  33. }
  34. }
  35.  
  36. // update the current shipment methods
  37. $i = 0;
  38. foreach($_POST['which'] as $id)
  39. {
  40. mysql_query('UPDATE shipping_options
  41. SET
  42. price = "'.mysql_real_escape_string($_POST['curprice'][$i]).'",
  43. active = "'.mysql_real_escape_string($_POST['check'][$i]).'"
  44. WHERE
  45. id = "'.mysql_real_escape_string($id).'"');
  46. $i++;
  47. }
  48. }
  49. ?>
  50.  
  51. <form name="shipmentmethods" method="post" enctype="multipart/form-data">
  52. <fieldset>
  53. <legend>Verzendmethodes</legend>
  54.  
  55. <?php
  56. $query = mysql_query('SELECT id, name, price, logo, active FROM shipping_options
  57. ORDER BY name DESC');
  58.  
  59. $rows = mysql_num_rows($query);
  60.  
  61. if($rows == 0)
  62. {
  63. echo SHPMENT_NONE;
  64. }
  65. else
  66. {
  67. // start a table
  68. echo '<table width="500px">';
  69.  
  70. // process the loop
  71. while($fetch = mysql_fetch_assoc($query))
  72. {
  73. // is the checkbox checked
  74. if($fetch['active'] == 1){
  75. $re = ' checked="checked"';
  76. } else {
  77. $re = '';
  78. }
  79.  
  80. // check if the logo exists
  81. if(file_exists('uploads'.DIRECTORY_SEPARATOR.$fetch['logo'])){
  82. echo '<tr>
  83. <td>
  84. <input type="checkbox" name="check[]" value="1" '.$re.' />
  85. </td>
  86. <td>
  87. <img src="uploads'.DIRECTORY_SEPARATOR.$fetch['logo'].'" alt="logo" />
  88. </td>
  89. <td>'.$fetch['name'].'</td>
  90. <td>
  91. &euro; <input type="text" name="curprice[]" value="'.number_format($fetch['price'], 2, '.', '.').'" />
  92. <input type="hidden" name="which[]" value="'.$fetch['id'].'" />
  93. </td>
  94. </tr>';
  95. } else {
  96. echo '<tr><td></td><td>'.$fetch['name'].'</td><td>&euro; <input type="text" name="curprice" value="'.number_format($fetch['price'], 2, ',', '.').'" /></td></tr>';
  97. }
  98. }
  99.  
  100. // end the table
  101. echo '</table>';
  102. }
  103. ?>
  104.  
  105. </fieldset>
  106.  
  107. <fieldset>
  108. <legend>Methode toevoegen</legend>
  109.  
  110. <label>Naam</label>
  111. <input type="text" name="name" class="input" />
  112. <br />
  113.  
  114. <label>Standaard kosten</label>
  115. <input type="text" name="price" class="input" />
  116. <br />
  117.  
  118. <label>Logo</label>
  119. <input type="file" name="logo" class="input" />
  120. </fieldset>
  121.  
  122. <input type="submit" name="submit" id="submit" value="<?=WRD_SAVE?>" />
  123.  
  124. </form>

1 antwoord

Gesponsorde links
Offline vinTage - 10/02/2012 13:36
Avatar van vinTage Nieuw lid in je foreach geruik je $_POST['which'], maar je wilt iets doen met de checkbox => $_POST['check']

Verder is het probleem wat lastig uitgelegd, heb je niet een VEEL simpelere 'voorbeeld' code + uitleg ? 
Gesponsorde links
Je moet ingelogd zijn om een reactie te kunnen posten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.179s