PHP Need help How do I create a contact form?

Malcolm

Administrator
Administrator
Jan 2, 2018
1,015
277
95
Canada
codeforum.org
Hello Coders!

Looking to build a contact form for my website and was wondering how I should start. I heard that you require PHP, but other than that not sure what else I need to do. Can you please give me an idea, (don't just paste code, get me to work for it).
 

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
You need to first create the HTML.
Start with simple name & email form.

You need the <form> tag, and you will need to learn about the input tag, and the input type="text"

Then you can learn how to make an HTML button submit the form to a page using the POST method.

You can choose other languages besides PHP to save the data or send an email, but PHP is one of the easiest to learn and is available on most web hosts.

With PHP you should Google how to use the php tags to start your script, the isset() function to see if your form data arrived and the &_POST array which has your form data.

Happy searching :)
 
  • Like
Reactions: Malcolm

Tealk

Active Coder
Jun 5, 2019
169
39
28
SysAdmin
Anzah.Tools
So if you have required fields in your form. Then you can query the yes over JS or over PHP whether these are filled out. There are many forms that use JS and which I can send empty. :Rofl:
 

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
So if you have required fields in your form. Then you can query the yes over JS or over PHP whether these are filled out. There are many forms that use JS and which I can send empty. :Rofl:
Yeah, basically you can use JavaScript / HTML to display things to a user, like an input field required by the form and only allowing 10 characters.

However, you can edit the HTML in your browser to strip all of this away. If the site doesn't use PHP to verify the form is filled correctly, the user can submit disallowed data.

For example, let's pretend that CF wasn't as secure. Imagine the new thread form showing <input type="text" required="required" name="thread-title" />

If CF didn't use PHP to verify that $_POST['thread-title'] was sent by the form and more than 0 letters, then I could create a thread with no title.

Another worst example would be if the Edit thread page checked if I had permission to Edit the thread, but the PHP script used to modify the thread did not. That would allow me to send form data to the thread-Edit PHP script without having permissions to edit that thread ID. Basically, everything you secure / do on HTML needs to also be verified and secured with PHP.
 

Tealk

Active Coder
Jun 5, 2019
169
39
28
SysAdmin
Anzah.Tools
Or some other example I've just come across:
There are actually websites that link their PayWall with JavaScript. JavaScript an = PayWall. JavaScript off = free access.
 

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
Or some other example I've just come across:
There are actually websites that link their PayWall with JavaScript. JavaScript an = PayWall. JavaScript off = free access.
Yep, many news sites do this.
I commonly delete the HTML or fix up the JS to prevent or counteract soft paywalls
 

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
So I created a 'submit' button by using <input type="submit" value="Contact us">. Now I'm learning the post method!
Yep make sure that submit button is inside your form!

Make sure your form uses POST method and the correct target PHP script for the "action" attribute!
 

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
Alright done! Now figuring out how to process the form.
Sure, so you need to set up a way to accept the form data.
I made an example for you to work off of.

As you can see, the action="" tells the form where to send the data to, and it sets the method as POST. But you can also use GET, but that's less secure and not ideal for doing things with user submissions from a form.

In the process.php file, we use isset() to make sure the form was submitted in full.
We can then use things like is_int() or value comparisons ( >=, <=, ==, >, < ) from a standard math toolkit. In my example, I make sure the user is over 18 to continue.

Try to construct something similar with your own code :)
Once you are able to analyze the form data in your PHP file, you can move on to things like saving that data to a file

PHP:
<!-- MAIN HTML FILE -->
<form action="process.php" method="post">
  <input type="text" name="username" />
  <input type="text" name="age" />
  <input type="email" name="email" />
</form>

<?php
// PROCESS.PHP FILE:
if(isset($_POST['username']) && isset($_POST['age']) && isset($_POST['email'])){
  if(!is_int($_POST['age'])){
    echo "Please provide an accurage age.";
  } else if($_POST['age'] >= 18){
    /*
      User is over 18 years old,
      Check if Email is valid,
      Do something with username,
      etc etc etc
    */
    
    echo "Thanks for providing your details."
    
  } else {
    echo "You are not old enough to use this site.";
  }
} else {
  echo "You must supply your desired username, your email address, and your age.";
}
?>