英文:
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
<?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><div class=wpmd>
<div align=center><font size=2>".$row['crime_desc']."</font></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 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:
<?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><div class=wpmd>
<div align=center><font size=2>".$row['crime_desc']."</font></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.
答案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.php
和delete_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.php
、delete_page.php
和original_page.php
。
通过这些修改,当用户从下拉菜单中选择“Modify”时,他们将被重定向到具有相应case_number
的edit_page.php
。类似地,选择“Delete”将重定向用户到具有适当case_number
的delete_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:
<select name=dropdown id=dropdown onchange="handleSelection(this)">
<option>Please Select</option>
<option value="modify">Modify</option>
<option value="delete">Delete</option>
</select>
B. Add a JavaScript function to handle the selection and redirect the user to the appropriate PHP page:
<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. 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['case_number']
and perform the necessary database operations.
For example, in edit_page.php
:
<?php
$case_number = $_GET['case_number'];
// 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's modifications
// Redirect the user back to the original page
header('Location: original_page.php');
exit;
?>
And in delete_page.php
:
<?php
$case_number = $_GET['case_number'];
// Delete the data for the specified case_number from the database
// Redirect the user back to the original page
header('Location: original_page.php');
exit;
?>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论