Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][MySQL]Jak obsłużyć taki formularz?, Nie, nie taki zwykły...
mlawnik
post
Post #1





Grupa: Zarejestrowani
Postów: 455
Pomógł: 49
Dołączył: 12.04.2010

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


Witam,

Zrobiłem sobie formularz z dodawaniem pól po kliknięciu na przycisk z obsługą:
  1. <?php
  2.  
  3. require_once '../inc/config.inc.php';//konfiguracja
  4.  
  5. require_once "../lang/$lang.lang.php";//plik językowy
  6.  
  7. require_once '../inc/allfunctions.inc.php';
  8.  
  9. require_once '../inc/db.config.php';
  10.  
  11. $PHP_SELF = getnamefile();
  12.  
  13. if (!($conn=mysql_connect($db_hostname, $db_username, $db_password))) {
  14. print("$LANG[db_connect_error]");
  15. error_log("$LANG[db_connect_error]", 3, "../log/db.log");
  16. exit;
  17. }
  18.  
  19. if (!($db=mysql_select_db($db_name,$conn))) {
  20. print("$LANG[db_select_error]");
  21. error_log("$LANG[db_select_error]", 3, "../log/db.log");
  22. exit;
  23. }
  24.  
  25. $FORM = <<<FOTM
  26. <head>
  27. <script type="text/javascript">
  28. <!--
  29. function dodaj_element(kontener){
  30. var znacznik = document.createElement('input');
  31. znacznik.setAttribute('type', 'text');
  32. znacznik.setAttribute('name', 'answer');
  33. znacznik.className = 'input';
  34. var kontener = document.getElementById(kontener);
  35. kontener.appendChild(znacznik);
  36. }
  37. //-->
  38. </script>
  39. <style type="text/css">
  40. <!--
  41. input.input { display: block; }
  42. -->
  43. </style>
  44. </head>
  45. <body>
  46. <form action="$PHP_SELF" method="post">
  47. Lvl:<input type="text" name="lvl_id" /><br>
  48. Question:<br>
  49. <textarea name="question" rows="5" cols="20">
  50.  
  51. </textarea><br>
  52. <div id="answers">
  53. Answers:<br><input type="text" name="answer" class="input"/>
  54. </div>
  55. <div>
  56. <input type="button" value="Dodaj odpowiedź" onclick="dodaj_element('answers');" />
  57. </div>
  58. <input type="hidden" name="IsSent" value="Yes" />
  59. <button type="submit">Wyślij</button>
  60. </form>
  61. </body
  62. FOTM;
  63.  
  64. If(IsSet($_POST['IsSent']) && $_POST['IsSent'] == 'Yes'){
  65. If (IsSet($_POST['lvl_id']) && IsSet($_POST['answer']) && IsSet($_POST['question'])){
  66. $lvl_id = mysql_real_escape_string($_POST['lvl_id']);
  67. $answer = mysql_real_escape_string($_POST['answer']);
  68. $question = mysql_real_escape_string($_POST['question']);
  69.  
  70. $query1 = "INSERT INTO Answers (ID, ID_lvl, Answer) VALUES('NULL', '$lvl_id', '$answer');";
  71. $query2 = "INSERT INTO Levels (ID, ID_lvl, Question) VALUES('NULL', '$lvl_id','$question');";
  72.  
  73. If (!($result=mysql_query($query1))) {
  74. $error = mysql_error();
  75. print("$error");
  76. error_log("$error/n", 3, "../log/db.log");
  77. exit;
  78. }
  79. else{
  80. echo "$LANG[db_query_success]";
  81. echo "<form action=\"$PHP_SELF\" method=\"post\">
  82. <button type=\"submit\">Z powrotem</button>
  83. </form>";
  84. }
  85. If (!($result=mysql_query($query2))) {
  86. $error = mysql_error();
  87. print("$error");
  88. error_log("$error/n", 3, "../log/db.log");
  89. exit;
  90. }
  91. else{
  92. echo "$LANG[db_query_success]";
  93. echo "<form action=\"$PHP_SELF\" method=\"post\">
  94. <button type=\"submit\">Z powrotem</button>
  95. </form>";
  96. }
  97. }
  98. }
  99. else {
  100. echo "$FORM";
  101. }
  102. ?>


Problemem jest to, że obsługuje tylko pierwsze pole niestety.
Jak to zrobić, aby obsłużył wszystkie pola?

Podgląd:
http://www.stw.net23.net/googleme_experime...ions.insert.php
Pozdrawiam,
mlawnik

Ten post edytował mlawnik 22.06.2010, 14:51:11
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: 23.12.2025 - 15:29