Solved

PHP - Undefined Index Array Problem

Posted on 2016-07-21
3
54 Views
Last Modified: 2016-07-21
I am taking my first course in PHP and I found some logic I am using to build a Tic-Tac-Toe game.  However, for some reason the 0th position of the array is being ignored.  Here is my code

<?php

$box = array('', '', '', '', '', '', '', '', '');
if (isset($_POST["submitbtn"])) {
  $box[0] = $_POST["box0"];
$box[1] = $_POST["box1"];
  $box[2] = $_POST["box2"];
  $box[3] = $_POST["box3"];
  $box[4] = $_POST["box4"];
  $box[5] = $_POST["box5"];
  $box[6] = $_POST["box6"];
  $box[7] = $_POST["box7"];
$box[8] = $_POST["box8"];
  echo "<pre>";
print_r($box);
}
 ?>
<html>
  <head>
    <title>Tic-Tac-Toe</title>
  </head>
  <body>
    <form name="TicTacToe" method="post" action="test2.php"
          <?php
                 for ($i=0; $i<=8; $i++) {
  printf('<input type ="text" name="box%s" value="%s">', $i, $box[$i]);
  if($i == 2 || $i == 5 || $i == 8){
    print('<br />');
  }
}
          print('<input type="submit" name="submitbtn" value="Submit">');
          ?>
          
    </form>
  </body>
</html>

Open in new window


Here is a screenshot of he error.
https://gyazo.com/500b6db6b2ff591c9d80b2f21aa03370

As you can see when I put 'a' into the first element of the array it is showing it in the second element.  I want to know how it can be corrected to be in the first element.

Please keep in mind I can't use techniques I haven't been taught so please keep it simple.

Thanks
0
Comment
Question by:sharingsunshine
  • 2
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41722959
you don' t close the <form> tag properly...
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 41722960
<form name="TicTacToe" method="post" action="test2.php"
>
<?php
0
 

Author Closing Comment

by:sharingsunshine
ID: 41722974
Looked all over for that problem.  Thanks for finding it so quickly
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now