Jump to content

richarro1234

Members
  • Posts

    146
  • Joined

  • Last visited

Everything posted by richarro1234

  1. Hello, Ive just made a shop which is based on ajax/php and mysql. But when i got the response text back i need it to update the users money. So say someone has 10,000, the buy 3 items, the "buy" request is sent off via ajax to a seperate page, where it adds info to the database and takes off the cost from the money. then the responsetext comes back showing that the database has been updated. But what i need now is a script or a function that will update the money without re-loading the page either. Or that will show the money live, so that whenever someone buys something there money will automatically change Thanks Rich
  2. Thanks for the reply, i have changed the above but this time i dont get a response back. EDIT: managed to get it working, changed: document.getElementById('quantity' + myid).value; to document.getElementById('quantity_' + myid).value;
  3. 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: 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 Rich
  4. 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: 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 Rich
  5. anyone willing to help me sort this out? Thanks Rich
  6. Hey, sorry for not posting this the first time, i was quite tired and it just slipped my mind. Here is the full working page i have at the moment, i have removed my attempt a tmultiple inserts as it stopped the whole script from inserting anything. <?php $dbhost = "localhost"; $dbuser = "user"; $dbpass = "pass"; $dbname = "dbname"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); $query1 = mysql_query("SELECT * from users where username = '".$_COOKIE["twusername"]."'") or exit( mysql_error() ); $r = mysql_fetch_array($query1); $friendid = $r['id']; $query2 = mysql_query("SELECT * from friends where myid = '$friendid'") or exit( mysql_error() ); $friendlist = mysql_fetch_array($query2); $friend_feed = $friendlist['friendid']; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ClientIP = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ClientIP = $_SERVER['REMOTE_ADDR']; } //$ClientHost = gethostbyaddr($ClientIP); $ClientAgent = $_SERVER['HTTP_USER_AGENT']; $MyTimeStamp = time(); // Retrieve data from Query String $status = $_GET['status']; $id = $r['id']; if ($sex = 'Male'){ $sex = 'his'; }else{ if ($sex = 'Female') $sex = 'her'; } // Escape User Input to help prevent SQL Injection $status = mysql_real_escape_string($status); //build query $query = "UPDATE users SET mystatus = '$status' WHERE username = '".$_COOKIE["twusername"]."'"; $query1 = "INSERT INTO `feed` (`id`, `userid`, `friendid`, `action`, `did`, `when`, `groupname`) VALUES ('', '$id', '$friend_feed', 'status', 'Updated $sex', '$MyTimeStamp', '$status')"; //Execute query // Exit if calling directly the script file! if ($status != "") { $qry_result = mysql_query($query) or die(mysql_error()); $qry_result = mysql_query($query1) or die(mysql_error()); echo "<b><font color='#FF0000'>Status Updated Successfully</font></b>"; } else { echo '<b>Hacking Attempt!!</b><br><br>'; } ?> I need the whole code to make it work, that works now, but it only inserts one row with no friend_id. Thanks Rich
  7. Hello, Im trying to insert multiple rows into a mysql database but cant seem to get anything to work. Basically, without goin into detail, the page is called using ajax and it works fine, but im trying to get a list of ID's from a table, put it into an array (maybe?!) then seperate those ID's and add a new row into a table for each ID. I.E im trying to send out a mass message to people on a list, so i query the database for the ID's of people who are on the list, then i need to be able to get those ID's from the query, and insert a new row into the database for each of those ID's. How do i do that? Thanks Rich
  8. Hey, instead of mysql_query("INSERT INTO 'Highscores' (userID, score, ageGroup, saveTimeDate) VALUES($userID, $score, $ageGroup, $dateTimeStamp) "); Try: mysql_query("INSERT INTO 'Highscores' (userID, score, ageGroup, saveTimeDate) VALUES ('$userID', '$score', '$ageGroup', '$dateTimeStamp') ");
  9. Hey goldeneye, thanks for the help. i have changed what you told me to, and just to test it i: "echo $friend ;" but all it outputs is "ArrayArrayArrayArrayArrayArrayArray" (which is the amount of friendids there are for the selected user) i have also moved the curly-brace so it finishs the WHILE-loop before it runs the foreach, but it still doesnt add to the database. Also i would like help with the rewrite/re-organising of the above code please. thanks Rich
  10. Hey, I'm having some issues with this piece of script... <?php $dbhost = "localhost"; $dbuser = "user"; $dbpass = "pass"; $dbname = "dbname"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); $query1 = mysql_query("SELECT * from users where username = '".$_COOKIE["twusername"]."'") or exit( mysql_error() ); $r = mysql_fetch_array($query1); if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ClientIP = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ClientIP = $_SERVER['REMOTE_ADDR']; } //$ClientHost = gethostbyaddr($ClientIP); $ClientAgent = $_SERVER['HTTP_USER_AGENT']; $MyTimeStamp = time(); // Retrieve data from Query String $status = $_GET['status']; $id = $r['id']; $query10 = mysql_query("SELECT * from friends WHERE myid = $id"); while ($fre = mysql_fetch_array($query10)) { $friend = $fre['friendsid']; if ($sex = 'Male'){ $sex = 'his'; }else{ if ($sex = 'Female') $sex = 'her'; } // Escape User Input to help prevent SQL Injection $status = mysql_real_escape_string($status); //build query $query = "UPDATE users SET mystatus = '$status' WHERE username = '".$_COOKIE["twusername"]."'"; $a = array($friend); foreach ($a as $b) { $query1 = "INSERT INTO `richspri_social`.`feed` (`id`, `userid`, `friendid`, `action`, `did`, `when`, `groupname`) VALUES ('', '$id', '$b', 'status', 'Updated $sex', '$MyTimeStamp', '$status')"; } //Execute query } // Exit if calling directly the script file! if ($status != "") { $qry_result = mysql_query($query) or die(mysql_error()); $qry_result = mysql_query($query1) or die(mysql_error()); echo "<b><font color='#FF0000'>Status Updated Successfully</font></b>"; } else { echo '<b>Hacking Attempt!!</b><br><br>'; } ?> Now, i have tried this so many different ways. i added the foreach loops in after trying it another way, but it still doenst work. It will echo $b fine and will show what's it meant to, but when it adds it to the database, it adds everything, but doesn't add "$b" (which is the list of friends ID's). What its coded to do is get a list of ID's from a database, and for each of the ID's (say for example there's 4 ID's) then it will add it 4 times into the database with each of the ID's (1,5,11,32), but instead all it does is adds it once to the database and doesn't insert the ID, it just adds a blank space. Can someone have a look at it and find out whys it not doing what its meant to. thanks Rich
  11. oo i see, that makes more sense lol. can you explain abit more on how to do it though, i understand i just havent really used join() so not fully sure how it works. Thanks Rich
  12. Ok, heres whats happening.... Its a different kind of social networking site (i wont go into detail) basically im creating a mini feed like facebook has got, and im trying to make it so that the news feed is posted to other peoples mini feeds. The only way i could think of doin that was by having it add a new row for each friend that person has. So, say i was id 1 and i was friends with id's 3,5,6,7,9,12. The database would look like this: userid | Friendid | action 1 3 updated status 1 5 updated status 1 6 updated status 1 7 updated status 1 9 updated status 1 12 updated status Then on the friends mini feeds (3,5,6,7,9,12) it will show <username>(id 1) has updated his status. unless you can think of an easier way to do it that doesnt involve creating a new row for every friend? Hope that explains it a bit better. Thanks Rich
  13. bumping the topic, still need help please
  14. well its goin into a different table. its like a news feed, ppl select what catagorys they want to see, then the script gets the list from the database, adds it to the new database, then the script outputs the users choice. but it needs to be a new row for each catagory. like: ID|CATID 1 | 1 1 | 3 2 | 1 2 | 5 etc etc... Well, the page im using needs to get a list of id's then that list needs to be passed to another script where that list of numbers can be added to the database as new rows for each. Im trying to create a news feed so need the friends id's as a list so that when its added to the database it shows to friends of the person posting the news.
  15. well there isnt any code yet as i dont knowe where to starts, thats what i was hopeing for, someone to help me get started and show me some examples of the type of thing im after and that kinda stuff.
  16. Hey. I need some help with arrays, please. Im trying to get a list of numbers from a database, put it into an array, send it to another page (using ajax) then the second page qill sort the array into a list of numbers, count how many numbers there are, and add them into the database. but i have no idea how to do arrays very well, i have looked at a few tutorials but still dont understand how to do what i need. Can someone help me please. Thanks Rich
  17. once logged in, refresh the page, i have added a link on the side for it.
  18. still nothing. it works fine on another page, its used to delete comments. it just wont work on this page.
  19. Thanks, that worked fine, EDIT: i fixed the PHP bit. it also doesnt fade out after clicking on the link. $.ajax({ type: 'GET', url: 'addfriend.php', data: data, success: function(){ $(this).parents('.record').animate({ backgroundColor: "#000000" }, "fast") .animate({ opacity: "hide" }, "normal"); } }); Thanks Rich
  20. Thanks for the quick response. That seems to run this time, but it doesnt run the "success" bit would that be to the code still not working or the other script not working? Thanks
  21. Hello, Im trying to setup a page where people can accept requests without having the page refresh, but i need to send the external page 2 variables from the first page, but for some reason it doesnt work, it works fine if i try to send 1, but not 2. Can someone please help me try to sort this out, here is the code: <script type="text/javascript"> $(function() { $(".addfriend").click(function(){ //Save the link in a variable called element var element = $(this); //Find the id of the link that was clicked var add_id = element.attr("myid"); var addf_id = element.attr("friendsid"); //Built a url to send var info = 'myid=' + add_id, '&friendid=' + addf_id; if(confirm("This will add this person as a friend!")) { $.ajax({ type: "GET", url: "inc/addfriend.php", data: info, success: function(){ } }); $(this).parents(".record").animate({ backgroundColor: "#000000" }, "fast") .animate({ opacity: "hide" }, "normal"); } return false; }); }); </script> I have also tried this line: var info = 'myid=' + add_id, '&friendid=' + addf_id; Like this: var info = 'myid=' + add_id '&friendid=' + addf_id; but it still didnt work. Anyone know how to fix this? Thanks Rich
  22. but would that still show up when the libk is clicked?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.