Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-26-2010, 11:35 PM   PM User | #1
schenn
New to the CF scene

 
Join Date: Dec 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
schenn is an unknown quantity at this point
Form won't post

So I am working on a greenhouse game. This page searches the user inventory for all lights and displays them in a table with a simple text box (later to be changed to hidden but for debugging purposes this does the job) that holds the lightid for selecting. Upon clicking the image of the item in question, it copies the lightid from the position box to the selected box.

All this works fine

However, when I click submit, the form merely resets itself and no actions are taken. I added a little 'print "this has posted" just beneath the if($_post) to check if the ($_post) was going through or not, it doesnt appear to be. I have tried moving the if($_POST) to the top, above the light inventory generation but was still unsuccessful

help...

Code:
<?php

 //dbase connecting code and userid retrieval goes here
//Connect to Dbase and get inventory of lights and place into cookie for retrieval and display later on (this works fine)


	if($_POST)
	{
                      print "this has posted";
		$lighttoadd = $_POST['selected'];
		if (isset($_SESSION['temppos'])
		{
			$pos = ((int)$_SESSION['temppos']);
		}
		else
		{
			$pos=0;
		}
		//If position is known, update position
		if($pos!=0)
		{
			
			$query = "UPDATE player_flower_lights SET lightid = '$lighttoadd' WHERE playerid = '$user' AND lightpos = '$pos'";
			$result = mysql_query($query);
		}
		else
		//if position unknown, check if position is available and add to end
		{
			//getnumlights + 1 = lightposition
			$playerlights = array();
			$maxlights = 0;

			$query = "SELECT * FROM player_flower_lights WHERE playerid = '$user'";
			$result = mysql_query($query);
			while($result)
			{
				$row = mysql_fetch_assoc($result);
				$playerlights[]=$row['lightpos'];
			}
			$numlights = count($playerlights);
			$nextpos = $numlights + 1;

			//getmaxlights
			$query = "SELECT environmentid FROM player_env_hardware WHERE playerid = '$user'";
			$result = mysql_query($query);
			if($result)
			{
				$row = mysql_fetch_assoc($result);
				$envid = $row['environmentid'];
			
				$query = "SELECT maxlights FROM environment WHERE id = '$envid'";
				$result = mysql_query($query);
				if($result)
				{
					$row=mysql_fetch_assoc($result);
					$maxlights = $row['maxlights'];
				}
				else
				{
                	print "Error loading maxlights";
					$success = 0;
				}
			}
			else
			{
        			print "Error loading environment";
					$success = 0;
			}
			if($nextpos<=$maxlights)
			{
                                            //if a position is available, insert into the players dbase of lights at the next available position
				$query = "INSERT INTO player_flower_lights (playerid, lightid, lightpos) VALUES ('$user', '$lighttoadd', '$nextpos')";
				print "Light successfully added to player database";
				$result = mysql_query($query);
				
				$query = "SELECT lumens, tempmod FROM lights WHERE id = '$lighttoadd'";
				$result = mysql_query($query);
				$lumens=0;
				$tempmod=0;
				if($result)
				{
                                                       //get light stats
					$row = mysql_fetch_assoc($result);
					$lumens = $row['lumens'];
					$tempmod = $row['tempmod'];
				}
				else
				{
					print "Error retrieving light stats";
				}
		
				//update room stats with new light stats
				//get current room stats
				if($lumens !=0)
				{
				$totallumens = 0;
				$totaltempmod = 0;
				$query = "SELECT totallumens, tempmod FROM player_env_stats WHERE playerid = '$user'";
				$result = mysql_query($query);
				if($result)
				{
                                                       //get current room stats
					$row = mysql_fetch_assoc($result);
					$totallumens = $row['totallumens'];
					$totaltempmod = $row['tempmod'];
					$totallumens += $lumens;
					$totaltempmod += $tempmod;
			                     
                                                      //update stats
					$query = "UPDATE player_env_stats SET totallumens = '$totallumens' WHERE playerid = '$user'";
					$result=mysql_query($query);
			
					$query = "UPDATE player_env_stats SET tempmod = '$totaltempmod' WHERE playerid = '$user'";
					$result=mysql_query($query);
		
			//remove item from inventory
					$query = "DELETE FROM player_inventory WHERE itemid = '$lighttoadd' AND UCASE(itemtype) = 'LIGHTS' AND playerid = '$user'";
					$result = mysql_query($query);
				}
				else
				{
					print "Error connecting to environment statistics";
				}
			}
			else
			{
				print "All light positions full";
			}
		}
		}
	}
		

?>
	
	<html>
	<head>
	<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
	<meta http-equiv='Content-Script-Type' content='text/javascript'>
	</head>
	<body>
	
	<?php
	
	print "<table id='inventory' border=1>";
	print "<tr>";
	
	//step2 display all lights (from cookie) in table with onclick that selects clicked item (hidden form item)
	$numitems = $_COOKIE['numitems'];
	
	for ($i=0;$i<=$numitems-1;$i++)
	{
		$cookieid = 'lightarray'.($i+1);
		$lightid = $_COOKIE[$cookieid];
		print "<td id = 'invbox".($i+1)."' >";
		print "<form id='lightarray".($i)."'>";
		print "<img src = 'images/Lights/".$lightid."inv.jpg' onclick=\"selectedpos.selected.value=lightarray".($i).".position".($i+1).".value\">";
		print "<input type = 'text' id = 'position".($i+1)."' value='".$lightid."'>";
		print "</form>";
		print "</td>";
	}
	print "</tr>";
	print "</table>";
	
?>	
	
	<form id='selectedpos' method='POST' action='addflowerlight.php'>		
	<input type ='text' id='selected'>
	<input type ='submit' value='Equip Light'>
	</form>
	</body>
	</html>

Last edited by schenn; 12-26-2010 at 11:46 PM..
schenn is offline   Reply With Quote
Old 12-27-2010, 01:46 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
What is the name of the script you are showing us.
Always provide the filename when you post a script.

As far as I know, "addflowerlight.php" is a script you aren't showing us.



.
mlseim is offline   Reply With Quote
Old 12-27-2010, 05:07 AM   PM User | #3
schenn
New to the CF scene

 
Join Date: Dec 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
schenn is an unknown quantity at this point
It is addflowerlight.php
schenn is offline   Reply With Quote
Old 12-27-2010, 05:29 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
This form ....

<form id='selectedpos' method='POST' action='addflowerlight.php'>
<input type ='text' id='selected'>
<input type ='submit' value='Equip Light'>
</form>


Has no variable names ... how is it supposed to work?

"id" is not a variable name.

Maybe you meant to do this?

<form id='selectedpos' method='POST' action='addflowerlight.php'>
<input type ='text' name='selected' id='selected'>
<input type ='submit' name='submit' value='Equip Light'>
</form>


.... and this line:
if($_POST)

would become:
if($_POST['submit'])
mlseim is offline   Reply With Quote
Old 12-27-2010, 05:33 PM   PM User | #5
schenn
New to the CF scene

 
Join Date: Dec 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
schenn is an unknown quantity at this point
Thank you.

cant believe I missed that...
schenn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:14 AM.


Advertisement
Log in to turn off these ads.