Undefined Offset errors

My form produces the correct information on the second page. However, before it displays the right answer I get several “Undefined offset” error message. If you notice the correct answer is posted beneath all the error messages.
Here is the code from the two pages.

first page:

    <td width="82"><div align="center">
      <input type="checkbox" name="128" value="128">
    </div></td>
    <td width="82"><div align="center">
      <input type="checkbox" name="64" value="64">
    </div></td>
    <td width="82"><div align="center">
      <input type="checkbox" name="32" value="32">
    </div></td>
    <td width="82"><div align="center">
      <input type="checkbox" name="16" value="16">
    </div></td>
  <td width="82"><div align="center">
    <input type="checkbox" name="8" value="8">
  </div></td>
  <td width="82"><div align="center">
    <input type="checkbox" name="4" value="4">
  </div></td>
  <td width="82"><div align="center">
    <input type="checkbox" name="2" value="2">
  </div></td>
    <td width="82"><div align="center">
      <input type="checkbox" name="1" value="1">
    </div></td>
    <td width="96"></td>
</tr>
128
64
32
16
8
4
2
1

Second PHP page:

<?php $var1 = $_POST['128']; $var2 = $_POST['64']; $var3 = $_POST['32']; $var4 = $_POST['16']; $var5 = $_POST['8']; $var6 = $_POST['4']; $var7 = $_POST['2']; $var8 = $_POST['1']; $add = $var1 + $var2 + $var3 + $var4 + $var5 + $var6 + $var7 + $var8; $sum = $add; echo "The answer is ", $sum; ?>

[php][/php]

Just on a quick glance… You are trying to add values that may not have been checked, therefore there is no value in the POST variable. You need to check if the POST value is set with isset or make sure it is not empty, then add it.

Well, I be I learn something new everyday, I didn’t think it was possible to do $_POST[‘128’] for example. Not that I personally would do that, but it nice to know that can be done. Sorry, for going off-topic. ;D

I want to say that this post is very good information to read up on. I’m reading things simple. Not too labyrinthine

It is now working. This code on the second page solved things. The hint was using the isset command.

$var1 = isset($_GET[‘128’]) ? $_GET[‘128’]:’’;
$var2 = isset($_GET[‘64’]) ? $_GET[‘64’]:’’;
$var3 = isset($_GET[‘32’]) ? $_GET[‘32’]:’’;
$var4 = isset($_GET[‘16’]) ? $_GET[‘16’]:’’;
$var5 = isset($_GET[‘8’]) ? $_GET[‘8’]:’’;
$var6 = isset($_GET[‘4’]) ? $_GET[‘4’]:’’;
$var7 = isset($_GET[‘2’]) ? $_GET[‘2’]:’’;
$var8 = isset($_GET[‘1’]) ? $_GET[‘1’]:’’;

Here is the code in action on my webserver.
http://cnjmug.com/subnet/index.html

Sponsor our Newsletter | Privacy Policy | Terms of Service