0

I'm trying to create a multistep form that sends an email to me every time the form has been submitted. The form fully works except it won't send me the email. The code I used does work though for another test I did where the form is not a multistep form.

Here's the code I use:

<form <?php
          error_reporting(E_ALL);
          if(isset($_POST['submit'])){
                        $name=$_POST['name'];
                        $email=$_POST['email'];
                        $phone=$_POST['phone'];
                        $message=$_POST['message'];

                        $to='email@email.com'; // Receiver Email ID, Replace with your email ID
                        $subject='Form Submission';
                        $message="Name :".$name."\n"."Email :".$email."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$message;
                        $headers="From: ".$email;

                        if(mail($to, $subject, $message, $headers)){
                            echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly!</h1>";
                        }
                        else{
                            echo "Something went wrong!";
                        }
                    }
                ?> method="post" name="form" class="form-box">

        <div class="step step-1 active">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" id="name" name="name">
            </div><br>
            <button type="button" class="next-btn">Next</button>
        </div>

        <div class="step step-2">
            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" id="email" name="email">
            </div><br>
            <div class="form-group">
                <label for="phone">Phone Number</label>
                <input type="number" id="phone" name="phone">
            </div>
            <button type="button" class="prev-btn">Previous</button>
            <button type="button" class="next-btn">Next</button>
        </div>

        <div class="step step-3">
            <div class="form-group">
                <label for="message">Message</label>
                <textarea name="message" class="message-box" placeholder="Enter Your Message Here..."></textarea>
                <!--                    <input type="text" id="message" name="message">-->
            </div>
            <button type="button" class="prev-btn">Previous</button>
            <button type="submit" name="submit" value="Send" class="submit-btn">Submit</button>
        </div>

    </form>

Thanks in advance and I hope someone can help me figure it out.

  • 1
    Why is your PHP code _inside_ the `
    ` element?
    – brombeer Apr 29 '21 at 09:43
  • 1
    What @brombeer said and shouldn't you send the email _after_ the form has been submitted and not _before_ it's been sent to the client (PHP is executed on the server before it sends the response to the client/browser) since you seem to want to send the data from the form? – M. Eriksson Apr 29 '21 at 09:46
  • @brombeer I was required to have everything in the same file. I put it there because that's where you would otherwise put the action for the php file, and it worked for a normal one page for this way. @MagnusEriksson Where would you suggest the code to be instead of in the `
    ` element?
    – Jeffrey Hulleman Apr 29 '21 at 09:59
  • HTML (and CSS/JavaScript) are client side and PHP is server side. When you put a URL in the forms `action`, all that does is telling the browser what URL it should send the data to (as a new request to the server). You can't have inline PHP in actions in HTML. If you must have them in the same file (weird requirement), it's common to put the PHP before you start outputting the HTML. But... it should be on the next page, not the current page (as the form hasn't been submitted at that point, so your `$_POST`-array won't have any data). – M. Eriksson Apr 29 '21 at 10:07
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – M. Eriksson Apr 29 '21 at 10:07
  • You say that the form fully works so I'm assuming that you already have some PHP code that handles the submitted data? If so, that's where you should add your email script. If you don't have any PHP code that handles the submitted data, then I'm curious what you mean by "fully works". – M. Eriksson Apr 29 '21 at 10:11
  • @MagnusEriksson this thread partially helps me. It helps me understand how PHP works a bit better but it hasn't helped me solve my problem yet. What I meant with fully works is that I can go through the steps (using JavaScript) and once I click submit I get an array with the data. – Jeffrey Hulleman Apr 29 '21 at 11:13
  • If you go through the steps using JS and want to send an email after each step, you need to read up on Ajax and make an Ajax request to a PHP file that sends the email when switching step. Read up on Ajax and you'll might understand the flow better. – M. Eriksson Apr 29 '21 at 15:56

0 Answers0