0
私は、ページが読み込まれるとすぐにデータベースのアーティストにテーブルを取り込むことになっている次のコードを持っています。アーティストが検索されると、それに合ったアーティストが表示されます。存在しない場合は、「アーティストが見つかりませんでした!」と表示されます。 (それはうまく動作します)。問題は、新しいテーブルやメッセージを元のテーブルの隣に置くので、元のテーブルを削除するJavaScriptを追加してから、PHPが新しいテーブルやメッセージをエコーする前に、それがうまくいかないことです。JavaScriptでHTMLテーブルを削除する
<table id="artistTable">
<!-- PHP to display default table of all artists -->
<?php
include 'connection.php';
$stmt = $conn->prepare("SELECT * FROM artist");
if(!$stmt)
{
echo "Error creating SQL statement";
return 1;
}
$stmt->execute();
$stmt->bind_result($artID, $artName);
echo "<tr>\n
<th>Artist ID:</th>\n
<th>Artist Name:</th>\n
<th>Options:</th>\n
</tr>\n";
while($stmt->fetch())
{
echo "<tr>\n
<td>" . htmlspecialchars($artID) . "</td>\n
<td>" . htmlspecialchars($artName) . "</td>\n
<td><a href=\"\">Edit</a> - <a href=\"\">Albums</a></td>\n
</tr>\n";
}
?>
<!-- PHP to display table after search -->
<?php
include 'connection.php';
if(isset($_GET["artists"]))
{
/* JavaScript to remove original table */
echo "<script>
var table = Document.getElementByID(\"artistTable\");
table.parentNode.removeChild;
</script>";
$artist = htmlspecialchars($_GET["artists"]);
/* Add wildcards to $artist */
$artist = "%$artist%";
$stmt = $conn->prepare("SELECT * FROM artist WHERE (artName LIKE ?)");
if(!$stmt)
{
echo "Error creating SQL statement";
return 1;
}
$stmt->bind_param('s', $artist);
$stmt->execute();
$stmt->bind_result($artID, $artName);
if($stmt->fetch())
{
echo "<tr>\n
<th>Artist ID:</th>\n
<th>Artist Name:</th>\n
<th>Options:</th>\n
</tr>\n";
while($stmt->fetch())
{
echo "<tr>\n
<td>" . htmlspecialchars($artID) . "</td>\n
<td>" . htmlspecialchars($artName) . "</td>\n
<td><a href=\"\">Edit</a> - <a href=\"\">Albums</a></td>\n
</tr>\n";
}
}
else
echo "<p>No artists were found!</p>\n";
}
?>
</table>
ページをリロードして、テーブルを表示しないでください(PHP経由で) –
あなたのデフォルトテーブルを 'if(isset($ _ GET ["アーティストの 'else {}' "]))' – Sean
@Dagon Ahありがとう私は、検索があったときにページがリロードされたことに気付かなかった。 –