Jump to content

Help - PHP Code works in Google Chrome, but not in IE or Firefox!


stansbie

Recommended Posts

Hi All,

 

I'm new around here, and hoping someone can help. I've recently started coding in PHP, and am having trouble with some code. It's basically a form that asks a user to pick a Football (soccer) team from a list of players. Once they've selected their team, the code checks that their team doesn't exceed a certain monetary value and prevents them submitting their team. If that's the case, then they click the browser back button, and they are shown their original selections and given the opportunity to make changes before attempting to resubmit it.

The code seems to work exactly the way I want it to if I run it in a Google Chrome browser, but in IE and Firefox, when they click the back browser button, their original selections are wiped and they have to start from scratch.

I can't figure out why this happens......is anyone able to help, and point me in the right direction?

 

The code is here:

<?php 
$gk = $_POST["gk"];
$def1 = $_POST["def1"];
$def2 = $_POST["def2"];
$def3 = $_POST["def3"];
$def4 = $_POST["def4"];
$mid1 = $_POST["mid1"];
$mid2 = $_POST["mid2"];
$mid3 = $_POST["mid3"];
$midstrike = $_POST["midstrike"];
$stk1 = $_POST["stk1"];
$stk2 = $_POST["stk2"];
$stk3 = $_POST["stk3"];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>

<html><body>



<FORM action="<?php echo $PHP_SELF;?>" method="post">

<?php

// Function to retrieve a list of all players for a select box.

function getPlayerDetailsType1($selectname, $playtype, $playtitle){

echo "<select name='$selectname'>";

$query = "SELECT * FROM gffl_players WHERE player_type1 = '$playtype'";
$result = mysql_query($query) or die ("Couldn't execute query");

echo "<option selected='yes' align='center'>$playtitle</option>";

while ($row = mysql_fetch_array($result))
{
echo "<option value='{$row['player_code']}'>{$row['player_code']}-{$row['player_name']}-{$row['player_team']}-£{$row['player_value']}</option>";
}

echo "</select>";}

// Function to retrieve a list of all players for a select box.

function getPlayerDetailsType2($selectname, $playtype, $playtitle){

echo "<select name='$selectname'>";

$query = "SELECT * FROM gffl_players WHERE player_type2 = '$playtype'";
$result = mysql_query($query) or die ("Couldn't execute query");

echo "<option selected='yes' align='center'>$playtitle</option>";

while ($row = mysql_fetch_array($result))
{
echo "<option value='{$row['player_code']}'>{$row['player_code']}-{$row['player_name']}-{$row['player_team']}-£{$row['player_value']}</option>";
}

echo "</select>";}


echo "<p align='center'>";

getPlayerDetailsType1("gk", "G", "Goalkeeper");

echo "</p>";


echo "<p align='center'>";

getPlayerDetailsType1("def1", "D", "Defender1");
getPlayerDetailsType1("def2", "D", "Defender2");
getPlayerDetailsType1("def3", "D", "Defender3");
getPlayerDetailsType1("def4", "D", "Defender4");

echo "</p>";


echo "<p align='center'>";

getPlayerDetailsType1("mid1", "M", "Midfielder1");
getPlayerDetailsType1("mid2", "M", "Midfielder2");
getPlayerDetailsType1("mid3", "M", "Midfielder3");

echo "</p>";

echo "<p align='center'>";

getPlayerDetailsType2("midstrike", "Y", "Midfielder4 or Striker3");

echo "</p>";

echo "<p align='center'>";

getPlayerDetailsType1("stk1", "S", "Striker1");
getPlayerDetailsType1("stk2", "S", "Striker2");


echo "</p>";


?>


<p align="center"><input type="submit" value="Test Team" name="submit"></p>

<?php

} ELSE { 


// This section displays a list of player details in table format.

// Function to build table rows

function buildTeamTable($selectedcode, $colour){

$query2 = "SELECT * FROM gffl_players WHERE player_code = $selectedcode";
$result2 = mysql_query($query2)
          or die ("Couldn't execute query.");

$row2 = mysql_fetch_assoc($result2);
extract($row2);

echo
             "<tr bgcolor='$colour'>
              <td align='center'>$selectedcode</td>\n
              <td align='center'>$player_name</td>\n
              <td align='center'>$player_value</td>\n 
              </tr>\n";

              return $player_value;
}


echo
     "<table border='1' align='center' rules=all frame=void cellspacing='6'>\n
      <tr bgcolor='gainsboro'>\n
      <td width='50' align='center'><b>Code</b></td>\n
      <td width='100' align='center'><b>Name</b></td>\n
      <td width='40' align='center'><b>Value</b></td>\n
      </tr>\n";



$teamvalue = $teamvalue + buildTeamTable($gk, "#FFFF99");
$teamvalue = $teamvalue + buildTeamTable($def1, "#FFCC00");
$teamvalue = $teamvalue + buildTeamTable($def2, "#FFCC00");
$teamvalue = $teamvalue + buildTeamTable($def3, "#FFCC00");
$teamvalue = $teamvalue + buildTeamTable($def4, "#FFCC00");
$teamvalue = $teamvalue + buildTeamTable($mid1, "#00CCCC");
$teamvalue = $teamvalue + buildTeamTable($mid2, "#00CCCC");
$teamvalue = $teamvalue + buildTeamTable($mid3, "#00CCCC");

IF ($midstrike <= 6000) 
{$teamvalue = $teamvalue + buildTeamTable($midstrike, "#00CCCC");}
ELSE 
{$teamvalue = $teamvalue + buildTeamTable($midstrike, "#66FF66");}

$teamvalue = $teamvalue + buildTeamTable($stk1, "#66FF66");
$teamvalue = $teamvalue + buildTeamTable($stk2, "#66FF66");



$teamvalue_formatted = number_format($teamvalue, 1);

echo         "<tr bgcolor='gainsboro'>\n
              <td></td>\n
              <td align='center'><b>Totals</b></td>\n 
              <td align='center'><b>$teamvalue_formatted</b></td>\n
              </tr>\n

     </table>";

echo "
<BR>
<BR>";

// This section checks and displays the team formation, and indicates if the team value is under or over budget.

$overbudget = $teamvalue - 50;

IF ($midstrike <= 6000)
{ echo "You have selected a 4-4-2 formation";}
ELSE
{ echo "You have selected a 4-3-3 formations";}

echo "
<BR>
<BR>";

IF ($teamvalue <= 50)
{ echo "Your team value is within the £50m budget";
  echo "<p align='center'><input type='submit' value='Submit Team' name='submit'></p>";
}
ELSE
{ echo "Your team value is over the £50m budget by £$overbudget .... please make changes!";
  echo "<p align='center'><input type='reset' value='Make Changes' name='makechanges'></p>";}
}


?>

</FORM>

</body></html>

 

Any assistance you can provide will be gratefully appreciated.

 

Regards

 

Ant

You shouldn't rely on the browser to do this. You should store the submitted values in a session var, a database or a cookie and have the script return you to the pre-filled form. All browsers handle the back button differently.

You shouldn't rely on the browser to do this. You should store the submitted values in a session var, a database or a cookie and have the script return you to the pre-filled form. All browsers handle the back button differently.

 

Thanks for the prompt reply!

I've not yet got to grips with the techniques you've mentioned. Would you be able to elaborate a bit, or point me in the direction of a useful resource that would  help me understand this better?

Better still......could you indicate where I should use the technique within my code, with maybe a bit of dummy code to illustrate? Or is that being a bit too cheeky / lazy of me!?  ;D

It's for this very reason that it's often easier to have forms submit to themselves. You can do the processing at the top and, if there's errors, very easily populate the form. For example:

 

<?php
$errors = false;
if(count($_POST) > 0 ){
    //form has been submitted to process
    if(players_cost_too_much){
        $errors = true;
    }else{
        //show thanyou page or whatever       
    }
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if($errors){
    //print some error text
}
<input type="text" name="somefield" value="<?php echo (isset($_POST['somefield'] && $errors)) ? $_POST['somefield'] : '';?>" />
</form>

Thanks Bens! I'll give this a try later.  8)

 

It's for this very reason that it's often easier to have forms submit to themselves. You can do the processing at the top and, if there's errors, very easily populate the form. For example:

 

<?php
$errors = false;
if(count($_POST) > 0 ){
    //form has been submitted to process
    if(players_cost_too_much){
        $errors = true;
    }else{
        //show thanyou page or whatever       
    }
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if($errors){
    //print some error text
}
<input type="text" name="somefield" value="<?php echo (isset($_POST['somefield'] && $errors)) ? $_POST['somefield'] : '';?>" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.