2016-07-24 16 views
2

私の問題は、テーブルデータの各行の下に提出ボタンが表示されることです。私はウェブ全体を調べて、フォーム全体に対して1つのサブミットだけを使用するようにコードする方法を見つけました。それは私が処理するためにフォーム全体を提出する必要がある一方で、私は1行に対してのみ$_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

ありがとうございましたTrincotありがとうございました –

答えて

0

foreachと、下のコードにある送信ボタンを移動する必要があります。他のすべてのコードはこの変更には関係ありません。以下のように見える、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 
} 

から****MOVED****とのコメントを参照してください:私は// **** Moveで移動する必要が3部をマークしている

?> 
<!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

これは私が探していた答えです。 –