ambit ambit - 4 months ago 15
HTML Question

PHP not working in contact us form

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>

Answer

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.