Annie Png Annie Png - 5 months ago 7
SQL Question

JQuery to GET the link with # only

For some reason , my variable $startwith is only getting home.php instead of the links that startswith '#'.(eg.href = "#A')...
This is my JQuery Form

<ul>
<li><a href="home.php">Home</a></li>
<li class="dropdown"><a href="authors.php">Authors</a>
<div class="dropdown-content">
<a href="viewAuthor.php">View Author Profile</a>
</li>
<li class="dropdown"><a href="books.php">Books</a>
<div class="dropdown-content">
<a href="viewBookList.php">View Books</a>
</div>
</li>

<li class="dropdown"><a href="publishers.php">Publishers</a>
<div class="dropdown-content">
<a href="publishers.php">View Publishers</a>
<a href="addPublisher.php">Add Publisher</a>
</div>
</li>

<li class="dropdown"><a href="genres.php">Genres</a>
<div class="dropdown-content">
<a href="genres.php">View Genres</a>
<a href="addGenre.php">Add Genre</a>
</div>
</li>
<li><a href="signout.php">Sign Out</a>
<br>
</ul>

<br><br>
<script>
$(function () {
$("a[href^='#']").on('click', function () {

var sW = $('a').attr('href').replace("#", "");

$.get("getpublisher.php", {startsWith: sW})
.done(function (data) {
$('#listData').html(data);
});

});
});
</script>
<div align="center">
<a href="#A">A | </a>
<a href="#B">B | </a>
<a href="#C">C | </a>
<a href="#D">D | </a>
<a href="#E">E | </a>
<a href="#F">F | </a>
<a href="#G">G | </a>
<a href="#H">H | </a>
<a href="#I">I | </a>
<a href="#J">J | </a>
<a href="#K">K | </a>
<a href="#L">L | </a>
<a href="#M">M | </a>
<a href="#N">N | </a>
<a href="#O">O | </a>
<a href="#P">P | </a>
<a href="#Q">Q | </a>
<a href="#R">R | </a>
<a href="#S">S | </a>
<a href="#T">T | </a>
<a href="#U">U | </a>
<a href="#V">V | </a>
<a href="#W">W | </a>
<a href="#X">X | </a>
<a href="#Y">Y | </a>
<a href="#Z">Z | </a>

</div>

<div id="listData"></div>


getpublisher.php
In this case when I echo $startWith , it shows me home.php and my query returns me with a 0..Any idea how to change?

$startsWith = $_GET['startsWith'];

$sql = "SELECT * FROM `publisher` WHERE publisher_name LIKE '"+$startsWith+"%' ORDER BY publisher_name ASC";
$r = mysqli_query($link, $sql);
$rowcount = mysqli_num_rows($r);
//echo $sql ;

if ($rowcount > 0) {


while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo $row['publisher_name'] . "<br>";
}
} else {
echo "0 results";
}

Answer

Within the click handler you need the href of the element that was clicked.

var sW = $('a').attr('href').replace("#", "");

This will get the href of the first <a> tag in the page

Change to

var sW = $(this).attr('href').replace("#", "");

Inside the event handler this is the element the event occurred on