gpt4 book ai didi

php - 提交按钮呈现在表数据下。我只想要一个表单提交按钮

转载 作者:行者123 更新时间:2023-11-28 02:53:01 26 4
gpt4 key购买 nike

我的问题是在每行表格数据下呈现一个提交按钮。我在整个网络上进行了搜索,以了解如何编写代码以仅对整个表单使用一次提交。实际上,我只能为一行提交 $_POST,而我需要提交整个表单进行处理。

这是我的代码:

<?php
require("config.inc.php");
session_start();
$usrname = $_POST["uname"]; //variable passed from validateuser.html
global $usrname;
//echo $usrname;

// initial query
$query = "SELECT * FROM dfd_usr_profiles WHERE username= '".$usrname."'";
/*$query2 = "UPDATE dfd_usr_profiles SET filters = :fltrs,
regions = :regns
WHERE $usrname = :username"; */

//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}

$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Profile Information Available!";
$response["posts"] = array();

foreach ($rows as $row) {

$post = array();
$post["userID"] = $row["userID"];
$post["username"] = $row["username"];
$post["filters"] = $row["filters"];
$post["regions"] = $row["regions"];



//update our repsonse JSON data
array_push($response["posts"], $post);

$endi = count($post);
//echo $endi;


?>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<body>
<form name="updatefilters" action="update_action.php" method="post" enctype="multipart/form-data>" id="update">
<!-- <input type="submit" name="submit" id="update" value="Update" /> -->
<fieldset>
<table border="1" >
<legend>Update Filters and Regions</legend>
<tr>
<td><input type="checkbox" name="pID[]" value="<?php echo $post['userID']; ?>" onclick="getRow(this)" /></td>
<td><input type="input" name="uname" value="<?php echo $usrname;?>" /></td>
<td><?php echo $post['filters']; ?></td>
<td><label valign="top" for="" id="mfiltervals">Select Deal Type(s) you want:</label></p></td>
<td><?php require("dtypelist.php");?></td>
<td><?php echo $post['regions']; ?></td>
<td><label valign="top" for="" id="mregionvals">Select Region(s) you want:</label></td>
<td><?php require("regionslist.php");?></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
<?php
echo '<button type="submit" name="submitupdate" form="update">Update</button>';
}

// echoing JSON response
// echo json_encode($response); // Commented out: Displays profile unformatted data. TC 070516.


} else {
$response["success"] = 0;
$response["message"] = "No information for this Username is available!";
die(json_encode($response));
}
// session_start(); place-holder
?>

请帮忙。

最佳答案

您需要移动 foreach以及您在下面的代码中拥有的提交按钮。所有其他代码与此更改无关。我已经用 // **** Move 标记了需要移动的 3 个部分。 :

// **** Move this block to just before the `<tr>` tag
foreach ($rows as $row) {
$post = array();
$post["userID"] = $row["userID"];
$post["username"] = $row["username"];
$post["filters"] = $row["filters"];
$post["regions"] = $row["regions"];

//update our repsonse JSON data
array_push($response["posts"], $post);

$endi = count($post);
//echo $endi;
?>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<body>
<form name="updatefilters" action="update_action.php" method="post" enctype="multipart/form-data>" id="update">
<!-- <input type="submit" name="submit" id="update" value="Update" /> -->
<fieldset>
<table border="1" >
<legend>Update Filters and Regions</legend>
<tr>
<td><input type="checkbox" name="pID[]" value="<?php echo $post['userID']; ?>" onclick="getRow(this)" /></td>
<td><input type="input" name="uname" value="<?php echo $usrname;?>" /></td>
<td><?php echo $post['filters']; ?></td>
<td><label valign="top" for="" id="mfiltervals">Select Deal Type(s) you want:</label></p></td>
<td><?php require("dtypelist.php");?></td>
<td><?php echo $post['regions']; ?></td>
<td><label valign="top" for="" id="mregionvals">Select Region(s) you want:</label></td>
<td><?php require("regionslist.php");?></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
<?php // **** Move this up to just after the `</table>` tag
echo '<button type="submit" name="submitupdate" form="update">Update</button>';
// **** Move this closing brace just after the `</tr>` tag
}

3 步之后,它看起来如下 -- 参见 ****MOVED**** 的评论:

?>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<body>
<form name="updatefilters" action="update_action.php" method="post" enctype="multipart/form-data>" id="update">
<!-- <input type="submit" name="submit" id="update" value="Update" /> -->
<fieldset>
<table border="1" >
<legend>Update Filters and Regions</legend>
<?php // ****MOVED****
foreach ($rows as $row) {
$post = array();
$post["userID"] = $row["userID"];
$post["username"] = $row["username"];
$post["filters"] = $row["filters"];
$post["regions"] = $row["regions"];

//update our repsonse JSON data
array_push($response["posts"], $post);

$endi = count($post);
//echo $endi;
?>

<tr>
<td><input type="checkbox" name="pID[]" value="<?php echo $post['userID']; ?>" onclick="getRow(this)" /></td>
<td><input type="input" name="uname" value="<?php echo $usrname;?>" /></td>
<td><?php echo $post['filters']; ?></td>
<td><label valign="top" for="" id="mfiltervals">Select Deal Type(s) you want:</label></p></td>
<td><?php require("dtypelist.php");?></td>
<td><?php echo $post['regions']; ?></td>
<td><label valign="top" for="" id="mregionvals">Select Region(s) you want:</label></td>
<td><?php require("regionslist.php");?></td>
</tr>
<?php // ****MOVED****
}
?>
</table>
<?php // ****MOVED****
echo '<button type="submit" name="submitupdate" form="update">Update</button>';
?>
</fieldset>
</form>
</body>
</html>
<?php

确保添加 <?php?>必要时在 PHP 和正常输出之间正确切换。

关于php - 提交按钮呈现在表数据下。我只想要一个表单提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com