我的问题是选项值
<option selected="selected" disabled="disabled">Select a city...</option>
问题是:尽管这个选项没有价值,php 代码将其值视为* 'Select a city'*。因此,即使未选择城市,我的代码if(isset($_POST['city'])
也始终返回 true。我该如何解决这个问题?
这是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);
}
);
});
这是fetch_city.php:
if (isset($_POST['city']) && !empty($_POST['city'])) {
$city_id = $_POST['city']);
echo $city_id;
} else {
echo "You did not select a city...!";
}