提交表格数据下的按钮渲染。我只想要一个表单的提交按钮

时间:2016-07-24 07:20:50

标签: php html forms

我的问题是在表格数据的每一行下呈现提交按钮。我已经在网上搜索了一下,我怎么可以编码只使用一个提交整个表单。因为它是我只能提交$_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
?>

请帮忙。

1 个答案:

答案 0 :(得分:0)

您需要移动下面代码中的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和正常输出之间正确切换。