如果我通過while循環從數據庫調用不可區分的記錄數量,並且每個記錄都帶有一個名爲submit按鈕的記錄,那我該如何獲取每個提交按鈕只能響應其各自的迭代記錄?視覺的什麼,我想實現:如何在PHP中爲多個提交按鈕提供一個唯一的自動化變量
在這背後的邏輯,每個「聯繫人」按鈕鏈接到其相鄰的輪廓。所以,如果我點擊「莉莉」旁邊的「聯繫人」按鈕,莉莉的名字會傳遞到下一頁。然而,到目前爲止,我遇到了這一點:
凡不管是哪個「聯繫人」按鈕我按下,最後一次迭代的名字被傳遞。在這種情況下,即使我點擊了「Lily」的聯繫人按鈕,最後一次迭代的名稱「Jane」也會被傳遞到下一頁。
這裏是我堅持的代碼:
list.php的:
<?php
session_start();
require_once("./inc/connect.inc.php");
if(!isset($_SESSION["email_login"])) {
header("location: index.php");
}
else {
}
?>
<?php
if ($searchST) {
while ($row = $searchST->fetch_assoc()) {
echo '<form action="list.php" method="POST">';
echo '<div class="z">';
echo '<div class="x">';
echo '<div class="y">';
echo '<div class="text_info">';
echo "<div id='input_titles'>C1</div><textarea readonly name='c1' id='a' rows='3' cols='54' maxlength='162' onCopy='return false' onDrag='return false' onDrop='return false' onPaste='return false' autocomplete='off'>".$row["c1"]."</textarea><br />";
echo "<div id='input_titles'>C2</div><textarea readonly name='c2' id='a' rows='3' cols='54' maxlength='162' onCopy='return false' onDrag='return false' onDrop='return false' onPaste='return false' autocomplete='off'>".$row["c2"]."</textarea><br />";
echo "<div id='input_titles'>C3</div><textarea readonly name='c3' id='a' rows='15' cols='54' maxlength='810' onCopy='return false' onDrag='return false' onDrop='return false' onPaste='return false' autocomplete='off'>".$row["c3"]."</textarea><br />";
echo "<div id='input_titles'>C4</div><textarea readonly name='c4' id='a' rows='2' cols='54' maxlength='108' onCopy='return false' onDrag='return false' onDrop='return false' onPaste='return false' autocomplete='off'>".$row["c4"]."</textarea>";
echo '</div>';
echo '<div class="visual_info">';
echo '<div class="p_pic_image">';
echo "<img src='".$row["pic"]."' id='p_pic'> ";
echo '</div>';
echo '<div class="country_and_name">';
echo "<textarea readonly name='country' id='country' rows='1' cols='14'>".$row["country"]."</textarea>";
echo " ".$row["first_name"];
echo '</div>';
echo '</div>';
echo '<form action="network.php" method="POST">';
echo '<input type="submit" name="message" id="message" value="Message">';
echo '</form>';
echo '</div>';
echo '</div>';
echo '</div>';
if (isset($_POST['message'])) {
$carry = "SELECT `first_name` FROM `users`";
$carryST = $con->query($carry);
if($carryST) {
while ($row = $carryST->fetch_assoc()) {
$_SESSION['to_name'] = $row["first_name"];
$_SESSION['to_id'] = $row["id"];
}
}
$carryST->close();
header("location: compose.php");
exit();
}
echo '</form>';
}
}
$searchST->close();
} else {
}
一個給定的配置文件的名稱應該是通過(compose.php)頁面:
<html>
<head>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Compose</title>
<link rel="stylesheet" type="text/css" href="./css/compose.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
</html>
<body>
<div class="wrapper">
<header>
<?php echo $_SESSION["to_name"]; ?>
<?php echo $_SESSION["to_id"]; ?>
<div class = "prep_stmt">Your message to <?php $_SESSION["to_name"]; ?>...</div>
</header>
<div id="back_to_inbox">
<a href="mail.php"/>inbox</a>
</div>
<div class="msg_input">
<textarea id="textbox" name="body" rows="28" cols="96" maxlength="2604"></textarea>
</div>
<script>
$(document).delegate('#textbox', 'keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start)
+ "\t"
+ $(this).val().substring(end));
// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});
</script>
<div class="send_button">
<input type="submit" value="send" />
</div>
更新:我覺得對我來說,每個記錄出來的循環將是我以某種方式結合不同的檢索值的方法:
for ($i = 0; $i < j; $i ++)
或東西的那種。我的問題的來源是因爲會話變量,每個循環都得到更新(這就是爲什麼最高的ID繼續前進)。
UPDATE2:相反,我在上面思維方法,我已經研究過將攜載周圍的提交按鈕標籤下的每個輸出的id的隱藏輸入值的另一種可能性,然後接收頁,設置條件從那裏查詢:
echo '<form action="network.php" method="POST">';
echo '<input type="hidden" name="id" value="'.$row['id'].'" />';
echo '<input type="submit" name="message" id="message" value="Message">';
echo '</form>';
和接收頁面包含:
<?php
if(isset($_POST['id']) && is_numeric($_POST['id']))
{
// run query to get first_name where the submitted record id matches
$toCD = "SELECT `first_name` FROM `users` WHERE `id` = intval($id)";
$toST = $con->query($toCD);
// make sure a row was return
if($toST->num_rows === 1)
{
$row = $toST->fetch_assoc();
echo "<div class = 'prep_stmt'>Your message to".$_row["first_name"]."...</div>";
}
else
{
// record id does not exist. Output error message
}
}
?>
注意,這些都增加。原始代碼中的其他內容大致相同。但是,我仍然沒有得到輸出。沒有任何內容顯示在接收頁面上。
這就是我闡述的,但我需要一個解決方案好夥計 –
好的。在你的while循環中。您需要創建一個具有相同action attr的表單,因爲當您提交表單時,瀏覽器只會考慮姓氏文本字段(您可能會用相同的名稱覆蓋文本字段)。試試這個: while(.. row ..... Fetch){
'; } 對不起,我在我的手機 – peter你可以在你的數據庫表中使用唯一標識符,你可以用它來查找每個用戶 – peter