0

I am just a beginner with PHP & Html. I have seen many questions on SO related to this, but somehow cannot fix a very simple looking problem at my end. So, kindly help.

The following code does not seem to be working. There is always a message "There seems to be a problem right now. Please try again after sometime" before the form when I run this code. Whether I press the submit button or not, it does not make any difference.

<h2 >Inquiry form</h2>

<?php
    if (isset($_POST['submit'])){

        echo "Thank You!";
    }
    else {
        echo "There seems to be a problem right now. Please try again after sometime";
    }                                   

?> 

<form name="input" method="POST" action="contact.php">
    <label for="Name">Name (required):</label>
    <br />
    <input type="text" name="Name" />
    <br />
    <div class="clear"></div>
    <label for="inputmail">Email(required):</label>
    <br />
    <input type="text" name="email" />
    <br />
    <div class="clear"></div>
    <label for="inputtelefon">Phone:</label>
    <br />
    <input type="text" name="phone" />
    <br />
    <div class="clear"></div>
    <label for="inputmessage">Message:</label>
    <br/>
    <textarea name="message" cols="28" rows="3" ></textarea>
    <div class="clear"></div>
    <div id="send">
        <input type="submit" value=" Submit " />
        <input type="reset" value="  Clear  "  />
    </div>
</form>

Changed code:

<h2 >Inquiry form</h2>

                                  <?php
                                  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                            //if ($_SERVER['REQUEST_METHOD'] == 'POST') {  This is not working as well
                                        echo "Thank You!";
                                    }
                                    else {
                                        echo "There seem to be a problem right now. Please try again after sometime";
                                    }
                             ?> 

                       <form name="input" method="POST" action="contact.php">
                            <label for="Name">Name (required):</label>
                            <br />
                            <input type="text" name="Name" />
                            <br />
                             <div class="clear"></div>
                             <label for="inputmail">Email(required):</label>
                            <br />
                            <input type="text" name="email" />
                            <br />
                            <div class="clear"></div>
                            <label for="inputtelefon">Phone:</label>
                            <br />
                            <input type="text" name="phone" />
                            <br />
                            <div class="clear"></div>
                            <label for="inputmessage">Message:</label>
                            <br/>
                            <textarea name="message" cols="28" rows="3" ></textarea>
                            <div class="clear"></div>
                  </div>
                  <div id="send">
                <input type="submit" value=" Submit " name="submit"/>
                </div>
            </form>
ambit
  • 1,099
  • 2
  • 28
  • 43
  • You should not have whitespace inside your quotes also. And the opening

    tag should not have whitespace.

    – eipark Feb 25 '13 at 18:30

4 Answers4

7

That's because your submit button does not have a name:

<input type="submit" value=" Submit " name="submit"/>

Edit

Based on the comment of RandomCoder (in Marc B's answer).

Found this similar question: isset($_POST['submit']) vs $_SERVER['REQUEST_METHOD']=='POST'

Community
  • 1
  • 1
Tim
  • 9,351
  • 1
  • 32
  • 48
  • +1 right answer. Without a name, the value is not sent as part of the data and so `isset` won't see it. – Niet the Dark Absol Feb 25 '13 at 18:30
  • Yeah.. that was the mistake. That should have fixed it. But, surprisingly it is still not working for me. – ambit Feb 25 '13 at 18:33
  • Thanks everyone for helping me out. But it is still not working. I have posted the changed code below my previous code in the original question.. – ambit Feb 25 '13 at 18:42
  • horsefeathers! NAME attr needs to be sent this field with request to server – Igor Popov Feb 26 '13 at 11:16
  • As @Saju suggested, we should combine this with the accepted answer that should be the real answer. Right? – Tim Feb 01 '14 at 09:58
4

Invalid method of checking for a submission. Never check for the presence/absence of a form field. It's unreliable. You might change the field name, but forget to update the PHP. Use this instead:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ...
}

it's 100% reliable and will always be "true" if a POST was performed.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • But it may not necessarily be the POST you expect. I frequently have pages receive multiple possible POST requests and do different things accordingly. – Niet the Dark Absol Feb 25 '13 at 18:31
  • that's fine. check for specific form fields later. but if all you care about is actually having a post, this is the best method. – Marc B Feb 25 '13 at 18:32
  • 3
    +1, also Internet Explorer doesn't send the submit button as a post var if the enter key is used - [Why `isset($_POST['submit'])` is bad](http://www.crosscode.co.uk/php/why-using-isset-post-submit-is-not-compatible-with-internet-explorer/). – RandomCoder Feb 25 '13 at 18:34
  • @RandomCoder Really? Worked 100% of the time for me, in all the versions of IE I've used while developing (6 through 10) – Niet the Dark Absol Feb 25 '13 at 19:21
  • @Kolink yes this has been the case for all versions of IE, no version has ever resolved this issue. To test it: create a form with a textbox and submit button, set the action to itself, at the top of the page use `print_r($_POST)`. View the page, type something into the textbox and hit enter - you'll see that only the textbox is sent and the submit button is not included in the post data. – RandomCoder Feb 26 '13 at 09:50
  • @RandomCoder I stand corrected for IE7 and 8, but IE9 and 10 resolved the issue. Of course it's kind of a moot point since all my forms are AJAX-powered :p – Niet the Dark Absol Feb 26 '13 at 15:55
  • @Kolink I have just tested my IE9 and the issue is still there. I haven't got an IE10 to test but I very much doubt its fixed because MS simply don't see this a problem, in IE this behavior is by design (in theory not a bug). The onus is on the PHP devs to use good code here, rather than for MS to "fix" this. IE is probably not alone anyway, if you look at some specialized browsers such as screen readers. – RandomCoder Mar 01 '13 at 08:29
1

Take a look at your updated if statement. It is saying: if the form has been submitted then show "thank you", if the form hasn't been submitted then show "there seems to be a problem".

That's the reason you're always seeing "there seems to be a problem", because when you view the form without submitting it, the else block of your if statement fires.

You should remove the else block because it doesn't make any sense, so it becomes:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // form has been submitted, handle the data and say thanks...
    echo "Thank You!";
}

Using this method instead of checking for the submit button is better because Internet Explorer will not send the submit button as a post variable if the user pressed the enter key to submit the form.

More info - Why isset($_POST['submit']) is bad.

RandomCoder
  • 1,358
  • 5
  • 20
  • 35
  • Thanks. I figured that out later.. But forgot to update my question. But thanks a lot anyway.. – ambit Feb 26 '13 at 11:03
0

There is Only one reasons which I see i.e. you have not defined the name submit to any variable. There are two ways to find the submitting form as:

Either change:

 <input type="submit" value=" Submit " />

 to

 <input type="submit" value=" Submit " name="submit"/>

or add the extra line to do things something like this

 <input type = "hidden" name="passed" />

and to check

 <?php
      if (isset($_POST['passed'])){
          echo "Thank You!";
      }else {
           echo "There seems to be a problem right now. Please try again after sometime";
      }                                   

 ?> 
Vineet1982
  • 7,730
  • 4
  • 32
  • 67