How do I send form results to email?

Hello to all it may concern, hope you’re having a great day…

I’m building a website for a clan me and a few mates have started up, and i’m trying to make a custom form due to the fact that the zyro form is limited to a certain amount of fields. So, i’m attempting to create my own form which i’ve done and i’ve styles it and what not. Now I have the issue of trying to make the form email the results to our clan email for now whilst I work on a members portal. I’ve followed steps from multiple tutorials, and none of them have worked at all. Could somebody possibly explain how this is done? I understand there are many similar topics, but none of them have an answer that works.

Hi @kwinkywolf

It’s better to use your own coding. As I can understand you are writing your own html and php right?

So…

HTML form will be like…

<form action="" method="POST"> 
   
    <input type="text" name="username">
    <input type="text" name="email">

</form>
  • You can add any number of input elements (even select, textarea elements). Then you must add a unique name attribute for each element.

  • action=""… empty value will tell the browser to send POST data to the current page. You can add any value for that according to your needs. (ex: action="do.php" will submit the form and send data to do.php page)

Then, you should do the backend…

<?php
    
     if ($_SERVER['REQUEST_METHOD'] === "POST") {
            // if it is a post request
          
           $name = $_POST['username']; // change username with the name attribute of your input field

           // then email the result to your email

           $email = 'youremail@emailhost.com';   //change this too ;) 
           $subject = 'I have no idea what to put as subject'; //:)
           $body = 'Someone has input ' . $name;

           mail($email, $subject, $body);

     }

?>

Add above code to your action script. If action script is same as the page that has the form, then you should add this to the top of the page.

However, I don’t know how you need to send the email (what content). But by this way you can send a input value in email. :wink:

If you have any question, please reply :slight_smile:

3 Likes

Wow… I had this all along earlier. I think the mistake I made, was that I made a second file and called it something then put the name of the file as the form action. That didn’t seem to work. But now it’s actually emailing me, the issue is that now it just emails the message and subject, it doesn’t actually email the result. It also has a note on the top of my page now…

Notice: Undefined index, firstname in /(filepath for page file) on line 8.

These are the two important pieces of code that would affect this I’d assume…

<?php

 if ($_SERVER['REQUEST_METHOD'] === "POST") {
        // if it is a post request
      
       $firstname = $_POST['firstname']; // change username with the name attribute of your input field

       // then email the result to your email

       $email = 'thegentlemensgaminggroup@gmail.com';   //change this too ;) 
       $subject = 'I have no idea what to put as subject'; //:)
       $body = 'Someone has input ' . $firstname;

       mail($email, $subject, $body);

 }

?>

and…

First Name: <br><input class="smalltextbox" type="text" name="firstname"><br><br>

Add following code to the top of your php code and reply your result.

var_dump($_POST); exit();

2 Likes

I go onto the page and it’s now just an entirely white screen with this text on the top left…

array(0) { }

Seems the post request isn’t being sent correctly.

@Supun @Supun @Supun
:slight_smile:

1 Like

Do you maybe have a solution for this?

Have you added method="POST"

OWH… I made a mistake. Please add that code inside first conditional. ($_SERVER['REQUEST_METHOD'] conditional)

1 Like

Apologies, that went right over my head xD it’s 3am and i’m exhausted… What is it I need to do?

Apologies, so I figured out some more stuff but still don’t understand what you want me to change in the conditional. If I swap what’s in there for what you just sent me, it doesn’t even load the page and returns saying something else.

I’m sorry for the late response.

I meant following code.

if ($_SERVER['REQUEST_METHOD'] === "POST") {
    // if it is a post request
   var_dump($_POST); exit();       

   $firstname = $_POST['firstname']; // change username with the name attribute of your input field

   // then email the result to your email

   $email = 'thegentlemensgaminggroup@gmail.com';   //change this too ;) 
   $subject = 'I have no idea what to put as subject'; //:)
   $body = 'Someone has input ' . $firstname;

   mail($email, $subject, $body);

 }

?>

This way you can check whether the params are set or not.

1 Like

array(0) { }

Still returns the above instead of my page

It means your POST isn’t sending. What is your webaddress

1 Like

Until it’s all made and we can upgrade for the piad version…

https://gggweb.000webhostapp.com/