...

View Full Version : Form won't post



schenn
12-26-2010, 11:35 PM
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...




<?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>

mlseim
12-27-2010, 01:46 AM
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.



.

schenn
12-27-2010, 05:07 AM
It is addflowerlight.php

mlseim
12-27-2010, 05:29 PM
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'])

schenn
12-27-2010, 05:33 PM
Thank you. :thumbsup:

cant believe I missed that...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum