1

I'm trying to get the echo statement to run when the submit button is pressed. I have a name for the submit button and I'm using that in the if statement but the code does not run when I press the submit button. Thanks in advance.

<!DOCTYPE html>
<!-- Week3_Landis.php -->
<!-- Jul 3, 2018 -->
<html>
<head>
    <title>Lab3 Landis</title>
</head>
<body>
    <table border = "1">
        <form>
        <tr><td><h1>Welcome to the Diamond Maker!</h1></td></tr>
        <tr><td>Enter the size of your diamond: <input type="number" name="diamondSize"></td></tr>
        <tr><td><input type="submit" name="submit" value="Submit"></td></tr>
        </form>

        <tr><td>
        <?php
        // Row to create diamond
        if (isset($_POST['submit'])) {
            echo "stuff";
        } // End if



        ?>
        </td></tr>
    </table>
</body>
</html>
Andrew
  • 21
  • 3
  • 2
    you don't have a method set in your form tag. try
    – RyDog Jul 03 '18 at 23:02
  • @Carcigenicate it's not necessary to use AJAX. Set the `target` of the form to the same page with a `post` method and the php code will run as intended. – Richard Jul 03 '18 at 23:04
  • Your form needs an action and method. See [Your first HTML form](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Your_first_HTML_form) – WhereDidMyBrainGo Jul 03 '18 at 23:04
  • ALL it does not need a method attribute, but the default is `method="get"` So in this case there will be no data in the `$_POST` array it will be in the `$_GET` array – RiggsFolly Jul 03 '18 at 23:07
  • @smith NO, the OP could change the code to use `$_GET` _catch 22_ – RiggsFolly Jul 03 '18 at 23:09
  • @smith I would call if flexible, but whatever, certainly not worth falling out over – RiggsFolly Jul 03 '18 at 23:14

3 Answers3

3

The default submit method is GET and not POST. Use <form method="post">.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
3

You are missing one/two ideal arguments for your html form tag. Because PHP uses a server to process information, your form must go to the server and come back, thus requiring an action and a method in your starting form tag, as follows:

<form action="/YOUR_PAGE.php" method="post">

This way, when you hit submit, the form knows to send the information using the post method, and will return that same information back to the correct page.

1

Your <form> tag does not have an action or a method. You are not POSTing anything.

<form method="POST">
Mr Glass
  • 1,186
  • 1
  • 6
  • 14