Echo Table PHP并添加了下拉列表,如何在PHP中使用下拉列表

huangapple go评论111阅读模式
英文:

Echo Table PHP and added Dropdown list, how to use the drop down in PHP

问题

I have successfully called this from a database. I want to include a combobox (dropdown) in a PHP string to echo it and form a table.

The PHP to do that is looking thus

  1. <?php
  2. require_once('inc/config.php');
  3. $con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect , Reason: '.mysqli_connect_error());
  4. $sql = "select * from crime_db_table";
  5. $result = $con->query($sql);
  6. if($result->num_rows>0){
  7. while($row = $result->fetch_assoc()){
  8. $result_table = "<TR valign=top>
  9. <TD width=127 align=center><div class=wpmd>
  10. <div align=center><font size=2>".$row['case_number']."</font></div>
  11. </div>
  12. </TD>
  13. <TD width=109 height=26 align=center><div class=wpmd>
  14. <div align=center><font size=2>".$row['crime_type']."</font></div>
  15. </div>
  16. </TD>
  17. <TD width=90 height=26 align=center><div class=wpmd>
  18. <div align=center><font size=2>".$row['fullname']."</font></div>
  19. </div>
  20. </TD>
  21. <TD width=122 height=26 align=center><div class=wpmd>
  22. <div align=center><font size=2>".$row['address']."</font></div>
  23. </div>
  24. </TD>
  25. <TD width=118 height=26 align=center><div class=wpmd>
  26. <div align=center><font size=2>".$row['telephone']."</font></div>
  27. </div>
  28. </TD>
  29. <TD width=157 height=26 align=center><div class=wpmd>
  30. <div align=center><font size=2>".$row['incident_date']."</font></div>
  31. </div>
  32. </TD>
  33. <TD width=103 height=26 align=center><div class=wpmd>
  34. <div align=center><a href=http://google.com title=''><font size=2>Image.jpg</font></a></div>
  35. </div>
  36. </TD>
  37. <TD width=138 height=26 align=center><font size=2><select name=dropdown id=dropdown><option>Please Select</option><option>Modify</option><option>Delete</option></select></font></TD>
  38. </TR>";
  39. echo $result_table;
  40. }
  41. }else{
  42. echo "<div><font face=Verdana><B>No Results Found</B></font></div>";
  43. }
  44. $con->close();
  45. ?>

Which works pretty fine, but the major challenge I have is the <select></select> which is embedded into the PHP code in this case. As you can see <select name=dropdown id=dropdown><option>Please Select</option><option>Modify</option><option>Delete</option></select> is the full dropdown.

How do I do it that when I select "Modify," it loads another PHP page where the user can edit the form and save data to the MySQL database, and when I click "Delete," it removes the data from the MySQL table.

Edit

New source code looks thus:

  1. <?php
  2. require_once('inc/config.php');
  3. $con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect , Reason: '.mysqli_connect_error());
  4. $sql = "select * from crime_db_table";
  5. $result = $con->query($sql);
  6. if($result->num_rows>0){
  7. while($row = $result->fetch_assoc()){
  8. $result_table = "<TR valign=top>
  9. <TD width=127 align=center><div class=wpmd>
  10. <div align=center><font size=2>".$row['case_number']."</font></div>
  11. </div>
  12. </TD>
  13. <TD width=109 height=26 align=center><div class=wpmd>
  14. <div align=center><font size=2>".$row['crime_type']."</font></div>
  15. </div>
  16. </TD>
  17. <TD width=90 height=26 align=center><div class=wpmd>
  18. <div align=center><font size=2>".$row['fullname']."</font></div>
  19. </div>
  20. </TD>
  21. <TD width=122 height=26 align=center><div class=wpmd>
  22. <div align=center><font size=2>".$row['address']."</font></div>
  23. </div>
  24. </TD>
  25. <TD width=118 height=26 align=center><div class=wpmd>
  26. <div align=center><font size=2>".$row['telephone']."</font></div>
  27. </div>
  28. </TD>
  29. <TD width=157 height=26 align=center><div class=wpmd>
  30. <div align=center><font size=2>".$row['incident_date']."</font></div>
  31. </div>
  32. </TD>
  33. <TD width=103 height=26 align=center><div class=wpmd>
  34. <div align=center><a href=http://google.com title=''><font size=2>Image.jpg</font></a></div>
  35. </div>
  36. </TD>
  37. <TD width=138 height=26 align=center><font size=2><select name=dropdown id=dropdown onchange='handleSelection(this)'><option>Please Select</option><option>Modify</option><option>Delete</option></select></font></TD>
  38. </TR>";
  39. echo $result_table;
  40. }
  41. }else{
  42. echo "<div><font face=Verdana><B>No Results Found</B></font></div>";
  43. }
  44. $con->close();
  45. ?>

Worse now, it does not even open the form as I created.

英文:

I have successfully called this from a database. I want to include a combobox (dropdown) in a PHP string to echo it and form a table.

The PHP to do that is looking thus

  1. &lt;?php
  2. require_once(&#39;inc/config.php&#39;);
  3. $con = mysqli_connect($host, $user, $pass, $db) or die (&#39;Cannot Connect , Reason: &#39;.mysqli_connect_error());
  4. $sql = &quot;select * from crime_db_table&quot;;
  5. $result = $con-&gt;query($sql);
  6. if($result-&gt;num_rows&gt;0){
  7. while($row = $result-&gt;fetch_assoc()){
  8. $result_table = &quot;&lt;TR valign=top&gt;
  9. &lt;TD width=127 align=center&gt;&lt;div class=wpmd&gt;
  10. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;case_number&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  11. &lt;/div&gt;
  12. &lt;/TD&gt;
  13. &lt;TD width=109 height=26 align=center&gt;&lt;div class=wpmd&gt;
  14. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_type&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  15. &lt;/div&gt;
  16. &lt;/TD&gt;
  17. &lt;TD width=90 height=26 align=center&gt;&lt;div class=wpmd&gt;
  18. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;fullname&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  19. &lt;/div&gt;
  20. &lt;/TD&gt;
  21. &lt;TD width=122 height=26 align=center&gt;&lt;div class=wpmd&gt;
  22. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;address&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  23. &lt;/div&gt;
  24. &lt;/TD&gt;
  25. &lt;TD width=118 height=26 align=center&gt;&lt;div class=wpmd&gt;
  26. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;telephone&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  27. &lt;/div&gt;
  28. &lt;/TD&gt;
  29. &lt;TD width=157 height=26 align=center&gt;&lt;div class=wpmd&gt;
  30. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;incident_date&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  31. &lt;/div&gt;
  32. &lt;/TD&gt;
  33. &lt;TD width=103 height=26 align=center&gt;&lt;div class=wpmd&gt;
  34. &lt;div align=center&gt;&lt;a href=http://google.com title=&gt;&lt;font size=2&gt;Image.jpg&lt;/font&gt;&lt;/a&gt;&lt;/div&gt;
  35. &lt;/div&gt;
  36. &lt;/TD&gt;
  37. &lt;TD width=138 height=26 align=center&gt;&lt;div class=wpmd&gt;
  38. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_desc&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  39. &lt;/div&gt;
  40. &lt;/TD&gt;
  41. &lt;TD width=138 height=26 align=center&gt;&lt;font size=2&gt;&lt;select name=dropdown id=dropdown&gt;&lt;option&gt;Please Select&lt;/option&gt;&lt;option&gt;Modify&lt;/option&gt;&lt;option&gt;Delete&lt;/option&gt;&lt;/select&gt;&lt;/font&gt;&lt;/TD&gt;
  42. &lt;/TR&gt;&quot;;
  43. echo $result_table;
  44. }
  45. }else{
  46. echo &quot;&lt;div&gt;&lt;font face=Verdana&gt;&lt;B&gt;No Results Found&lt;/B&gt;&lt;/font&gt;&lt;/div&gt;&quot;;
  47. }
  48. $con-&gt;close();
  49. ?&gt;

Which works pretty fine, but the major challenge I have is the &lt;select&gt;&lt;/select&gt; which is embedded into the PHP code in this case.
As you can see &lt;select name=dropdown id=dropdown&gt;&lt;option&gt;Please Select&lt;/option&gt;&lt;option&gt;Modify&lt;/option&gt;&lt;option&gt;Delete&lt;/option&gt;&lt;/select&gt; is the full drop down.

How do i do it that when i select modify, it loads another PHP page where the user can edit the form and save data to the MySQL database and when i click delete, it removes the data from the MySQL table.

Edit

New Source code Looks thus:

  1. &lt;?php
  2. require_once(&#39;inc/config.php&#39;);
  3. $con = mysqli_connect($host, $user, $pass, $db) or die (&#39;Cannot Connect , Reason: &#39;.mysqli_connect_error());
  4. $sql = &quot;select * from crime_db_table&quot;;
  5. $result = $con-&gt;query($sql);
  6. if($result-&gt;num_rows&gt;0){
  7. while($row = $result-&gt;fetch_assoc()){
  8. $result_table = &quot;&lt;TR valign=top&gt;
  9. &lt;TD width=127 align=center&gt;&lt;div class=wpmd&gt;
  10. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;case_number&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  11. &lt;/div&gt;
  12. &lt;/TD&gt;
  13. &lt;TD width=109 height=26 align=center&gt;&lt;div class=wpmd&gt;
  14. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_type&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  15. &lt;/div&gt;
  16. &lt;/TD&gt;
  17. &lt;TD width=90 height=26 align=center&gt;&lt;div class=wpmd&gt;
  18. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;fullname&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  19. &lt;/div&gt;
  20. &lt;/TD&gt;
  21. &lt;TD width=122 height=26 align=center&gt;&lt;div class=wpmd&gt;
  22. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;address&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  23. &lt;/div&gt;
  24. &lt;/TD&gt;
  25. &lt;TD width=118 height=26 align=center&gt;&lt;div class=wpmd&gt;
  26. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;telephone&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  27. &lt;/div&gt;
  28. &lt;/TD&gt;
  29. &lt;TD width=157 height=26 align=center&gt;&lt;div class=wpmd&gt;
  30. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;incident_date&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  31. &lt;/div&gt;
  32. &lt;/TD&gt;
  33. &lt;TD width=103 height=26 align=center&gt;&lt;div class=wpmd&gt;
  34. &lt;div align=center&gt;&lt;a href=http://google.com title=&gt;&lt;font size=2&gt;Image.jpg&lt;/font&gt;&lt;/a&gt;&lt;/div&gt;
  35. &lt;/div&gt;
  36. &lt;/TD&gt;
  37. &lt;TD width=138 height=26 align=center&gt;&lt;div class=wpmd&gt;
  38. &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_desc&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
  39. &lt;/div&gt;
  40. &lt;/TD&gt;
  41. &lt;TD width=138 height=26 align=center&gt;&lt;font size=2&gt;&lt;select name=dropdown id=dropdown onchange=&quot;handleSelection(this)&quot;&gt;&lt;option&gt;Please Select&lt;/option&gt;&lt;option&gt;Modify&lt;/option&gt;&lt;option&gt;Delete&lt;/option&gt;&lt;/select&gt;&lt;/font&gt;&lt;/TD&gt;
  42. &lt;/TR&gt;&quot;;
  43. echo $result_table;
  44. }
  45. }else{
  46. echo &quot;&lt;div&gt;&lt;font face=Verdana&gt;&lt;B&gt;No Results Found&lt;/B&gt;&lt;/font&gt;&lt;/div&gt;&quot;;
  47. }
  48. $con-&gt;close();
  49. ?&gt;

Worse now, it does not even open the form as i created.

答案1

得分: -1

为实现您描述的功能,您可以按照以下方式修改您的PHP代码:

A. 更新$result_table字符串中的<select>元素,以包括一个onchange事件:

  1. <select name=dropdown id=dropdown onchange="handleSelection(this)">
  2. <option>Please Select</option>
  3. <option value="modify">Modify</option>
  4. <option value="delete">Delete</option>
  5. </select>

B. 添加一个处理选择并将用户重定向到适当PHP页面的JavaScript函数:

  1. <script>
  2. function handleSelection(selectElement) {
  3. var selectedOption = selectElement.value;
  4. if (selectedOption === 'modify') {
  5. window.location.href = 'edit_page.php?case_number=<?php echo $row["case_number"]; ?>';
  6. } else if (selectedOption === 'delete') {
  7. window.location.href = 'delete_page.php?case_number=<?php echo $row["case_number"]; ?>';
  8. }
  9. }
  10. </script>

C. 创建两个单独的PHP页面:edit_page.phpdelete_page.php,分别处理修改和删除功能。在这些页面中,您可以使用$_GET['case_number']从URL中检索case_number值,并执行必要的数据库操作。

例如,在edit_page.php中:

  1. <?php
  2. $case_number = $_GET['case_number'];
  3. // 从数据库中检索指定case_number的数据,并预填写编辑表单
  4. // 根据用户的修改在数据库中更新数据
  5. // 将用户重定向回原始页面
  6. header('Location: original_page.php');
  7. exit;
  8. ?>

delete_page.php中:

  1. <?php
  2. $case_number = $_GET['case_number'];
  3. // 从数据库中删除指定case_number的数据
  4. // 将用户重定向回原始页面
  5. header('Location: original_page.php');
  6. exit;
  7. ?>

请确保用您的实际文件名或URL替换edit_page.phpdelete_page.phporiginal_page.php

通过这些修改,当用户从下拉菜单中选择“Modify”时,他们将被重定向到具有相应case_numberedit_page.php。类似地,选择“Delete”将重定向用户到具有适当case_numberdelete_page.php

英文:

To achieve the functionality you described, you can modify your PHP code as follows:

A. Update the select element in the $result_table string to include an onchange event:

  1. &lt;select name=dropdown id=dropdown onchange=&quot;handleSelection(this)&quot;&gt;
  2. &lt;option&gt;Please Select&lt;/option&gt;
  3. &lt;option value=&quot;modify&quot;&gt;Modify&lt;/option&gt;
  4. &lt;option value=&quot;delete&quot;&gt;Delete&lt;/option&gt;
  5. &lt;/select&gt;

B. Add a JavaScript function to handle the selection and redirect the user to the appropriate PHP page:

  1. &lt;script&gt;
  2. function handleSelection(selectElement) {
  3. var selectedOption = selectElement.value;
  4. if (selectedOption === &#39;modify&#39;) {
  5. window.location.href = &#39;edit_page.php?case_number=&lt;?php echo $row[&quot;case_number&quot;]; ?&gt;&#39;;
  6. } else if (selectedOption === &#39;delete&#39;) {
  7. window.location.href = &#39;delete_page.php?case_number=&lt;?php echo $row[&quot;case_number&quot;]; ?&gt;&#39;;
  8. }
  9. }
  10. &lt;/script&gt;

C. Create two separate PHP pages: edit_page.php and delete_page.php, to handle the modification and deletion functionality, respectively. In these pages, you can retrieve the case_number value from the URL using $_GET[&#39;case_number&#39;] and perform the necessary database operations.

For example, in edit_page.php:

  1. &lt;?php
  2. $case_number = $_GET[&#39;case_number&#39;];
  3. // Retrieve the data for the specified case_number from the database and pre-fill the form for editing
  4. // Update the data in the database based on the user&#39;s modifications
  5. // Redirect the user back to the original page
  6. header(&#39;Location: original_page.php&#39;);
  7. exit;
  8. ?&gt;

And in delete_page.php:

  1. &lt;?php
  2. $case_number = $_GET[&#39;case_number&#39;];
  3. // Delete the data for the specified case_number from the database
  4. // Redirect the user back to the original page
  5. header(&#39;Location: original_page.php&#39;);
  6. exit;
  7. ?&gt;

Make sure to replace edit_page.php, delete_page.php, and original_page.php with the actual filenames or URLs of your pages.

With these modifications, when the user selects "Modify" from the dropdown, they will be redirected to the edit_page.php with the corresponding case_number in the URL. Similarly, selecting "Delete" will redirect the user to the delete_page.php with the appropriate case_number in the URL.

huangapple
  • 本文由 发表于 2023年6月19日 00:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定