2016-07-24 92 views
2

我的問題是提交按鈕在每行表格數據下呈現。我已經在網上搜索瞭解,我如何編寫代碼以僅使用整個表單的一個提交。因爲它是我只能提交$_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 
?> 

請幫助。

+0

在此先感謝您的任何和所有建議 –

回答

0

您需要移動foreach以及下面代碼中的提交按鈕。所有其他代碼與此更改無關。我已標記的3個部分需要與// **** Move移動:

// **** 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和正常輸出之間正確切換的地方使用?>

+0

謝謝Trincot。這是我正在尋找的答案.. –