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

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

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

<?php
require_once('inc/config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect , Reason: '.mysqli_connect_error());
$sql = "select * from crime_db_table";
$result = $con->query($sql);
if($result->num_rows>0){
  while($row = $result->fetch_assoc()){
    $result_table = "<TR valign=top>
    <TD width=127 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['case_number']."</font></div>
    </div>
    </TD>
    <TD width=109 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['crime_type']."</font></div>
    </div>
    </TD>
    <TD width=90 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['fullname']."</font></div>
    </div>
    </TD>
    <TD width=122 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['address']."</font></div>
    </div>
    </TD>
    <TD width=118 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['telephone']."</font></div>
    </div>
    </TD>
    <TD width=157 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['incident_date']."</font></div>
    </div>
    </TD>
    <TD width=103 height=26 align=center><div class=wpmd>
    <div align=center><a href=http://google.com title=''><font size=2>Image.jpg</font></a></div>
    </div>
    </TD>
    <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>
    </TR>";

    echo $result_table;
  }
}else{
  echo "<div><font face=Verdana><B>No Results Found</B></font></div>";
}
  $con->close();
?>

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:

<?php
require_once('inc/config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect , Reason: '.mysqli_connect_error());
$sql = "select * from crime_db_table";
$result = $con->query($sql);
if($result->num_rows>0){
  while($row = $result->fetch_assoc()){
    $result_table = "<TR valign=top>
    <TD width=127 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['case_number']."</font></div>
    </div>
    </TD>
    <TD width=109 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['crime_type']."</font></div>
    </div>
    </TD>
    <TD width=90 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['fullname']."</font></div>
    </div>
    </TD>
    <TD width=122 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['address']."</font></div>
    </div>
    </TD>
    <TD width=118 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['telephone']."</font></div>
    </div>
    </TD>
    <TD width=157 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>".$row['incident_date']."</font></div>
    </div>
    </TD>
    <TD width=103 height=26 align=center><div class=wpmd>
    <div align=center><a href=http://google.com title=''><font size=2>Image.jpg</font></a></div>
    </div>
    </TD>
    <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>
    </TR>";

    echo $result_table;
  }
}else{
  echo "<div><font face=Verdana><B>No Results Found</B></font></div>";
}
$con->close();
?>

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

&lt;?php
require_once(&#39;inc/config.php&#39;);
$con = mysqli_connect($host, $user, $pass, $db) or die (&#39;Cannot Connect , Reason: &#39;.mysqli_connect_error());
$sql = &quot;select * from crime_db_table&quot;;
$result = $con-&gt;query($sql);
if($result-&gt;num_rows&gt;0){
  while($row = $result-&gt;fetch_assoc()){
    $result_table = &quot;&lt;TR valign=top&gt;
    &lt;TD width=127 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;case_number&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=109 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_type&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=90 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;fullname&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=122 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;address&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=118 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;telephone&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=157 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;incident_date&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=103 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &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;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=138 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_desc&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &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;
    &lt;/TR&gt;&quot;;

    echo $result_table;
  }
}else{
  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;;
}
  $con-&gt;close();
?&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:

&lt;?php
require_once(&#39;inc/config.php&#39;);
$con = mysqli_connect($host, $user, $pass, $db) or die (&#39;Cannot Connect , Reason: &#39;.mysqli_connect_error());
$sql = &quot;select * from crime_db_table&quot;;
$result = $con-&gt;query($sql);
if($result-&gt;num_rows&gt;0){
  while($row = $result-&gt;fetch_assoc()){
    $result_table = &quot;&lt;TR valign=top&gt;
    &lt;TD width=127 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;case_number&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=109 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_type&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=90 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;fullname&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=122 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;address&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=118 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;telephone&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=157 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;incident_date&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=103 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &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;
    &lt;/div&gt;
    &lt;/TD&gt;
    &lt;TD width=138 height=26 align=center&gt;&lt;div class=wpmd&gt;
    &lt;div align=center&gt;&lt;font size=2&gt;&quot;.$row[&#39;crime_desc&#39;].&quot;&lt;/font&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;/TD&gt;
    &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;
    &lt;/TR&gt;&quot;;

    echo $result_table;
  }
}else{
  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;;
}
  $con-&gt;close();
?&gt;

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

答案1

得分: -1

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

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

<select name=dropdown id=dropdown onchange="handleSelection(this)">
  <option>Please Select</option>
  <option value="modify">Modify</option>
  <option value="delete">Delete</option>
</select>

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

<script>
function handleSelection(selectElement) {
  var selectedOption = selectElement.value;
  if (selectedOption === 'modify') {
    window.location.href = 'edit_page.php?case_number=<?php echo $row["case_number"]; ?>';
  } else if (selectedOption === 'delete') {
    window.location.href = 'delete_page.php?case_number=<?php echo $row["case_number"]; ?>';
  }
}
</script>

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

例如,在edit_page.php中:

<?php
$case_number = $_GET['case_number'];

// 从数据库中检索指定case_number的数据,并预填写编辑表单

// 根据用户的修改在数据库中更新数据

// 将用户重定向回原始页面
header('Location: original_page.php');
exit;
?>

delete_page.php中:

<?php
$case_number = $_GET['case_number'];

// 从数据库中删除指定case_number的数据

// 将用户重定向回原始页面
header('Location: original_page.php');
exit;
?>

请确保用您的实际文件名或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:

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

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

&lt;script&gt;
function handleSelection(selectElement) {
  var selectedOption = selectElement.value;
  if (selectedOption === &#39;modify&#39;) {
    window.location.href = &#39;edit_page.php?case_number=&lt;?php echo $row[&quot;case_number&quot;]; ?&gt;&#39;;
  } else if (selectedOption === &#39;delete&#39;) {
    window.location.href = &#39;delete_page.php?case_number=&lt;?php echo $row[&quot;case_number&quot;]; ?&gt;&#39;;
  }
}
&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:

&lt;?php
$case_number = $_GET[&#39;case_number&#39;];

// Retrieve the data for the specified case_number from the database and pre-fill the form for editing

// Update the data in the database based on the user&#39;s modifications

// Redirect the user back to the original page
header(&#39;Location: original_page.php&#39;);
exit;
?&gt;

And in delete_page.php:

&lt;?php
$case_number = $_GET[&#39;case_number&#39;];

// Delete the data for the specified case_number from the database

// Redirect the user back to the original page
header(&#39;Location: original_page.php&#39;);
exit;
?&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:

确定