Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> skrypt JS.-zmiana wartości
BoomBox20
post
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 8.06.2011

Ostrzeżenie: (0%)
-----


Witam serdecznie,
mam oto taki problem:
skrypty poniżej ( pochodzący z http://designconcept.webdev20.pl/articles/...-edycji-danych/ ) wyświetlają po drobnych przeróbkach z mojej bazy status użytkownika czyli 1 albo 0-odpowiednio aktywny,nieaktywny.Jako że nie znam się w ogóle na js proszę o pomoc w przerobieniu tego tak aby były dwa przyciski aktywuj i deaktywuj(w zależności który potrzebny) i po kliknięciu ich podmieniało wartość w bazie na przeciwną czyli z 1 na 0 i odwrotnie.Bardzo serdecznie proszę o pomoc.pozdrawiam



index.php
  1.  
  2. <body>
  3. <?php
  4. define('DB_HOST','localhost');
  5. define('DB_USER','user');
  6. define('DB_PASS','pass');
  7. define('DB_NAME','system_newsow');
  8. $con=mysql_connect(DB_HOST,DB_USER,DB_PASS,true);
  9. $select_db = mysql_select_db(DB_NAME,$con);
  10. if($select_db)
  11. {
  12. mysql_query("SET NAMES utf8",$con);
  13. }
  14. else
  15. {
  16. die('Nie mozna sie polaczyc z baza danych ');
  17. }
  18. $query = "SELECT * FROM user";
  19. $result = mysql_query($query,$con);
  20. if($result===false && trim(mysql_error($con)))
  21. {
  22. echo("Błędne zapytanie: \n");
  23. }
  24. while($row = mysql_fetch_assoc($result))
  25. {
  26. $out[]=$row;
  27. }
  28. echo '<table id="tab">
  29. <tr><th>Id</th><th>Stasus</th><th>Treść</th><th>Akcje</th></tr>';
  30. foreach($out as $art)
  31. {
  32. echo '<tr>';
  33. echo '<td>'.$art['id'].'</td>';
  34.  
  35. if($art['status']==1){
  36. echo '<td>"aktywny"</td>';
  37. }else
  38. {
  39. echo '<td>"nieaktywny"</td>';
  40. }
  41.  
  42. //echo '<td>'.htmlspecialchars($art['status'],ENT_QUOTES).'</td>';
  43. // echo '<td>'.htmlspecialchars($art['tresc'],ENT_QUOTES).'</td>';
  44. echo '<td><form method="post"><input type="hidden" name="id" value="'.$art['id'].'" /><input class="sub" type="submit" value="Edytuj" /></form>
  45. <form method="post" action=""><input type="hidden" name="id" value="'.$art['id'].'" /><input class="del" type="submit" value="" title="Usuń" /></form></td>';
  46. echo '</tr>';
  47. }
  48. echo '</table>
  49. <form action="" method="post" id="add_form">
  50. <label>Tytuł:</label><input type="text" name="tytul" value="" /><br />
  51. <label>Treść:</label><input type="text" name="tresc" value="" /><br />
  52. <input class="sub2" type="submit" value="Dodaj" />
  53. </form>';
  54. ?>
  55. </body>
  56.  


edit.php
  1.  
  2. <?php
  3. if(isset($_POST['status']) && isset($_POST['id']))
  4. {
  5. define('DB_HOST','localhost');
  6. define('DB_USER','user');
  7. define('DB_PASS','pass');
  8. define('DB_NAME','system_newsow');
  9. $con=mysql_connect(DB_HOST,DB_USER,DB_PASS,true);
  10. $select_db = mysql_select_db(DB_NAME,$con);
  11. if($select_db)
  12. {
  13. mysql_query("SET NAMES utf8",$con);
  14. }
  15. else
  16. {
  17. die('Nie mozna sie polaczyc z baza danych ');
  18. }
  19. $tytul = mysql_real_escape_string($_POST['status']);
  20. //$tresc = mysql_real_escape_string($_POST['tresc']);
  21. $id = intval($_POST['id']);
  22. $query = "UPDATE user SET status = '$tytul'";
  23. $result = mysql_query($query,$con);
  24. echo json_encode(array('status'=>$_POST['status']));
  25. }
  26. ?>
  27.  
  28.  


custom.js
  1.  
  2. $(document).ready(function() {
  3. $('input.sub').live('click',function(){
  4. var id_art = $(this).parent().find('input[type=hidden]').val();
  5. var title = $(this).parent().parent().parent().find('td:eq(1)').text();
  6. // var content = $(this).parent().parent().parent().find('td:eq(2)').text();
  7. var a = $('<div/>', {
  8. class: 'box',
  9. html: '<form action="" method="post"><input type="hidden" name="id" value="'+id_art+'" /><label>Tytuł:</label><input type="text" name="status" value="'+title+'" /><br /><label>Treść:</label><input type="text" name="tresc" value="'+content+'" /><br /><input class="sub2" type="submit" value="Edytuj" /></form>'
  10. });
  11. var thi = $(this).parent();
  12. var next_thi = thi.next();
  13. $(this).parent().parent().append(a);
  14. $(this).parent().remove();
  15. a.find('form').submit(function(){
  16. $.post('edit.php', $(this).serialize(), function(dane) {
  17. dane = eval('('+dane+')');
  18. tr = a.parent().parent();
  19. tr.find('td:eq(1)').text(dane['status']).css('background-color', 'yellow').animate({
  20. opacity: 1
  21. }, 3000, function() {
  22. $(this).css({'background-color' : '#DFF7FF', 'opacity' : '1'});
  23. });
  24. /* tr.find('td:eq(2)').text(dane['tresc']).css('background-color', 'yellow').animate({
  25. opacity: 1
  26. }, 3000, function() {
  27. $(this).css({'background-color' : '#DFF7FF', 'opacity' : '1'});
  28. }); */
  29. a.remove();
  30. next_thi.before(thi);
  31. });
  32. return false;
  33. });
  34. return false;
  35. });
  36.  
  37.  
  38.  
  39. });


dwa buttony aktywuj albo dezaktywuj zrobię w php w pliku index.php


  1. if($art['status']==1){
  2. echo '<td><form method="post"><input type="hidden" name="id" value="'.$art['id'].'" /><input class="sub" type="submit" value="Dezaktywuj" /></form>
  3. <form method="post" action=""><input type="hidden" name="id" value="'.$art['id'].'" /><input class="del" type="submit" value="" title="Usuń" /></form></td>';
  4. }
  5. else if(($art['status']==0))
  6. {
  7. echo '<td><form method="post"><input type="hidden" name="id" value="'.$art['id'].'" /><input class="sub" type="submit" value="Aktywuj" /></form>
  8. <form method="post" action=""><input type="hidden" name="id" value="'.$art['id'].'" /><input class="del" type="submit" value="" title="Usuń" /></form></td>';
  9. }
  10.  



ale gdzie zrobić zamianę z 0 na 1 i odwrotnie i jak wysłać np później tą wartość do bazy z js.?tzn w js. bo chyba tam to trzeba zrobic zamienić 0 na 1 wysłać do edit.php i wysłać do bazy ?!tylko jak?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 17:58