Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] wartość z sesji w polu wartości formularza
dentopolis
post
Post #1





Grupa: Zarejestrowani
Postów: 252
Pomógł: 0
Dołączył: 14.08.2016

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


pola input formularza mają wartości domyślne.chciałbym aby po wpisaniu jednej litery np."x" w polu input wartość została zapisana dla tej osoby odwiedzającej stronę (inna osoba mogła wpisywać inne wartości). próbowałem znaleźć odpowiedź w sieci ale doszedłem do tego momentu:
ten kod działa na tej stronie: przykład
  1. <?php>
  2. <?php>
  3. ?>
  4. <!doctype html>
  5. <html lang="pl-PL">
  6.  
  7.  
  8. </head>
  9.  
  10. <body>
  11.  
  12. <?php
  13. /*
  14. if (isset($_GET['submit'])) {
  15. session_start();
  16. $_SESSION['rightuppereight'] = $_GET['rightuppereight'];
  17. echo $_GET['rightuppereight'];
  18. $_SESSION['rightuppereight'] = $_GET['rightuppereight'];
  19. echo $_GET['rightuppereight'];
  20.  }
  21. */
  22.  
  23. if(isset($_GET["submit"])){
  24. $_SESSION['rightuppereight']=$_GET['rightuppereight'];
  25. $rightuppereight=$_SESSION['rightuppereight'];
  26. echo "Hi " . $_GET["rightuppereight"];
  27. } else {
  28. $rightuppereight='0';
  29. echo "Welcome Guest!";
  30. }
  31.  
  32.  
  33.  
  34. //$_SESSION['rightupperseven']=$_GET['rightupperseven'];
  35. //$rightupperseven=$_SESSION['rightupperseven'];
  36.  
  37.  
  38. ?>
  39.  
  40.  
  41.  
  42. <?php
  43. ###################################################################
  44. ###################### POCZĄTEK DIAGRAMU ############################
  45. ###################################################################
  46. echo "<form action='' method='get'>";
  47. echo "<table border='0' width='100%' class='table-bordered' align='center' cellspacing='0'>";
  48.  
  49. echo "<tr align='center'>";
  50.  
  51. echo "<td><input type='text' style='text-align:center;' name='rightuppereight' value='$rightuppereight' size='1'></td>";
  52. echo "<td><input type='text' style='text-align:center;' name='rightupperseven' size='1' value='$rightupperseven'></td>";
  53. echo "<td><input type='text' style='text-align:center;' name='16' size='1' value='$rightuppersix'></td>";
  54. echo "<td><input type='text' style='text-align:center;' name='15' size='1' value='$rightupperfive'></td>";
  55. echo "<td><input type='text' style='text-align:center;' name='14' size='1' value='$rightupperfour'></td>";
  56. echo "<td><input type='text' style='text-align:center;' name='13' size='1' value='$rightupperthree'></td>";
  57. echo "<td><input type='text' style='text-align:center;' name='12' size='1' value='$rightuppertwo'></td>";
  58. echo "<td><input type='text' style='text-align:center;' name='11' size='1' value='$rightupperone'></td>";
  59.  
  60. echo "<td style='background:navy;'> </td>";
  61.  
  62. echo "<td><input type='text' style='text-align:center;' name='21' size='1' value='$leftupperone'></td>";
  63. echo "<td><input type='text' style='text-align:center;' name='22' size='1' value='$leftuppertwo'></td>";
  64. echo "<td><input type='text' style='text-align:center;' name='23' size='1' value='$leftupperthree'></td>";
  65. echo "<td><input type='text' style='text-align:center;' name='24' size='1' value='$leftupperfour'></td>";
  66. echo "<td><input type='text' style='text-align:center;' name='25' size='1' value='$leftupperfive'></td>";
  67. echo "<td><input type='text' style='text-align:center;' name='26' size='1' value='$leftuppersix'></td>";
  68. echo "<td><input type='text' style='text-align:center;' name='27' size='1' value='$leftupperseven'></td>";
  69. echo "<td><input type='text' style='text-align:center;' name='28' size='1' value='$leftuppereight'></td>";
  70. echo "</tr>";
  71.  
  72.  
  73.  
  74. #start obrazki górny łuk
  75. echo "<tr align='center'>";
  76. echo "<td>";
  77.  
  78. if ($rightuppereight == 'x') {
  79. echo "<img src='gfx/x.png' style='width:50px;'>";
  80. }
  81. elseif ($rightuppereight == 'w') {
  82. echo "<img src='gfx/w.png' style='width:50px;'>";
  83. }
  84. elseif ($rightuppereight == 'c') {
  85. echo "<img src='gfx/c.png' style='width:50px;'>";
  86. }
  87. elseif ($rightuppereight == 'o') {
  88. echo "<img src='gfx/o.png' style='width:50px;'>";
  89. }
  90. elseif ($rightuppereight == 'e') {
  91. echo "<img src='gfx/e.png' style='width:50px;'>";
  92. }
  93. elseif ($rightuppereight == 'i') {
  94. echo "<img src='gfx/i.png' style='width:50px;'>";
  95. }
  96. elseif ($rightuppereight == 'v') {
  97. echo "<img src='gfx/v.png' style='width:50px;'>";
  98. }
  99. elseif ($rightuppereight == '-') {
  100. echo "";
  101. }
  102. elseif ($rightuppereight == '') {
  103. echo "<img src='gfx/tooth.jpg' width='50px'>";
  104. }
  105.  
  106. else {
  107. echo "<img src='gfx/tooth.jpg' width='50px'>";
  108. }
  109. echo "</td>";
  110. echo "</tr>";
  111.  
  112. echo "</table>";
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. echo " <input type='submit' value='submit'>";
  124. echo "</form>";
  125.  
  126.  
  127.  


Ten post edytował dentopolis 10.10.2019, 19:15:45
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dublinka
post
Post #2





Grupa: Zarejestrowani
Postów: 594
Pomógł: 66
Dołączył: 22.02.2008
Skąd: Dublin

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


Cytat(dentopolis @ 11.10.2019, 12:40:33 ) *
  1. <input type='submit' name='submit' value='submit'>";


dane wpisane w polu na stronie http://dentopolis.org/test/ są odczytywane na stronie http://dentopolis.org/test/dokumentacja.php
ale chciałbym aby te wartości wypełniały też pola input


czyli użytkownik wchodząc na stronę powinien pewne pola mieć domyślnie wypełnione jakimiś wartościami, a potem je móc modyfikować. Jeśli rozumiem to pole input powinno mieć value (wartość ) z sesji
  1. echo "<td><input type='text' style='text-align:center;' name='rightuppereight' value='$rightuppereight' size='1'></td>";

Jesli zaczniesz czytac ze zrozumieniem to tak bedzie.
Pole submit nie ma nazwy. Warunek ktory zacytowalem sprawdza najpierw czy te pole ma wartosc ale to sie robi odwolujac do nazwy. Submit bo tak sobie ktos w tym warunku nazwal nie znaczy ze odwolujesz sie do typu pola. Musisz dodac nazwe. W tym momencie jest to 'submit'.

Ten przyklad jest troche bez sensu bo najpierw sprawdzasz (na chlopski rozum) czy wcisneles submit a co za tym idzie formularz zostal wyslany. Tyle ze samo wyslanie formularza nie jest jednoznaczne z tym ze wypelniles pola a w tym momencie warunek brzmi:

"Jesli zostal wyslany form to zarejestrowac sesje" Tyle ze nie ma wartosci z pola wiec co tu rejestrowac.

To powinno ygladac mniej wiecej tak:

  1. if(isset($_POST["submit"])){
  2. $one = $_POST['rightuppereight'];
  3. if(!empty(trim($one)))
  4. {
  5. $rightuppereight = $_SESSION['rightuppereight'] = $_POST['rightuppereight'];
  6. echo "Hi " . $rightuppereight;
  7. }else{
  8. $rightuppereight = null;
  9. echo "Welcome Guest!";
  10. }
  11. } else {
  12. $rightuppereight = null;
  13. echo "Welcome Guest!";
  14. }


Wtedy jesli pole nie jest puste czyli ma wartosc wtedy nastepuje rejestracja konkretnej sesji. Jesli pole nie ozstalo
wypelnione to wysw sie informacja 'Welcome ...'

Nie uzywal metody get lecz post.

Mozna uzyc jednej sesji i zapisac od niej wartosci wszystkich pól bo sesja to tez tablica.


  1.  
  2. <?php
  3.  
  4.  
  5. if(isset($_POST['submit']))
  6. {
  7.  
  8. if(!empty($_POST['pole']))
  9. {
  10.  
  11. $session = $_SESSION['all'] = $_POST['pole'];
  12. $_SESSION['reg'] = 'Witaj costam';
  13. header('location:' . $_SERVER['SCRIPT_NAME']);
  14.  
  15. }else {
  16.  
  17. echo 'Welcome guest!';
  18.  
  19. }
  20.  
  21. }
  22.  
  23.  
  24.  
  25. echo "<form action='' method='post'>";
  26. echo "<table border='0' width='100%' class='table-bordered' align='center' cellspacing='0'>";
  27.  
  28. if(isset($_POST['del']))
  29. {
  30.  
  31. if(!empty($_SESSION['all']))
  32. {
  33.  
  34. unset($_SESSION['all']);
  35. unset($_SESSION['reg']);
  36. $_SESSION = array();
  37. }
  38.  
  39. }
  40.  
  41. if(!empty($_SESSION['all']))
  42. {
  43.  
  44. while(list(,$val) = each($_SESSION['all']))
  45. {
  46.  
  47. echo "<tr><td>pole: <input type='text' name='pole[]' value='$val'></td></tr>";
  48.  
  49. }
  50. echo $_SESSION['reg'];
  51.  
  52. }else {
  53.  
  54. $ile_pol = 7;
  55. for($i=0; $i<$ile_pol; $i++)
  56. {
  57.  
  58. echo "<tr><td>pole: <input type='text' name='pole[]'></td></tr>";
  59.  
  60. }
  61.  
  62. }
  63.  
  64.  
  65.  
  66.  
  67. echo "</table>";
  68.  
  69. echo " <input type='submit' value='submit' name=\"submit\"> <button type='submit' value=1 name='del'>Usuń sesje</button>";
  70. echo "</form>";
  71.  
  72.  
  73.  
  74.  


Ten post edytował dublinka 12.10.2019, 09:48:26
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: 16.10.2025 - 16:20