1

次のようなフォーム内に動的リストを生成する PHP コードがあります。リストはデータベースから動的に構築されることに注意してください。

echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}

私が必要とするのは、ユーザーがその行のボタンをクリックして別のページに移動したときに、ユーザーの選択に対応するデータを POST することです。クエリ文字列でハイパーリンクを使用する場合、GET 要求を使用して他のページからデータを受け取り、ハイパーリンクがユーザーに表示されるときに静的であるため、問題はありません。また、POST リクエストでのみ可能なテキストボックスからユーザー入力を取得する必要があります。

単純に他のページ (checkList.php) から、さらに処理するためにこれらのデータが必要です。

$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];

フィールドを生成する while ループがあるため、ユーザーが LIST から選択した行 (行) に対応するエントリではなく、データベースから常に最後のエントリを受け取ります。

4

5 に答える 5

2

私はこれが正しいかどうかわからない

echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}

とジャバスクリプト

<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>

checkList.php について

$id=$_GET['id'];
echo $id;
于 2012-07-24T07:28:36.543 に答える
2

変数の名前をクリーンアップすることをお勧めします。これにより、コードが少なくとも何をすべきかを伝えることができます。誰かがあなたのコードを見て、あなたが達成しようとしていることを理解するのに苦労することはめったにありません:P、特に何かで助けが必要な場合;]. いくつかのことを試して、あなたがやりたいことを理解しやすくし、おそらくあなたの答えを得られるようになることを願っています.

script 内で不必要に大量の HTML をエコーし​​ないように最善を尽くすのは良いことです。そのため、まずエコーが不要な場所から削除します。

次に、処理しやすい結果を返す mysql 関数を使用します。 $user = mysqli_fetch_assoc($sql)

第三に、名前を持つフォームが実際に php のバックエンドまたはフロントエンドに対して何かを行うかどうかはわかりません。そのため、無効な HTML であるか、そうでない余分なクラストをいくつか削除します。あなたが私たちに提示したように、あなたがやろうとしていることに価値を加えないでください.

はい、コードが =P のように見えるため、データベースから何かを構築していることに「注意」します。

また、コーディングスタイルや、このようなhtmlのエコーに関するものに関して、他の回答からの推奨事項が見られないのはとても悲しいです:(。

<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
    <input type='password' name='code' value='<?php echo $user['code'] ?>' />
    <input type='hidden' name='SessionID'  value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
    <input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
于 2012-07-24T08:19:38.130 に答える
1

You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields. And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID']; I suppose it is the easiest way to solve that.

于 2012-07-24T07:15:15.267 に答える
1

次のようなことを試すことができます:

while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}

checkList.php について

 <?php 
  $num = explode('_', $_POST['SessionID']);
  $num = $num[1];
  //in $num you will get the number of row where you can perform action

 ?>
于 2012-07-24T07:20:27.053 に答える
0
$form = 1;
while($rows=mysqli_fetch_array($sql))
{

echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}
于 2012-07-24T07:57:31.887 に答える