...

View Full Version : Problem passing $_POST Values from Form



bgh10788
08-13-2007, 04:46 PM
I'm working on creating a gaming ladder for a tournament I'm in charge of. I'm having some trouble with getting the post values from a form after it has been submitted. Here is the link to the page...

gulfcoastcommandos.freehostia.com/changeposition.php

Everything comes up fine, but as soon as you try to submit it, all the positions in the database are set to 0 (zero). I believe I have narrowed it down to the
$newpos=$_POST['$teamname'] line because in on the next page, it displays "Team Name: ... Position: "with nothing following position(no number, no text).

Here is the code for the place where the admin can make the changes to the team position. Everything on this page displays and works appropriately (except the POST passing of values). You'll have to trust me on this because as soon as someone here goes and tries to change them, this page no longer works since all team positions are 0.



<?php
include("include/logincheck.php");
include("include/header.php");
include("include/connect.php");
include("include/admincheck.php");
?>
<div align="center" class="style7">
<br><br>
Adjust the order of the teams by selecting a new number in the box.<br><br>
<form action="processchangeposition.php" method="POST">
<table width="40%">
<?php

//get the maximum position from the database
$query="SELECT position FROM team";
$result=mysql_query($query)
or die("Database is unreachable. Contact an administrator or try again in a few minutes.");
while ($row=mysql_fetch_assoc($result))
{
extract($row);
if ($position>$maxpos)
{
$maxpos=$position;
}
}

//get all the team names and positions
$query="SELECT name, position FROM team ORDER BY position";
$result=mysql_query($query)
or die("Error connecting to database");

//cycle through the array of teams and make an option to change the position of each team based on the $maxpos variable
while ($row=mysql_fetch_assoc($result))
{
extract($row);
//create different drop-down select for each team
echo "<tr>
<td class='style7'>$name</td>
<td class='style7'><select name='$name'>";
for ($a=1;$a<=$maxpos;$a++)
{
if ($position==$a)
{
echo "<option value='$a' selected='selected'>$a";
}
else
{
echo "<option value='$a'>$a";
}
}
echo "</select></td></tr>";
}
?>

<tr>
<td class="style7" colspan="2" align="center"><input type="submit" value="Submit Changes"></td>
</tr>
</table>
</form>
</div>
<?php
include("include/footer.php");
?>



Here is the code for what happens to process the change


<?php
include("include/logincheck.php");
include("include/header.php");
include("include/connect.php");
include("include/admincheck.php");
?>
<div align="center" class="style7">
<?php
//cycle through all the teams in the database
$query="SELECT name FROM team ORDER BY position";
$result=mysql_query($query)
or die("Error connecting to database");
while ($row=mysql_fetch_assoc($result))
{
//get the team name from the database
$teamname=$row['name'];
//get the position from the POST variable - NOT WORKING!!!
$pos=$_POST['$teamname'];

echo "Name: $teamname Position: $pos <br>";
//update the database for each team
$query1="UPDATE team SET position='$pos' WHERE name='$teamname'";
$result1=mysql_query($query1)
or die("Error connecting to database");
}
?>
<br><br>
Team positions changed successfully.
</div>
<?php
include("include/footer.php");
?>


Thank you in advance for your help.

abduraooft
08-13-2007, 05:10 PM
Change $newpos=$_POST['$teamname']

to $newpos=$_POST['teamname']
(Just a quick look, hope this may help!)

bgh10788
08-13-2007, 05:28 PM
I'm not sure that is correct because...


<select name='$name'>


The name of the select fields is not "teamname" it's the actual name of the team.

aedrin
08-13-2007, 05:52 PM
$newpos=$_POST['$teamname']

Actually, that would be the problem.

Single quotes do not have their variables parsed.

So if $teamname is the name of the team, and the dropdown has that name, then you'd want this:


$newpos=$_POST[$teamname]

bgh10788
08-13-2007, 06:14 PM
Thank you for your help. That fixed part of the problem.

It still isn't completely successful and I think it is because the team names have spaces. Is there a way to get rid of the spaces(just for setting the name attribute)? Or is there a better way than my current system?

vinyl-junkie
08-13-2007, 06:57 PM
This is just a guess since I don't know your database schema, but how about changing team name to an id in your form? Your script could then look up the team name by id, then do subsequent processing based on that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum