0

I am new to PHP and I am trying to make a contact us form that will perform validation and will display an error if any of the fields are not entered correctly.

I tried following this tutorial W3schools php example

so this is how my form looks like

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  E-mail: <input type="text" name="email">
  <span class="error">* <?php echo $emailErr;?></span>
  <br><br>
  Website: <input type="text" name="website">
  <span class="error"><?php echo $websiteErr;?></span>
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <span class="error">* <?php echo $genderErr;?></span>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

but I get an error saying

Error 404 - Not Found

The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.

and my url changes to this

my domain name/%3C?php%20echo%20htmlspecialchars($_SERVER[

what am I doing wrong here? Also if this is not the right approach, can someone make a suggestion how can I display the error messages and success messages after validating and sending the email via PHP?

Update:

Ok, so first of all, the problem was the file extension so thank you Chris for your helpful comment. If you want, add your comment as an answer and I will select it as a correct answer.

Second. I had a problem when upon changing the extension and my href attributes, the server was still taking me to contactUS.html page instead of contactUs.php page. The problem was that my links were generated in JavaScript and for some reason, my browser was not using the updated javaScript files. I solved this by clearing my cookies.

Saik
  • 993
  • 1
  • 16
  • 40
  • 1
    Do you have PHP installed, and is your file a `.php`? Your are being directed to the literal PHP. The PHP should have outputted the value of that variable to the DOM, then the HTML would direct correctly. – chris85 Mar 28 '18 at 18:42
  • This almost looks like your server is not interpretting PHP - what setup are you using? – Jakub Judas Mar 28 '18 at 18:42
  • I run my website on www.inmotionhosting.com so I am sure the server can run PHP since if I replace the action with the name of my php file, it works great. chris85, my file extension is .html. I guess that was the problem, trying with php extension right now. – Saik Mar 28 '18 at 18:49
  • @Saik That is your issue, it either needs to be `.php` or you need to make a new handler so PHP looks at `.html` files as well. – chris85 Mar 28 '18 at 18:51
  • Chris I changed the extension to .php but now when I am clicking on the link to go to my contactUs page, it is still trying to navigate to contactUs.html and since that file was renamed to contactUs.php, it says that the file does not exist. My links to that page look like this. Contact Us – Saik Mar 28 '18 at 19:01
  • @Saik Are you sure the page with the link is not getting cached somwehrere along the way? If you changed the link to .php, there is no reason why it should keep redirecting you to .php – Jakub Judas Mar 28 '18 at 19:20
  • Jakub that is exactly what was happening. My browser was caching .js files and since I was generating my links in javaScrtipt, the browser was using the old cached versions. It works great now. – Saik Mar 28 '18 at 19:31

1 Answers1

-1

Use double quotes arround php tag and single quotes for $_SERVER key. Like use below example then it will work for you.

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" >
chris85
  • 23,846
  • 7
  • 34
  • 51
  • That would make no difference `` ends it. The double quotes aren't in PHP. – chris85 Mar 28 '18 at 18:51
  • No. When PHP is working, there is no single parser that will treat both sets of quotes as having special meaning. This answer won't make any useful difference. – Quentin Mar 28 '18 at 18:52
  • Ya but can you please use below form tag and check once again ?
    – Ankit Patel Mar 28 '18 at 18:55
  • Ankit I actually caught that mistake and fixed it right after I made this post. It is still not working since my file had html extension and not php extension. – Saik Mar 28 '18 at 19:02