I have been stuck over this for almost a day now and I can not figure out whats the problem here. I need $_POST['brand'] in the SQL statement here and I get a blank.
// sql for getting nearby stores
$sql = "SELECT *, ( 3959 * ACOS( COS( RADIANS(".$_POST['lat'].") ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(".$_POST['lng'].") ) + SIN( RADIANS(".$_POST['lat'].") ) * SIN( RADIANS( latitude ) ) ) ) AS distance
FROM stores
WHERE brand='".$_POST['brand']."'
HAVING distance <= ".$_POST['distance']."
ORDER BY distance";
This is the form in the PHP file.
<form method="post" action="./index.php" id="store_locator">
<fieldset>
<legend>Store Locator</legend>
<div class="input">
<label>Address/Postcode: <span class="required">*</span></label>
<input type="text" class="texta" name="address" id="address" value="<?php if(isset($_POST['address'])){ echo $_POST['address'];} ?>" />
<span>e.g: "Sydney", "Magill Road"</span>
</div>
<div class="input">
<label>Brand: <span class="required">*</span></label>
<select name="brand" id="brand">
<option selected="selected" value="bmw">BMW</option>
<option value="fiat">Fiat</option>
<option value="ford">Ford</option>
</select>
<label>Distance: <span class="required">*</span></label>
<select name="distance" id="distance">
<?php
// populate selected option
$selected = 1;
if(isset($_POST['distance'])) {
$selected = $_POST['distance'];
}
?>
<?php foreach($distances as $k=>$v): ?>
<option value="<?php echo $k; ?>" <?php if($selected == $k){ echo 'selected="selected"';} ?>><?php echo $v; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="input buttons">
<button type="submit" name="find" id="find">Find</button>
</div>
</br>
<input type="button" value="Reset Map" onclick="reset2();" />
</fieldset>
</form>
What am I missing here? Strangely $_POST['distance'] is getting value. Please help.
If I echo the SQL I see brand ='' in the SQL statement.
This is the SQL code getting executed:
SELECT *, ( 3959 * ACOS( COS( RADIANS(-31.9530044) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(115.85746930000005) ) + SIN( RADIANS(-31.9530044) ) * SIN( RADIANS( latitude ) ) ) ) AS distance FROM stores WHERE brand='' HAVING distance <= 30 ORDER BY distance
Contents of $_POST
array(5) {
["ajax"]=> string(1) "1"
["action"]=> string(17) "get_nearby_stores"
["distance"]=> string(1) "5"
["lat"]=> string(11) "-31.9530044"
["lng"]=> string(18) "115.85746930000005"
}
That comment thread is getting too long.
You appear to be posting using ajax. We need to see the javascript you are connecting to the submit button.
You likely forgot to tell jQuery (or whatever function you are using) to serialze the
brandselect.