Advertisement
MadhujitB

Drop Down List Issue In PHP

May 18th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. **1st File: DropDown.php**
  2. <!DOCTYPE html>
  3. <html>
  4. <body>
  5.  
  6. <?php
  7.  
  8. $servername = "localhost";
  9. $username = "root";
  10. $password = "";
  11. $dbname = "movies";
  12.  
  13. // Create connection
  14. $conn = new mysqli($servername, $username, $password, $dbname);
  15.  
  16. // Check connection
  17. if ($conn->connect_error)
  18. {
  19. die("Connection failed: " . $conn->connect_error);
  20. }
  21.  
  22. echo "<form method=\"post\" action = \"DropDownList.php\">";
  23.  
  24.  
  25.  
  26. $actlist = "SELECT AID, ANAME FROM actors";
  27. $aresult = mysqli_query($conn, $actlist);
  28.  
  29. echo "<select id=\"dd\" name=\"ddl\">";
  30.  
  31. if (mysqli_num_rows($aresult) > 0)
  32. {
  33.  
  34. echo "<option value = \" \" selected>ACTORS</option>";
  35.  
  36. while($ares = mysqli_fetch_assoc($aresult))
  37. {
  38.  
  39. $act = $ares["ANAME"];
  40. $aid = $ares["AID"];
  41.  
  42. echo "<option value = ".$aid.">".$aid."&nbsp;".$act. "</option>";
  43.  
  44. //Below you can see the session variable, I used that to pass $aid of selected item from this file to another php file, //though the value got transferred but only the last item in the list is getting transferred even if I select the other item. //That's the problem I am facing
  45. /*session_start();
  46. $_SESSION['seVar'] = $aid;
  47. session_write_close();
  48. */
  49.  
  50. }
  51. }
  52.  
  53. else
  54. {
  55. echo "No rows";
  56. }
  57. echo "</select>";
  58. echo $aid;
  59. echo "<input type=\"submit\" value=\"Submit\"></input>";
  60. echo "</form>";
  61. ?>
  62.  
  63. </body>
  64. </html>
  65.  
  66. **2nd File: DropDownList.php**
  67. <?php
  68.  
  69. $servername = "localhost";
  70. $username = "root";
  71. $password = "";
  72. $dbname = "movies";
  73. $onclk =$_POST["$aid"];
  74.  
  75.  
  76. if(isset($onclk))
  77. {
  78. echo $onclk; //Here I tried to receive the selected value from DropDown.php file, but unable to do so.
  79. }
  80.  
  81.  
  82. /*/ Create connection
  83. $conn = new mysqli($servername, $username, $password, $dbname);
  84.  
  85. // Check connection
  86. if ($conn->connect_error)
  87. {
  88. die("Connection failed: " . $conn->connect_error);
  89. }
  90.  
  91.  
  92. $actlist = "SELECT AID FROM actors";
  93. $aresult = mysqli_query($conn, $actlist);
  94.  
  95.  
  96.  
  97. session_start();
  98.  
  99.  
  100. $x=0;
  101.  
  102.  
  103.  
  104.  
  105. $act = $_SESSION['seVar'];
  106.  
  107. echo $act; //It is displaying the last item of the list, doesn't matter which item I select from the drop down list.
  108.  
  109. session_write_close();
  110.  
  111.  
  112. mysqli_close($conn);
  113. */
  114.  
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement