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
form class help (oop php5)
Hidy Ho Neighbors,I'm forcing myself to learn oop/classes for php5. It seems like a good idea for repetitious coding such as with forms. I've been through the oop tutorials here and elswhere in
filesize() returns 0
I have a socket program that runs in an infinite loop, listening for connections, in this socket I have a method, that is supposed to open a file and write to the file, but I am having issues. Each
Linux socket programming
Where can I find a good introduction to socket programming?
Converting RGB values to HEX
Code: <?PHP$file_handle = fopen("colors/rgb.csv", "r");while (!feof($file_handle) ) {$line_of_text = fgetcsv($file_handle, 1024);print $line_of_text[2] . $line_of_text[3] .
BackButton Behaviour in AJAX
I have an ASPX Page AJAX Enabled!The page has a gridView and a DDL for filtering on it. The Gridview has links on its items. The behaviour I am running into is:If I filter a criteria from DDL and then
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
Loop Through Date Range
Hi guys,
How can I get my program to read a larger array ??
Hi:
Print 'a' to 'z' via for loop
A very simple problem..How Can I print a to z NOT a to y ?It is a part of a code where | $alpha= 'a';for($alpha='a';$alpha!='z';$alpha++){echo $alpha.' ';} Output: a b c d e f g h i j k l m n o p q
Sending CC Info by email
I am a little bit less knowledgable in the security area as most developers and I know I have a lot to learn so maybe this is a start. But I have a client who just came over to us and previously was