我对jquery
很陌生,但我想知道它们是否可以通过下面的jquery运行mysql
。
在我的cart.php
我有一个query
,检查发布的内容是否在数据库中,如果没有,则不会添加该项目。
在我的view.php页面中,jquery
和hidden div
位于以下works
当您点击add button
时隐藏的div显示了什么你添加了。
<button type="submit" name="submit" class="show_hide" id="button" name="submit">Add</button>
Jquery的
$(document).ready(function(){
$('#selected').hide();
$('#button').click(function(){
var pid = $('#pid').val();
var length = $('#length option:selected').text();
var Category = $('#Category').val();
var cat = $('#Category option:selected').text();
var Qty = $('#Qty').val();
var qty = parseInt($('#Qty').val());
var price = parseFloat($('#pricetag').text().replace(/^\D/, ''), 10) * qty;
price = '\u00A3' + price.toFixed(2);
var category = $('#Category').val();
if (!/^[1-9]\d?$/.test(Qty)){
alert('Quantity should not be below 1 or null');
return false; // don't continue
}
else {
$('#sprice').text(price);
$('#scategory').text(cat);
$('#slength').text(length);
$('#selected').slideDown();
}
$.ajax({
url: '../cart.php',
type: 'POST',
data: { pid:pid,
length:length,
Qty:Qty,
Category:Category },
success: function(data)
{
location.reload();
}
});
});
});
隐藏div
<div id="selected">
<table class="tableclass">
<tr>
<td>Price:</td>
<td id='sprice'></td>
</tr>
<tr>
<td>Category:</td>
<td id='scategory'></td>
</tr>
<tr>
<td>Length:</td>
<td id='slength'></td>
</tr>
</table>
</div>
已修改 添加查询代码
在cart.php页面中,我有query
$statement = $conn->prepare("SELECT * FROM Category ct, Itemised ip, Product pd
WHERE ct.CatID = ? AND ip.Length = ? AND pd.pid = ?
");
$statement->execute(array($Category, $length, $pid));
if (!$statement->fetch()) {
exit();
}
用于检查发布的内容是否存在于数据库中。这或多或少是我想在jquery中运行的查询,以检查var
是否存在,如果不存在,url:./cart.php
不应该运行。
更新以显示我的cart.php代码
if(isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && isset($_POST['Category'])){
$pid = $_POST['pid'];
$length = $_POST['length'];
$qty = $_POST['Qty'];
$Category = $_POST['Category'];;
$wasFound = false;
dbconnect();
$statement = $conn->prepare("SELECT * FROM Category ct, Itemised_Product ip, Product pd
WHERE ct.CatID = ? AND ip.ItemID = ? AND pd.Sef = ?
");
$statement->execute(array($Category, $length, $pid));
if (!$statement->fetch()) {
exit();
}
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"]as $array_key=>$each_item) {
if ($each_item['item_id'] == $pid && $each_item['length'] == $length && $each_item['Category'] == $Category) {
$_SESSION["cart_array"][$array_key]['quantity']+=$qty;
$wasFound = true;
}
}
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
}
}
header('Location: '.fetchinline($bpages).$currentFile);
exit();
}
我的问题
在购物车页面中,我有一个查询,用于检查发布的内容是否未被用户检查。我想对上面的jquery做同样的事情,如果用户have
我要停止,我想阻止hidden div
显示。
我希望我已经向你理解了。谢谢你的时间。
答案 0 :(得分:0)
我不确定我是否完全理解你的问题。是吗:
OR
无论如何,在上述两种情况下,您都可以向服务器执行AJAX请求并检查产品是否存在。如果你研究$ .ajax成功函数,那么&#39;数据&#39;实际上是来自服务器的响应,您可以包含所需的任何内容,并使用javascript / jquery处理数据。
答案 1 :(得分:0)
的js
$(document).ready(function(){
$('#selected').hide();
$('#button').click(function(){
var pid = $('#pid').val();
var length = $('#length option:selected').text();
var Category = $('#Category').val();
var cat = $('#Category option:selected').text();
var Qty = $('#Qty').val();
var qty = parseInt($('#Qty').val());
var price = parseFloat($('#pricetag').text().replace(/^\D/, ''), 10) * qty;
price = '\u00A3' + price.toFixed(2);
var category = $('#Category').val();
if (!/^[1-9]\d?$/.test(Qty)){
alert('Quantity should not be below 1 or null');
return false; // don't continue
}
else {
$('#sprice').text(price);
$('#scategory').text(cat);
$('#slength').text(length);
$('#selected').slideDown();
}
$.ajax({
url: '../cart.php',
type: 'POST',
data: { pid:pid,
length:length,
Qty:Qty,
Category:Category },
dataType:json,
success: function(data)
{
if(data.response=='failed')
{
alert('error');
}
else if(data.response=='success')
{
alert('success');
//location.reload();
}
else if(data.response == 'already_in_database')
{
alert('Already exists in database');
}
},
error: function()
{
alert('error');
}
});
});
});
Cart.php
if (isset($_POST['pid']) && isset($_POST['length']) && isset($_POST['Qty']) && isset($_POST['Category']))
{
$pid = $_POST['pid'];
$length = $_POST['length'];
$qty = $_POST['Qty'];
$Category = $_POST['Category'];
$wasFound = false;
dbconnect();
$statement = $conn->prepare("SELECT * FROM Category ct, Itemised_Product ip, Product pd
WHERE ct.CatID = ? AND ip.ItemID = ? AND pd.Sef = ?
");
$statement->execute(array($Category, $length, $pid));
$result=array();
if (!$statement->fetch())
{
$result['response'] = 'already_in_database';
echo jsone_encode($result);
die();
}
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
{
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
}
else
{
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"]as $array_key => $each_item)
{
if ($each_item['item_id'] == $pid && $each_item['length'] == $length && $each_item['Category'] == $Category)
{
$_SESSION["cart_array"][$array_key]['quantity']+=$qty;
$wasFound = true;
}
}
if ($wasFound == false)
{
array_push($_SESSION["cart_array"], array("item_id" => $pid, "length" => $length, "Category" => $Category, "quantity" => $qty));
}
}
header('Location: ' . fetchinline($bpages) . $currentFile);
exit();
}