Hey all,
Problem: im trying to setup a shop where people can use a drop-down list to select the amount of items they would like to purchase, then when they click on the "buy" button it uses ajax to update the database with the amount they selected.
The problem is it will only get the number in the first drop down list. so if the first list is 1 and you select 5 in the second box (because you want 5 of the second item) it will only add one of the first item.
As the script stands at the moemnt, it will add whatever is in the first dropdown box, but will add the right item, so even if i chose 5 item2's it will only add 1 item2 at the price for item1.
I have worked out how to solve THAT problem, where the function showUser() is i can add strings in there (showUser(itemname,cost,etc etc)) but then it doesnt get the quantity of the item so will still only add one of the item.
Can someone please help me solve this issue.
Here are my pages:
shop.php
<?php
session_start(); // start up your PHP session!
?>
<?php require ("header.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<script src="selectuser.js"></script>
<link rel="stylesheet" type="text/css" href="css/layout.css">
</head>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
</body>
<div id="txtHint"></div>
<!--- PAGE CONTENT --->
<div class="shop">
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#000000" width="95%" id="AutoNumber1" bgcolor="#131313">
<tr>
<td width="100%" colspan="5"> </td>
</tr>
<tr>
<td width="100%" colspan="5"> </td>
</tr>
<?php
$results= mysql_query("SELECT * FROM weapons WHERE type = 'att' Order by cost asc");
$id = "id";
$name = "name";
$cost = "cost";
$max = "max";
$dbname = "dbname";
$att = "att";
$def = "def";
echo mysql_error();
if (mysql_Numrows($results)>0) //if there are records in the fields
{
$numrows=mysql_NumRows($results); //count them
$x=0;
while ($x<$numrows){ //loop through the records
$theid=mysql_result($results,$x,$id);
$thename=mysql_result($results,$x,$name);
$thecost=mysql_result($results,$x,$cost);
$themax=mysql_result($results,$x,$max);
$thedbname=mysql_result($results,$x,$dbname);
$theatt=mysql_result($results,$x,$att);
$thedef=mysql_result($results,$x,$def);
?>
<tr>
<td width="25%"><?=$thename?></td>
<td width="25%" align="center">$<?=number_format($thecost)?></td>
<td width="25%" align="center">attack: <?=number_format($theatt)?> | Defense: <?=number_format($thedef)?></td>
<td width="25%" align="center">
<form>
<input type='hidden' id='myid' value='<?=$fbuid?>' />
<input type='hidden' id='cost' value='<?=$thecost?>' />
<input type='hidden' id='attack' value='<?=$theatt?>' />
<input type='hidden' id='defense' value='<?=$thedef?>' />
<input type='hidden' id='dbname' value='<?=$thedbname?>' />
<select name="quantity" id="quantity">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<a href="#" onClick="showUser('<?=$thedbname?>')">BUY</a>
</td>
</form>
</tr>
<?
$x++;
}
}
?>
<tr>
<td width="100%" colspan="5"> </td>
</tr>
<tr>
<td width="100%" colspan="5">
<p align="center"><b>Total Attack: <?=number_format($attack)?></b></td>
</tr>
</table>
</center>
</div>
</div>
<!--- END WRAPPER DIV --->
</div>
</html>
selectuser.js:
Code: var xmlHttp;
function showUser(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var myid = document.getElementById('myid').value;
var quantity = document.getElementById('quantity').value;
var cost = document.getElementById('cost').value;
var attack = document.getElementById('attack').value;
var defense = document.getElementById('defense').value;
var url="ajx/shopbuy.php";
url=url+"?q="+quantity;
url=url+"&myid="+myid;
url=url+"&cost="+cost;
url=url+"&dbname="+str;
url=url+"&attack="+attack;
url=url+"&defense="+defense;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
and the file that updates the database shopbuy.php:
<?php
$con = mysql_connect('localhost', 'username', 'pasword');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("game", $con);
$id=$_GET['myid'];
$cost=$_GET['cost'];
$quantity=$_GET['q'];
$dbname=$_GET['dbname'];
$attack=$_GET['attack'];
$defense=$_GET['defense'];
$totcost = ($quantity*$cost);
$query = mysql_query("SELECT * from users WHERE fbuser_id = '$id'");
while ($s = mysql_fetch_array($query)) {
$mymoney = $s['money'];
}
if ($mymoney<$totcost) {
header ("Location: ../shop.php?error=2");
die();
}
$totatt = ($attack*$quantity);
$totdef = ($defense*$quantity);
$sql = "UPDATE users SET money=money-$totcost, attack=attack+$totatt, defense=defense+$totdef, $dbname=$dbname+$quantity WHERE fbuser_id = '$id'";
mysql_query($sql);
echo "<div class="rewards"><span class="rewards_title">Purchased $quantity $dbname for $totcost </span></div>";
mysql_close($con);
?>
This is really annoying and would appreciate any help.
Thanks
The IE or FF detector
Hello Friends,Problem of today ( we seek for php help ) as following ,we know all that PNG images not shown well on IE6 and some "css'ers" given us a solution --> fine 1but if that
Printer configuration
Hi All,
Get relative path from absolute path
How would one go about getting the relative path to a file from its absolute path?
Posting to another Site?
I have software that people install.When they install it, is there a way to make it post their domain name to my website?I know I could do a header('location: http//www.mysite.com/id=?domain.com');
Problem with HTTP Caching...?
Hi there, im trying to implement caching. But it doesn't always work.I post it here because there is no Client-Side HTTP topic...Code: private function setLastModified ($aTimestamp) {
Redirecting Admin
In my members table, I have a field called "perm" and it's set to zero for all members. However, I have two administrators, and theirs are set to 1.I want my members page that shows up on
Extract text from string
Hi folks,I have a string that looks like this:aaaaaaaaaa: bbbbbbbbbb (ccccccccc) dddddddddddddddddddddeeeeeeeeeeeeeeeeeeeee(format can't be changed - lengths of strings will vary) and I need to
php is not recognized as an internal or external command
Hello,I am trying to bake the code in CakePHP through my console.I have changed my Path in Environment Variables but still I am getting this error in console "php is not recognized as an internal
Ariba 9r - SAP MM Integration
Hi experts,
ALV List Display to point to another report on Double Click
Hi,