0

I want to create a form that calls PHP code within the same php file on submit.

Reading this, Why use $_SERVER['PHP_SELF'] instead of "", it seems I can use action="" instead of $_SERVER['PHP_SELF']; to do this.

If that's so, when I click submit, why isn't this PHP code at the top of the file executed?

if(isset($_POST['submit'])) {
    echo "submitted";
}

It should say submitted on the page.

In Google DevTools, the network tab doesn't show that the file is being called, just that the page is reloaded.

Form snippet:

<div id="contactForm">  
        <form role="form" action="" method='post' accept-charset='UTF-8'>
               <div class="row">
                   <div class="form-group col-xs-12 floating-label-form-group">
                         <input class="form-control" type="text" name="name" placeholder="Name">
                   </div>
               </div>
                <div class="row">
                    <div class="form-group col-xs-12">
                         <button type="submit" class="btn btn-lg btn-success">Send</button>
                    </div>
                </div>
         </form>
Community
  • 1
  • 1
user3871
  • 12,432
  • 33
  • 128
  • 268

3 Answers3

4

It says type="submit", not name="submit"..

LGT
  • 4,957
  • 1
  • 21
  • 22
  • Ah this fixed it. Why does it not recognize the type? – user3871 Jun 13 '14 at 00:30
  • Well, type tells the browser what it's for while (variable) name is used by PHP (in combination with value). So in this case, use both attributes.. or select a method from one of the other answers. – LGT Jun 13 '14 at 00:32
1

Because there is no variable in your form named "submit".

If you just want to test if the form has been submitted, you can use this:

if($_POST) {
    // ...
}
Max
  • 913
  • 1
  • 7
  • 18
0

You'll want to use if(isset($_POST)) (else you will get PHP notices) and try to change your action from "" to "#", also add the name="submit" attribute to your submit button.

Larry Williamson
  • 1,149
  • 5
  • 18