I have some names of places inside a select. Some of this names have apostrophe. When user makes his choice with that select, Ajax makes a query to a table to find occurrencies of shops that match that location. My code is this:
<?php
echo "<select id='location' name='location'>";
echo "<option value='' selected>Select a location</option>";
$sql = "SELECT * FROM locations_list ORDER BY location_name;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['location_name'] . "'>". $row['location_name'] . "</option>";
}
}
echo "</select>";
echo "<select id='shop' name='shop'>";
echo "<option value='' selected='shop'>Select location first</option>";
?>
<script>
$(document).ready(function(){
$('#location').on('change',function(){
var locationID = $(this).val();
if(locationID){
$.ajax({
type: 'POST',
url: 'extract_shops.php',
data: {'location': locationID},
success:function(html){
$("#shop").html(html);
}
});
}else{
$('#shop').html('<option value="">Select a location first!</option>');
}
});
});
</script>
<?php
echo "</select>";
Things run smoothly when the locations don't have apostrophe in their name. In such cases, Ajax passes location name to extract_shops.php (which has a simple "SELECT * FROM shops WHERE location = '$passedlocationname';"
) and gets back a list of shops.
But when location has an apostrophed name, Ajax does not pass anything to extract_shops.php. I have put an if inside extract_shops.php that returns this info inside the second select in case Ajax doesn't pass anything.
Where am I wrong?