好吧,有几种方法可以做到这一点。一种选择可能是将所选项目存储在 $_SESSION 变量中,然后将其打印为来自 venta.php 的 html 标记。此外,您可以在单击项目(元素)时添加事件以删除它们。这就像一个伪代码,当然,您需要验证数据,至少这试图解释一个关于如何使用 php、jquery 和 javascript 处理删除/添加项目的流程,而无需插件。
shop.php(存在要选择的项目表):
<!-- here you add jquery and your own javascript to play with that -->
<table>
<tr>
<td><a name="item_id" href="#">Item one</a></td>
</tr>
</table>
<div class="my-selected-items"></div>
<a hef="link-to-proceed-order">Proceed order</a>
javascript:
// add the result of php response to the *selected items* div
function selectRow(id) {
$.ajax
({
type: "POST",
url: "venda.php?action=add",
data: {selected: id},
cache: false,
success: function(data)
{
$('.my-selected-items').html(data);
register_delete_action();
}
});
}
function register_delete_action()
{
$(".remove-item").click(function(){
$.ajax
({
type: "POST",
url: "venda.php?action=delete",
data: {selected: id},
cache: false,
success: function(data)
{
$('.my-selected-items').html(data);
register_delete_action();
}
});
});
}
文塔.php
$action = $_GET['action'];
switch ($action)
{
case "add": addItem();
case: "delete": deleteItem();
}
function addItem(){
$selected_id = $_POST['selected'];
if(!array_key_exists('items', $_SESSION))
{
$_SESSION['items'] = array();
}
$_SESSION['items'][$selected_id] = array("id" => $item['id'], "name" => $item['name']);
//print the selected items in html markup
echo "<ul>";
foreach ($_SESSION['items'] as $id => $selection)
{
echo '<li> <a href="#" class="remove-item" name="' .$selection["name"] . '">' .$selection["name"] . '</a> </li>';
}
echo "</ul>";
}
function deleteItem(){
$selected_id = $_POST['selected'];
unset($_SESSION['items'][$selected_id]);
//print the selected items in html markup
echo "<ul>";
foreach ($_SESSION['items'] as $id => $selection)
{
echo '<li> <a href="#" class="remove-item" name="' .$selection["name"] . '">' .$selection["name"] . '</a> </li>';
}
echo "</ul>";
}