ユーザーのジャーナル ページに表示されるこのコードのチャンクがあります。エントリを追加することも、ページに表示されたエントリを削除することもできます。
いくつかのコメントを付けてコードを表示し、問題を説明します。
// Figures out how many recent posts to display
$posts = $config_journalposts + 1;
if($noposts!=1) {
// Gets the data from the query
while(($row = mysql_fetch_array($journalquery)) && ($posts > 1)) {
// For each of the posts that were gathered, display the following:
echo '<table border="0" width="100%">
<tr>
<td colspan="2" style="vertical-align:bottom;">
// Display the title as a link to be used as a permalink
<a href="?id='.$row['id'].'"><p class="fontheader">'.$row['title'].'</p></a>
</td>
</tr>
<tr>
// Show the o-so-important content
<td width="100%" style="vertical-align:top;padding-left:10px;">
'.$row['content'].'
</td>
</tr>
<tr>
// Show the date
<td style="font-size:8pt;padding-top:10px;">'.$row['date_day'].'/'.$row['date_month'].'/'.$row['date_year'].'</td>';
// Checks if the current user is the owner of the journal or an admin
if($_SESSION['user']==$pageowner || $_SESSION['user_rank']=='Admin') {
echo '<td align="right">
// FOCUS POINT
<form method="POST" id="deljournal">
<input type=\'hidden\' name=\'delete_id\' value=\''.$row['id'].'\' />
// A delete button that executes a bit of Javascript
<button type="button" class="button" name="delete" value="Delete" onClick="delete_journal()" />Delete</button>
</form>
// END FOCUS POINT
</td>';
}
echo '</tr>
</table>
<hr>
';
$posts --;
}
ボタンを押すとトリガーされるJavascriptは次のとおりです
function delete_journal() {
var answer = confirm("Are you sure you want to delete this journal entry?")
if (answer){
// Submits the form
$("#deljournal").submit()
}
}
この JavaScript は、上記の PHP コードでフォーラムをトリガーし、ページをリロードして、ページの一番上、タグの前でこれをトリガーします。
if(($_POST['delete_id'])) {
// Gets the post ID from the hidden forum tag
$deleteid = addslashes(strip_tags($_POST['delete_id']));
// Deletes the row that has the ID of the hidden form
mysql_query("DELETE FROM `gamezoid_accounts`.`journal_$pageowner` WHERE `id`='$deleteid'");
}
さて、問題です。while ループでは、このフォームが何度も繰り返されます。何が起こるかというと、削除ボタンを押すと、ID「deljournal」を持つフォームがトリガーされます。それらはすべて「deljournal」という ID を持っているため、ページの上部にあるものを実行します。投稿 ID をフォーム ID に埋め込もうとすると、最初に削除機能がトリガーされたことを mysql_query が認識しないため、コードが壊れます。
これを回避する方法はありますか?
私が Javascript をトリガーとして使用している理由は、誰かが尋ねた場合の確認ポップアップのためです。
とにかく、ここまで読んでくれてありがとう!