This has sessions in it, to store the 2-3 measurements chose, and shows the buynow button, and a Plus and Minus quantity buttons to change the amount to purchase.
It's this code here:
http://jsfiddle.net/laelitenetwork/puJ6G/
Problem is, if I run it in the ajax file, it shows up but doesn't work.
If I have the Plus and Minus fields in the file, and the Javascript code in the product.inc page, it doesn't work either.
If I run the code as supplied, in a test.html file it works.
The "js" files is stored locally and referred to correctly.
Oddly, we have this working on another site that doesn't use Ajax. It's just there on the product page.
So I wondered if it won't work because you perhaps cannot have the script on one page, and the form buttons on another. And the Script won't work on the ajax page because it isn't being called again?
Bit of advice please.
Here is the Ajax called page.
Code: Select all
<script type="text/javascript" src="/js/jquery-1.js"></script>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(0);
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(0);
}
});
});
});//]]>
</script>
<?php
session_start();
include "dbconn.php";
?>
<?php
$sku = isset($_SESSION['sku']) ? $_SESSION['sku'] : null;
echo "
<form id='myform' action='/cart' method='post'>
<input type='hidden' name='storeid' value='123'>
<input type='hidden' name='itemcode' value='$sku'>";
$query = mysql_query("SELECT depth FROM products WHERE depth <> '0' AND sku = '$sku'");
$num_rows = mysql_num_rows($query);
if ($num_rows == 0) { $depth = "0"; }
if ($num_rows != 0)
{
$depth = "0";
if(isset($_GET['depth']))
{
$depth = $_GET['depth'];
$_SESSION['depth']=$depth;
echo "<input type='hidden' name='itemname3' value='$depth'>";
}
else if (isset($_SESSION['depth']))
{
$depth=$_SESSION['depth'];
echo "<input type='hidden' name='itemname3' value='$depth'>";
}
}
$query = mysql_query("SELECT length FROM products WHERE length <> '0' AND sku = '$sku'");
$num_rows = mysql_num_rows($query);
if ($num_rows == 0) { $length = "0"; }
if ($num_rows != 0)
{
$length = "0";
if(isset($_GET['length']))
{
$length = $_GET['length'];
$_SESSION['length']=$length;
echo "<input type='hidden' name='itemname2' value='$length'>";
}
else if (isset($_SESSION['length']))
{
$length=$_SESSION['length'];
echo "<input type='hidden' name='itemname2' value='$length'>";
}
}
$query = mysql_query("SELECT thickness FROM products WHERE thickness <> '0' AND sku = '$sku'");
$num_rows = mysql_num_rows($query);
if ($num_rows == 0) { $thickness = "0"; }
if ($num_rows != 0)
{
$thickness = "0";
if(isset($_GET['thickness']))
{
$thickness = $_GET['thickness'];
$_SESSION['thickness']=$thickness;
echo "<input type='hidden' name='itemname4' value='$thickness'>";
}
else if (isset($_SESSION['thickness']))
{
$thickness=$_SESSION['thickness'];
echo "<input type='hidden' name='itemname4' value='$thickness'>";
}
}
$query = mysql_query("SELECT price, productid, sku FROM products WHERE depth = '$depth' AND length = '$length' AND thickness = '$thickness' AND sku ='$sku'");
$num_rows = mysql_num_rows($query);
if ($num_rows == 0 )
{
}
else
{
echo "<div class='ajax_buynowbox' >";
while ($row = mysql_fetch_object($query))
{
echo "<div class='product_price'>£$row->price</div>
<input type='submit' value='Add to Cart' class='submit_buynow'>
<input type='button' value='-' class='qtyminus' field='quantity' />
<input type='text' name='quantity' value='5' class='qty' />
<input type='button' value='+' class='qtyplus' field='quantity' />
";
}
echo "</div>";
}
mysql_close($sqlconn);
echo "</form>";
?>