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>
2
  • Show the form that is submitted Commented Jun 13, 2014 at 0:21
  • Heya. Are you sure $_POST['Submit'] is set? Try var_dump($_POST['Submit']).. or check if you used a lowercase 'submit' in the HTML code. Commented Jun 13, 2014 at 0:23

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ah this fixed it. Why does it not recognize the type?
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.
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) {
    // ...
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.