orangutat orangutat - 7 months ago 9
HTML Question

I've been working on this table for days

I've been working on this for days and can't figure out what's wrong, it's for class. It keeps returning 'syntax error unexpected end of file which i think means i missed a brace somewhere but i don't see where? Any help would be appreciated. I have two files and i'm trying to make a multiplication table in php.

Page 1:

<!DOCTYPE html>
<html>
<head>
<title>Make a Multiplication Table!</title>
</head>
<body>
<form action="./timestable.php" method="POST"/>
<input type="number" value="1" name="a"/>
<input type="number" value="1" name="b"/>
<input type="submit"/>
</form>
</body>
</html>

<?php

$n = $_POST; // TODO what should $n really be? Replace the 10 with the user-supplied value from the form

// if we don't have a number, redirect back to the form page
if (isSet($n)) {
header("Location: ./mult_form.php");
exit;
}

?>


Page 2:

<!DOCTYPE html>
<html>
<head>
<title>Your Table is Ready</title>
</head>
<body>
<table>
<?php
$a = $_POST['a'];
$b = $_POST['b'];
for ($i = 0; $i <($a)+1; $i++):
{
$output.='<tr>';

for($j=1;$j<($b)+1;$j++)

{

$output.= '<td>'.($i*$j).'</td>';

}


$output.='<tr>';

}

$output.='</table>';




?>

</body>
</html>

pid pid
Answer

Apart from like 10 things you could/should change let's cut to the chase. You never output the $output variable. Use:

echo $output;

right after the $output.='</table>'; and you should be fine.


EDIT: corrected code as explained in comments.

That's the fully corrected page 1:

<?php

if (!isset($_POST['n'])) {
    header("Location: mult_form.php");
    exit;
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Make a Multiplication Table!</title>
    </head>
    <body>
        <form action="timestable.php" method="POST"/>
            <input type="number" value="1" name="a"/>
            <input type="number" value="1" name="b"/>
            <input type="hidden" value="<?php echo $_POST['n'];?>" name="n"/>
            <input type="submit"/>
        </form>
    </body>
</html>

Note that if you want to set a header field with header() you need to have a clear output buffer. This means not a single character has been output yet. Otherwise, the HTTP header will be automatically closed and the HTTP body will begin. This means that the header() comes to late and is actually ignored. So, no redirect if the PHP is after any HTML character. That's why I put it at the start of the code.

You will need to implement mult_form.php which posts the n field.

The second page is this:

<!DOCTYPE html>
<html>
    <head>
        <title>Your Table is Ready</title>
    </head>
    <body>
        <table>
            <?php 
            $a = intval($_POST['a']);
            $b = intval($_POST['b']);
            $n = intval($_POST['n']);
            for ($i = 1; $i <= $a; $i++)
            {
                $output .= '<tr>';
                for ($j = 1; $j <= $b; $j++)
                {
                    $output .= '<td>' . ($i * $j * $n) . '</td>';
                }

                $output .= '</tr>';
            }
            ?>
        </table>
    </body>
</html>

This should do it.

If this doesn't work be more specific, maybe post another question with your fresh code. This question should be considered fully answered now.

Have a nice day!

Comments