0

So I'm trying to design a website that will allow the user to input data to design a tower. When it comes to the dimensions of basic parts of the tower (the height, width, and depth), I want to present them in a H x W x D format (i.e. 3 x 2 x 2). Trouble is, I'm having problems with concatenation in the PHP file for the second page of the website where everything's displayed.

Here's what my code looks like so far:

<!DOCTYPE html>
<html>
<head>
    <title>Results for tower design</title>
</head>
<body>
    Pillar base shape: <?php echo $_POST["tBase"]; ?><br>
    Pillar dimensions: <?php echo ($_POST["tHeight"];)." x ".($_POST["tWidth"];)." x ".($_POST["tDepth"];) ?><br>
</body>
</html>

It's highly possible that I might be making a common beginner error. If so, I want to know where I went wrong and how to avoid it.

  • 1
    From the manual http://php.net/manual/en/language.basic-syntax.instruction-separation.php *"As in C or Perl, PHP requires instructions to be **terminated** with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present."* – Funk Forty Niner Aug 04 '16 at 14:50
  • ...and running this, would have thrown you `Parse error: syntax error, unexpected ';' in...` but you failed to either check for errors or posted that error. – Funk Forty Niner Aug 04 '16 at 14:54

3 Answers3

2

You have some extra semicolons in improper places. A semicolon indicates the end of a PHP statement. You should not use them in the middle of building a string.

($_POST["tHeight"];)

Remove them and make sure you sanitize your output (or you'll be open to XSS attacks)

htmlspecialchars($_POST["tHeight"])." x ".htmlspecialchars($_POST["tWidth"])." x ".htmlspecialchars($_POST["tDepth"])

Running this, would have thrown you: Parse error: syntax error, unexpected ';' in...

Use error reporting during testing.

Plus, make sure your POST arrays contain values and that your form uses a post method with matching named attributes.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
0

Fun fact, you can have it all showed as a single line:

Pillar dimensions: <?php echo "{$_POST["tHeight"]} x {$_POST["tWidth"]} x {$_POST["tDepth"]}"; ?>

Every echo that is wrapped with double quotes can have variable been echoed just wrapping with {}.

Fabiano
  • 413
  • 3
  • 10
0

Remove semicolumns

<?php echo ($_POST["tHeight"] ." x ".$_POST["tWidth"] ." x ".$_POST["tDepth"]); ?>
ubm
  • 636
  • 11
  • 21