0

I'm new to PHP so forgive me if this is a dumb question. I'm trying to create a drop down list with multiple options. Then based off that list, once you hit submit, it generates a table with the appropriate information. As I'm writing this I'm starting to realize there has got to be a better way than constantly echo <tr> as i'm doing. Looking for any insight someone might have on this or a better/easier direction to take this

<h3>Configuration</h3>
<form method="post" action="">
 <label>Number:</label>
  <select name="outlets" id="outlets">
    <option value="blank">---</option>
    <option value="1 Outlet">1 Power Outlet</option>
    <option value="2 Outlets">2 Power Outlets</option>
    <option value="3 Outlets">3 Power Outlets</option>
    <option value="4 Outlets">4 or more Outlets</option>
  </select>
  <br><br>
  <label>Additional:</label>
  <select name="additional" id="additional">
    <option value="blank">---</option>
    <option value="noadd">None</option>
    <option value="1 Insert">1 Insert</option>
    <option value="2 Inserts">2 Inserts</option>
    <option value="3 Inserts">3 Inserts</option>
    <option value="4 Inserts">4 or more Inserts</option>
  </select>
  <br><br>
  <label>Style:</label>
  <select name="style" id="style">
    <option value="blank">---</option>
    <option value="Style 1">Style 1</option>
    <option value="Style 2">Style 2</option>
    <option value="Style 3">Style 3</option>
    <option value="Style 4">Style 4</option>
  </select>
  <br><br>
  <input type="submit" value="Submit">
</form>
<div>
    <h3>Selection:</h3>
    Power: <?=$_POST["outlets"]?><br>
    Inserts: <?=$_POST["additional"]?><br>
    Style: <?=$_POST["style"]?><br><br>
<?php
    if ($_POST["outlets"]=="1 Outlet" and $_POST["additional"]=="1 Insert" and $_POST["style"]=="Style 1") {
    echo "<table><thead><tr>";
        echo "<tr>";
        return '<img src="/image.html">';
        echo "</tr>";
    echo "</table></thead></tr>";
    } else {
    return "null";
    }
?>
</div>
Brandon
  • 374
  • 2
  • 15
  • 1
    I’m voting to close this question because this question would be better suited for https://codereview.stackexchange.com/. Aside, I'd also recommend changing your `return`s to `echo`. It's odd having a mix of both when outputting HTML to the page, and `return` is [better suited for returning from a function](https://www.php.net/manual/en/function.return.php) – WOUNDEDStevenJones May 27 '21 at 19:53
  • You might find these informative: [Which is better: to include HTML inside PHP code or outside it](https://softwareengineering.stackexchange.com/questions/180501/which-is-better-to-include-html-inside-php-code-or-outside-it) and [How can I echo HTML in PHP](https://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php). In short, ["Never echo static text or HTML"](https://softwareengineering.stackexchange.com/a/180504/356233). – showdev May 27 '21 at 19:57
  • @WOUNDEDStevenJones Sorry, i didn't even know codereview was a thing here. – Brandon May 27 '21 at 20:39

1 Answers1

0

You can switch back out of PHP:

<?php
  if ($_POST["outlets"]=="1 Outlet" and $_POST["additional"]=="1 Insert" and $_POST["style"]=="Style 1") { ?>
    <table>
      <thead>
        <tr>
          <td>Your Table Whatever</td>
        </tr>
      </thead>
    </table>
<?php } else { ?>
   Null
<?php } ?>

But really I would look into a templating engine, like https://twig.symfony.com/

dave
  • 62,300
  • 5
  • 72
  • 93