Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: CHEBOX CHECKED MYSQL
Forum PHP.pl > Forum > PHP
diamondking
Chcę na stronie zrobić pole zainteresowania i ma być kilkanaście checboxów np. sport, muzyka, film...

Jak teraz pobrać zaznaczone checboxy, wysłac do bazy a po odświeżeniu pobrać je i zaznaczyć te, które były wybrane ?
rad11
  1. <html>
  2. <head></head>
  3. <body>
  4.  
  5. <?php
  6. $inserted = false;
  7. if(isset($_POST['submit'])){
  8. $inserted = false;
  9. if(isset($_POST['muzyka'])){
  10. $_POST['muzyka'] = 1;
  11. }else{
  12. $_POST['muzyka'] = 0;
  13. }
  14.  
  15. if(isset($_POST['sport'])){
  16. $_POST['sport'] = 1;
  17. }else{
  18. $_POST['sport'] = 0;
  19. }
  20.  
  21. if(isset($_POST['film'])){
  22. $_POST['film'] = 1;
  23. }else{
  24. $_POST['film'] = 0;
  25. }
  26.  
  27. //tutaj robisz dodanie do bazy $_POST['muzyka'], $_POST['sport'], $_POST['film'] i inserted zwracasz po dodaniu rekordu
  28. $inserted = true;
  29.  
  30. }
  31. ?>
  32. <?php if($inserted == false): ?>
  33. <form action="index.php" method="POST">
  34. <input type="checkbox" name="muzyka"/>
  35. <input type="checkbox" name="sport"/>
  36. <input type="checkbox" name="film"/>
  37. <input type="submit" name="submit"/>
  38. </form>
  39. <?php else: ?>
  40. //tutaj pobierasz dane z bazy w petli ja przedstawilem Ci to poprosut na tablicy POST
  41. <form action="index.php" method="POST">
  42. <?php foreach($_POST as $k => $v): ?>
  43. <?php if($k != 'submit'): ?>// tutaj to jest gdyz w poscie przychodzi tez wartosc butonu ale to Ciebie nie dotyczy bo Ty masz zamiar pobrac dane z bazy
  44. <input type="checkbox" name="<?php echo $k ?>" <?php if($v == 1): echo 'checked=checked'; else: echo ''; endif; ?> />
  45. <?php endif; ?>
  46. <?php endforeach; ?>
  47. </form>
  48. <?php endif; ?>
  49. </body>
  50. </html>
diamondking
Chce mieć wszystkie dane w jednym polu ... znalazłem rozwiązanie - testuje je wink.gif

  1. <?php
  2. include_once("config.php"); //include your db config file
  3. db_connect();
  4. extract($_POST);
  5. $zapytanie=mysql_query("select test from users");
  6. $ile=mysql_num_rows($zapytanie);
  7. $zawartosc=mysql_fetch_assoc($zapytanie);
  8. $wybrane=explode(',',$zawartosc['test']);
  9.  
  10. if(isset($submit) && $_POST['test']>= 1)
  11. {
  12. $checked = implode(",",$_POST['test']);
  13.  
  14. //update
  15. mysql_query("UPDATE users SET test='".$checked."' WHERE user_id='1'");
  16.  
  17.  
  18.  
  19. } else{
  20. //update
  21. mysql_query("UPDATE users SET test='' WHERE user_id='1'");
  22.  
  23. }
  24.  
  25. ?>
  26.  
  27. <form method="post" action="">
  28. <input type="checkbox" name="test[]" value="1" <?php if(in_array(1,$wybrane)){echo "checked";}?>><label>1</label><br>
  29. <input type="checkbox" name="test[]" value="2" <?php if(in_array(2,$wybrane)){echo "checked";}?>><label>2</label><br>
  30. <input type="checkbox" name="test[]" value="3" <?php if(in_array(3,$wybrane)){echo "checked";}?>><label>3</label><br>
  31. <input type="checkbox" name="test[]" value="4" <?php if(in_array(4,$wybrane)){echo "checked";}?>><label>4</label><br>
  32. <input type="checkbox" name="test[]" value="5" <?php if(in_array(5,$wybrane)){echo "checked";}?>><label>5</label><br>
  33. <input type="checkbox" name="test[]" value="6" <?php if(in_array(6,$wybrane)){echo "checked";}?>><label>6</label><br>
  34. <input type="checkbox" name="test[]" value="7" <?php if(in_array(7,$wybrane)){echo "checked";}?>><label>7</label><br>
  35. <input type="checkbox" name="test[]" value="8" <?php if(in_array(8,$wybrane)){echo "checked";}?>><label>8</label><br>
  36. <input type="submit" name="submit" value="submit">
  37. </form>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.