My problem is option value which is
<option selected="selected" disabled="disabled">Select a city...</option>
The problem is : despite this option having no value, php code sees its value as * Select a city *. So, my code if(isset($_POST[ city ])
returns always true even a city is not choosed. How can I solve this?
这里是 < strong> > > html 代码 :
<form id="city_form">
<select id="city" name="city">
<option selected="selected" disabled="disabled">Select a city...</option>
<option value=1 name="city1">City 1</option>
<option value=2 name="city2">City 2</option>
<option value=3 name="city3">City 3</option>
<option value=4 name="city4">City 4</option>
</select>
<input type="submit" value="Submit form" id="submit_button" />
</form>
<div id="show_city"></div>
这里是 < 坚固 > jquery < /坚固 > 代码 :
$( #submit_button ).click(function(e) {
e.preventDefault();
var data = $( #city_form ).serialize();
$.post(
"fetch_city.php",
data,
function (response) {
$( #show_city ).html(response);
}
);
});
这里是 < 坚固 > etch_ city.php 坚固 > :
if (isset($_POST[ city ]) && !empty($_POST[ city ])) {
$city_id = $_POST[ city ]);
echo $city_id;
} else {
echo "You did not select a city...!";
}