tags:

views:

153

answers:

2

Okay, so I have this partially working game, I know the solutions should be simple in terms of game play,but for a noob nothing is ever simple, so here, I am requesting help, as a noob, what am I doing wrong, I think it's the way I setup the check-boxes, but I need other eyes, thank you.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" />
<table border="1" align="center" />
<?php
$fire = $_REQUEST["fire"];
$variable = $_REQUEST["variable"];
$step = $_REQUEST["step"];
$i = $_REQUEST["i"];
$j = $_REQUEST["j"];

if(isset($_POST['$i$j']))
{ 
echo 'checked';
}

//first there will be script that randomly generate 5 ships for both users

$step = $_POST['step'];
//4 steps of the game
if ($step > 3) 
{
    $step = 1;
} 
else 
{
    $step += 1;
}//end if
//step 1, first player 
if ($step == 1) 
{
    echo "Player 1";
 // Loop through the alphabet from a to j and stop before k on this for loop
    for ($i = a; $i < k; $i++)
 {
 // Print out the table with the letters from a to j using the variable $i
     echo "<tr><td width='20' align='right'>$i</td>";
  // Loop through the numbers for $j using numbers from 1 to less than 11 which is 10;
       for ($j = 1; $j < 11; $j++)
    {

    echo "<td><input type='submit' value='fire' name='$i$j'></td>";
       } 
    // end for loop
     echo "</tr>";
    } 
 // end for loop
    echo "<tr><td></td>";

 for ($j = 1; $j < 11; $j++)
 {

     echo "<td>$j</td>";
    }
    echo "</tr></table>";
    //step 2 result of first player fire
} 

else if($step == 2)
 {

    echo "Result 1";
        for ($i = a; $i < k; $i++)
  {

     echo "<tr><td width='20' align='right'>$i</td>";
       for ($j = 1; $j < 11; $j++){
      //<input type="checkbox" name="state[]" value="NE">
         echo "<td><input type='checkbox' type='submit' checked='checked' name='$i$j'></td>";
       } // end for loop
     echo "</tr>";
    } // end for loop
    echo "<tr><td></td>";
    for ($j = 1; $j < 11; $j++){
     echo "<td>$j</td>";
    }
    echo "</tr></table><br><input type='submit' name='' value='Player 2 Turn'>";
    //step 3 second player fireing
} else if($step == 3) {
    echo "Player 2";
    for ($i = a; $i < k; $i++){
     echo "<tr><td width='20' align='right'>$i</td>";
       for ($j = 1; $j < 11; $j++){
         echo "<td><input type='submit' value='fire' name='$i$j'></td>";
       } // end for loop
     echo "</tr>";
    } // end for loop
    echo "<tr><td></td>";
    for ($j = 1; $j < 11; $j++){
     echo "<td>$j</td>";
    }
    echo "</tr></table>";
    //step 4 result of second player fire
} else {
    echo "Result 2 ";
    for ($i = a; $i < k; $i++){
     echo "<tr><td width='20' align='right'>$i</td>";
       for ($j = 1; $j < 11; $j++){
         echo "<td><input type='checkbox' checked='checked' disabled='disabled' name='$i$j'></td>";
       } // end for loop
     echo "</tr>";
    } // end for loop
    echo "<tr><td></td>";
    for ($j = 1; $j < 11; $j++){
     echo "<td>$j</td>";
    }
    echo "</tr></table><br><input type='submit' name='' value='Player 1 Turn'>";
}//end if
for ($j = 1; $j < 11; $j++){
       if ($checked){
         echo "<td><input type='checkbox' checked='checked' name='variable'></td>";
        } else {
         echo "<td><input type='checkbox' name='variable'></td>";
        }
       }  

?>

<input type="hidden" name="step" value="<?php echo "$step"; ?>"
</form>
</center>
</html>
+2  A: 

A couple of things I see wrong:

  • echo "<td><input type='checkbox' type='submit' checked='checked' name='$i$j'></td>"; has two type attributes.
  • <input type="hidden" name="step" value="<?php echo "$step"; ?>" does not close the input tag.
  • Your not closing your table tag.

Try running your page through an online html validation service or use the firefox validation addon to validate the page and fix the errors in your html.

Another issue is that your not validating all your input from the user. ie. your $_REQUEST data. This can lead to Cross-site scripting and can be dangerous to users. Although this is only trivial in this example, user input validation is a good practice to get into in all situations.

MitMaro
+1 for actually reading through and finding problems...
ck
Yeah, I kind of suspected problems with the check-box, I will review the correct way to setup a check-box, thanks. For the two type attributes, I should be able to incorporate an array right?Something like name[][]? I just learned about those and I think they are cool.I will probably skip the $_REQUEST, I am still learning about sessions and cookies and how to use them correctly.
Newb
Specifying two types doesn't have anything to do with the name. You need to set your input as `<input type='checkbox' checked='checked' name='$i$j'>` The `type='submit'` is making your checkbox into a submit button. As for the name you can use `name='node[$i][$j]' to get a multi-dimensional array in php. You can replace `node` with any string you wish.
MitMaro
A: 

I think that it might help you with your code, and debugging, to split the main php up from the html.

You have large blocks of php right in the middle of your html which makes it harder to see where you may have a problem. It's also easier to not echo html code but write it as html and then drop in php from variables where needed.

eg:

this is kinda messy

<?php
echo "Result 2 ";
    for ($i = a; $i < k; $i++){
     echo "<tr><td width='20' align='right'>$i</td>";
       for ($j = 1; $j < 11; $j++){
         echo "<td><input type='checkbox' checked='checked' disabled='disabled' name='$i$j'></td>";
       } // end for loop
     echo "</tr>";
    } // end for loop
    echo "<tr><td></td>";
    for ($j = 1; $j < 11; $j++){
     echo "<td>$j</td>";
    }
    echo "</tr></table><br><input type='submit' name='' value='Player 1 Turn'>";
 ?>

whereas this is a bit neater.

<?php
echo "Result 2 ";
    for ($i = a; $i < k; $i++){
?>

<tr>
    <td width='20' align='right'><?php echo $i; ?></td>";

    <?php
       for ($j = 1; $j < 11; $j++){
    ?>

    <td><input type='checkbox' checked='checked' disabled='disabled' name='<?php echo $i$j; ?>' /></td>

    <?php
       } // end for loop
    ?>

</tr>

    <?php
    } // end for loop
    ?>

    <tr>
        <td></td>

        <?php
        for ($j = 1; $j < 11; $j++){
        ?>

        <td><?php echo $j; ?></td>

        <?php
        }
        ?>

    </tr>
</table>
<br />
<input type='submit' name='' value='Player 1 Turn' />

This makes it look like more code, but it's more down to line spacing. As you have quite a lot for statements it doesn't help a great deal, but if you're using a coding program with tag highlighting or something like that you'll have a better chance finding html errors at the least and it could help make your php more readable.

not echoing html code also frees up the use of single or double quotes in the html.

andy-score
Someone really writes hell of a mess of HTML inlined with PHP these days?
n0rd
Will fix and make it easier for the eyes and debugging, thanks.
Newb