I need some help. I'm loading a .php file using jquery. the php is a mysql query that pulls all the cities from Alaska. The problem is that I wrote my query a certain way and it seems jquery changes it when it loads it.

state_alaska.php

include 'config.php';

include 'opendb.php';
$AK = mysql_query("SELECT id, city, state FROM city WHERE state ='AK' ORDER BY city ASC") or die(mysql_error()); 

while ( $row = mysql_fetch_array($AK) ) {
    $page = "1";
    echo  "<div class='city'><a href='catagories.php?state=".urlencode($row["state"]) ."&city=".urlencode($row["city"])." &page=".$page."'/>".$row["city"] . "</a></div>";
    }
include 'closedb.php';

Jquery load

    $('#ak').load("state_alaska.php");

Source code

<div id="ak" style="display: block; ">
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Anchorage&amp;page=1"></a>
        Anchorage
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Bethel+&amp;page=1"></a>
        Bethel 
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Fairbanks+&amp;page=1"></a>
        Fairbanks 
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Homer+&amp;page=1"></a>
        Homer
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Juneau&amp;page=1"></a>
        Juneau
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Kenai+&amp;page=1"></a>
        Kenai 
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Ketchikan+&amp;page=1"></a>
        Ketchikan 
    </div>
    <div class="city">
        <a href="catagories.php?state=AK&amp;city=Kodiak+&amp;page=1"></a>
        Kodiak 
    </div>
</div>

In the code I never asked it to display block or to close the </a>'s I dont know why its doing that.. can anyone help?

Dani AI

Generated

— the behavior you showed is the classic symptom of an empty, self‑closed anchor tag: the browser sees an <a .../> so the link contains nothing and the city name is left outside the tag as plain text. is correct that the stray slash in the opening <a> is the culprit. jQuery’s load() simply inserts whatever HTML the server returns; it does not rewrite your SQL or generate those extra slashes for you.

To avoid string‑concatenation mistakes and make the output safer and clearer, build the query string and escape the visible text before printing. For example, construct the URL with http_build_query() and escape the city name with htmlspecialchars() so HTML special characters won’t break the markup:

$url = 'catagories.php?' . http_build_query(['state'=>$row['state'],'city'=>$row['city'],'page'=>$page]);
echo '<div class="city"><a href="' . $url . '">' . htmlspecialchars($row['city'], ENT_QUOTES) . '</a></div>';

Two short, important notes: urlencode() produces + for spaces (that’s why you saw Bethel+); rawurlencode() or http_build_query() will give you percent‑encoding if you prefer. Also, the old mysql_* functions are obsolete—migrate to mysqli or PDO with prepared statements to avoid SQL injection and for compatibility with modern PHP.

Quick debugging checklist: (1) look at the raw server response in DevTools → Network → Response (don’t rely only on the DOM inspector; browsers auto‑fix broken HTML). (2) Validate the generated HTML with a validator when in doubt. (3) Trim values and watch your quote placement in concatenation. Fixing the stray slash and adopting the safer output pattern above will resolve the immediate link problem and make the code more robust.

On line 8, remove the closing slash for the a tag, that's what messes it up. The line should be:

echo  "<div class='city'><a href='catagories.php?state=" . urlencode($row["state"]) . "&city=" . urlencode($row["city"]) . "&page=" . $page . "'>" . $row["city"] . "</a></div>";

or:

echo  "<div class='city'><a href='catagories.php?state=" . urlencode($row["state"]) . "&city=" . urlencode($row["city"]) . "&page=$page'>{$row['city']}</a></div>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.