Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP How do I create a contact form?

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
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).
 
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 :)
 
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:
 
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.
 
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.
 
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
 
So I created a 'submit' button by using <input type="submit" value="Contact us">. Now I'm learning the post method!
 
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.";
}
?>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom