Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Screen refresh when submitting a form - PHP

Status
Not open for further replies.

PinkMink

Technical User
Mar 28, 2005
7
US
Hi,

I'm in the process of building a fantasy football website using PHP/MySQL on Dreamweaver MX 2004.

Ideally I want a drop down menu of players for their particular area of the pitch (goalies, defenders, etc). When you have chosen the player you want, you click submit and the database is updated...you can then go back and change the player if you want to.

I've created a test form (for just the goalie). The problem is, when I submit the form to update the database, the screen refreshes and the first player in the list appears in the drop down (the database updates ok). I really want it to display the player that the user has just selected.

The piece of code that seems to relate to the submission is:


$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}



<body>
<form action="<?php echo $editFormAction; ?>" method="POST" name="choice" id="choice">
<p>
<input name="username" type="text" id="username" value="testUser">
</p>
<p>
<select name="choose_goalie" id="choose_goalie">
<?php
do {
?>
<option value="<?php echo $row_goalie['code']?>"><?php echo $row_goalie['surname']?></option>
<?php
} while ($row_goalie = mysql_fetch_assoc($goalie));
$rows = mysql_num_rows($goalie);
if($rows > 0) {
mysql_data_seek($goalie, 0);
$row_goalie = mysql_fetch_assoc($goalie);
}
?>
</select>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
<input type="hidden" name="MM_update" value="choice">
</form>
</body>


Any help greatly appreciated.

Kind regards,

Rob
 
When you output the <option> line you need to check to see if the option you're echoing is the same as the one that was clicked on previously. If so, echo the string " selected ".

Also, you should use the superglobal array $_SERVER instead of $HTTP_SERVER_VARS.

Here's my take on your code (with some modifications)
Code:
<body>
<form action="<?php echo $editFormAction; ?>" method="POST" name="choice" id="choice">
  <p>
    <input name="username" type="text" id="username" value="testUser">
  </p>
  <p>
    <select name="choose_goalie" id="choose_goalie">
      <?php
while ($row_goalie = mysql_fetch_assoc($goalie)) {  
      echo '<option value="' . $row_goalie['code'] .'">' . $row_goalie['surname'];
      if (isset($_POST['choose_goalie']) && ($_POST['choose_goalie'] == $row_goalie['surname'])) echo ' selectet ';
      echo "</option>\n";}
?>
    </select>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
  <input type="hidden" name="MM_update" value="choice">
</form>
</body>

BTW, I cleaned up your MySQL while loop (I think I understood what you were trying to do).

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top