我这里有一个使用php的购物车样本。我试着将它修改为我需要的东西。我在这里添加的是使用ajax / jquery的搜索功能。搜索引擎工作,但问题是添加按钮不起作用。为什么我无法在列表中添加项目?我在这里做的是将index.php中的表分成search.php,所以当我尝试只搜索项目时我需要在表格中显示的内容。任何帮助都会受到欢迎。
的search.php
<?php
session_start();
include_once("config.php");
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_POST['bid'])) {
$search = $mysqli->real_escape_string($_POST['bid']);
$search = preg_replace("/[^A-Za-z0-9 ]/", '', $search);
$search = $_POST['bid'];
$results = $mysqli->query("SELECT * from app WHERE item_name LIKE '%$search%' ORDER BY item_name ASC");
echo'<table id="tfhover" cellspacing="0" class="table-list" style="text-transform:uppercase;" border="1">
<thead>
<tr>
<th></th>
</tr>
</thead>';
echo'<tbody>';
if(($results->num_rows)>= 1){
while($obj = $results->fetch_object())
{
echo '<form method="post" action="cart_update.php">';
echo '<tr>
<td style="text-align:left;">'.$obj->item_name.'</td>
<td><button class="add_to_cart" style="font-size:9px;">Add</button></td>
</tr>';
echo '<input type="hidden" name="product_code" value="'.$obj->counter.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
}
echo "</tbody></table>";
}
if(($results->num_rows)<= 0){
echo '<div style="margin-left:360px;">No Results</div>';
}
}
?>
的index.php
<?php
session_start();
include_once("config.php");
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
?>
<input type="text" name="bid" id="bid" placeholder="Search" style="text-transform:uppercase;width:300px;text-align:center;"/>
<div id="bid_result"></div>
<script type="text/javascript">
$(document).ready(function () {
function load(query) {
$.post('search.php', {
bid: query
}, function (search_bid) {
$('#bid_result').stop(true, true).fadeIn(200).html(search_bid);
}); //End ajax call
}
load($("#bid").val());
//Live search
$("#bid").on('keyup', function () {
//Input field value
load($(this).val());
}); //End on function
}); //End document.ready state
</script>
Cart_update.php
这是我的完整代码cart_update.php http://pastebin.com/1iDENLa0
<?php
session_start();
include_once("config.php");
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); // product code
$return_url = base64_decode($_POST["return_url"]); //return url
}
?>
答案 0 :(得分:0)
好的,我更改了代码的某些部分,你可以查看这段代码吗?它应该提交。
<?php
echo '
<table id="tfhover" cellspacing="0" class="table-list" style="text-transform:uppercase;" border="1">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>';
if(($results->num_rows)>= 1) {
while($obj = $results->fetch_object()) {
echo '
<form method="post" action="cart_update.php">
<input type="hidden" name="product_code" value="'.$obj->counter.'" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="'.$current_url.'" />
<input type="submit" class="add_to_cart" value="Add" style="font-size:9px;"/>
</form>';
}
echo '</tr></td>';
}
echo "</tbody></table>";
?>