简体   繁体   中英

Why are my HTML tags disappearing?

I am creating a web site with three components. (I'll be adding JQuery and CSS, but for now I'm just trying to solve some basic mechanics)

My structure is :

  • an HTML file for the presentation layer.
  • a JAVASCRIPT file for client-side processing
  • a PHP file for server side processing.

Below is my code:

html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


    <script type="text/javascript" src="RipListQuery.js"></script>
    <title>CD Riplist Query</title>
</head>
<body onload="getArtists()">
    <h1>CD Riplist Query</h1>
    <div id="divArtists" >

    </div>

</body>
</html>

Javascript:

function getArtists() {
xmlhttp = new XMLHttpRequest();


xmlhttp.open("GET","dataAccess.php",false);
xmlhttp.send();

document.getElementById("divArtists").innerHTML=xmlhttp.responseText;
}

php:

<?php

$con = mysql_connect('myServerAddress', 'ainsworthremote', 'myPassword');
if (!$con) {
    die("Could not connect: " . mysql_error());

}

$response = 
 "<table border='1' id='Artists'><thead><tr><th>Artist</th><th>Albums</th></tr></thead><tbody>";

mysql_select_db("ainsworthremote", $con);

$sql="SELECT artist, COUNT(*) AS albums FROM CD_RipList GROUP BY artist ORDER BY artist ";

$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
    $response = $response . "<tr>" .
    "<td>" . $row['artist'] . "</td>" .
    "<td>" . $row['albums'] . "</td>" .
    "</tr>";
}
$response = $response . "</tbody></table>";

mysql_close($con);

echo $response;

?>

Instead of getting a table filled with artist names as expected, I get the following:

CD Riplist Query

ArtistAlbums"; mysql_select_db("ainsworthremote", $con); $sql="SELECT artist, COUNT(*) AS albums FROM CD_RipList GROUP BY artist ORDER BY artist "; $result=mysql_query($sql); while ($row = mysql_fetch_array($result)) { $response = $response . "" . "" . $row['artist'] . "" . "" . $row['albums'] . "" . ""; } $response = $response . ""; mysql_close($con); echo $response; ?> 

What I want to know (besides what I'm doing wrong) is: What happened to my tags?

Your while statement doesn't make sense, and even if it did you should have the equals '==' operator not '='. You are returning an array from the SQL request, so you need to iterate over that array. Also, when appending to and existing str variable you can use the ".=" operator, an example would be

$response .= "</tbody></table>";

And your while loop could be a foreach loop instead.

$query = mysql_query($sql);
$result = mysql_fetch_array($result);

foreach($result as $row){
    $response .= "<tr>" .
    "<td>" . $row['artist'] . "</td>" .
    "<td>" . $row['albums'] . "</td>" .
    "</tr>";
}

$response .= "</tbody></table>";
echo $response;

See if that helps. On top of that you may have some quote escaping issues, though it looks ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM