我有一个表格,允许用户从项目列表中进行选择:
<table class="table table-condensed table-striped table-bordered">
<thead>
<th scope="col">Code</th>
<th scope="col">Description</th>
<th class="text-center" scope="col">Select</th>
</thead>
<tbody>
<tr class="" id="PR7518">
<td>1234A</td>
<td>Option A</td>
<td class="text-center">
<button type="button" class="btn btn-success btn-sm">Select</button>
</td>
</tr>
<tr class="" id="PR7636">
<td>45678B</td>
<td>Option B</td>
<td class="text-center">
<button type="button" class="btn btn-success btn-sm">Select</button>
</td>
</tr>
<tr class="" id="PR9149">
<td>9988C</td>
<td>Option C</td>
<td class="text-center">
<button type="button" class="btn btn-success btn-sm">Select</button>
</td>
</tr>
<tr class="" id="PR9187">
<td>4455D</td>
<td>Option D</td>
<td class="text-center">
<button type="button" class="btn btn-success btn-sm">Select</button>
</td>
</tr>
</tbody>
</table>
&#13;
当他们点击Yes按钮时,它会触发一个AJAX请求,为他们选择的项目的ID设置一个PHP会话变量:
$productID = $_POST['itemID'];
$_SESSION['selectedItemID'] = $itemID;
当用户提交表单时,我会进行测试以确保$ _SESSION [&#39; selectedItemID&#39;]不为空,如果它为空,则显示错误消息,例如:
if ($_SESSION['selectedItemID'] == '') {
// display error message
}
我想添加一些客户端网站验证,以便在表单页面上显示警告,并阻止他们在用户尚未进行选择时提交表单。
这是我目前的剧本:
$(document).ready(function() {
$('button.btn-success').click(function() {
// Remove the classes from all of the TR elements
$(this).parents('table').find('tr').removeClass('success warning danger');
var itemID = $(this).closest('tr').attr('id');
console.log(itemID);
// Create a reference to $(this) here:
$this = $(this);
$.post('updateSelection.php', {
itemID: itemID,
selectionType: 'yes'
}, function(data) {
data = JSON.parse(data);
if (data.error) {
console.log(data);
console.log('something was wrong with the ajax request');
$this.closest('tr').addClass("warning");
//make alert visible
$("#alert_ajax_error").show();
return; // stop executing this function any further
} else {
console.log('update successful - success add class to table row');
$this.closest('tr').addClass("success");
$this.closest('tr').removeClass("danger");
//$(this).closest('tr').attr('class','success');
}
}).fail(function(xhr) {
console.log('ajax request failed');
// no data available in this context
$this.closest('tr').addClass("warning");
//make alert visible
$("#alert_ajax_error").show();
});
});
});
&#13;
我不确定如何链接提交按钮以检查是否存在PHP $ _SESSION [&#39; selectedItemID&#39;]会话变量?或者,如果有另一种方法可以达到相同的结果?
答案 0 :(得分:0)
我最终将所选ID写入隐藏表单输入,然后检查提交是否隐藏输入为空。