-2

I can't seem to debug this as this is the first time I've used php. I initially had this setup and it actually worked but I tried adding some more radio buttons and it stopped working. I took the radio buttons out to try to get back to square one, but now I can't even get the simple form to send. I've been staring at it for hours, can you please help me figure out what's wrong. Thank you!

<?php
if($_POST["submit"]) {

    $recipient="testing@yahoo.com";
    $subject="Business Sign-Up Request";
    $senderName = $_POST["senderName"];
    $businessName = $_POST["businessName"];
    $phone = $_POST["phone"];
    $senderEmail = $_POST["senderEmail"];
    $website = $_POST["website"];
    $businessLicense = $_POST["businessLicense"];

    $mailbody = "Contact Name: $senderName\n\nBusiness Name: $businessName\n\nEmail: $senderEmail\n\nPhone: $phone\n\nBusiness License: $businessLicense\n\nWebsite: $website";

    mail($recipient, $subject, $mailbody);
    $thankYou="<p>Thank you! Your message has been sent.</p>";
}
?>

Here is the HTML form that is below the php code...

<?=$thankYou ?>
<div id="TestModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="container">
                <div class="row">
                    <div class="col-12 float-right">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                </div>
                <div class="row">
                    <div class="offset-2 col-8 text-center">
                        <h4 class="businessSignUp-title" id="myModalLabel">Business Sign-Up</h4>
                    </div>
                </div>
                <div class="row">
                    <div class="offset-1 col-10 text-center">
                        <h3>Please submit your information and we will contact you shortly!</h3>
                    </div>
                </div>
                <div class="row text-center"> 
                    <div class="col-12 text-center modal-body">
                        <form method="post" action="home.php" class="text-center">
                            <div>
                                <input class="w-100 py-1 px-2 businessSignUp-input mt-2" required name="senderName" placeholder="Name">
                            </div>
                            <div>
                                <input class="w-100 py-1 px-2 businessSignUp-input mt-2" required name="businessName" placeholder="Business Name">
                            </div>
                            <div>
                                <input class="w-100 py-1 px-2 businessSignUp-input mt-2" required name="phone" placeholder="Phone">
                            </div>
                            <div>
                                <input class="w-100 py-1 px-2 businessSignUp-input mt-2" required name="senderEmail" placeholder="Email Address">
                            </div>
                            <div>
                                <input class="w-100 py-1 px-2 businessSignUp-input mt-2" name="businessLicense" placeholder="Business License (optional)">
                            </div>
                            <div>
                                <input class="w-100 py-1 px-2 businessSignUp-input mt-2" name="website" placeholder="Website (optional)">
                            </div>
                            <input class="btn btn-primary mt-4" type="submit" name="submit">
                        </form>
                    </div>
                </div> 
            </div>
        </div>
    </div>
</div>
josephT
  • 822
  • 1
  • 7
  • 16
  • I also keep reading about enabling SMTP but I'm not sure what that is. – josephT Oct 04 '19 at 05:34
  • https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail have you looked at this? You can even try adding mail headers –  Oct 04 '19 at 05:38

2 Answers2

1

try to run the code with hardcoded values first to see if mail is working fine i.e only php part not html

<?php
    $recipient="testing@yahoo.com";
    $subject="Business Sign-Up Request";
    $mailbody="test message";
    var_dump(mail($recipient, $subject, $mailbody));
    // echo mail($recipient, $subject, $mailbody);
?>
bob_1982
  • 715
  • 6
  • 16
  • I did this and submit the form to call on the method, but still nothing. I also noticed that the thank you message isn't popping up either so I feel like its not even reaching the mail() function – josephT Oct 04 '19 at 06:35
  • @josephT hope you can debug and fix it now .if not i am here to help. please upvote or accept answer – bob_1982 Oct 04 '19 at 07:01
  • I just checked my error_log in my server and saw this : [04-Oct-2019 05:12:00 UTC] PHP Parse error: syntax error, unexpected '$recipient' (T_VARIABLE) in /home/buddyjan/public_html/home.php on line 4 – josephT Oct 04 '19 at 07:04
  • OKAY so the weirdest thing happened. I got rid of the action="home.php" in my form and it worked! I got the email immediately, but I don't understand how that made the difference. Is it because it kept reloading the page so it didn't read the actual method? It's so weird because I had that there before and it was working, so I'm not sure why it stopped working. – josephT Oct 04 '19 at 07:11
  • but now, everytime I reload the page, it sends the same form without me even inputting the values. – josephT Oct 04 '19 at 07:14
  • So it works now because I got rid of my action="home.php" which is itself. It works great unless someone tries to refresh the page and then they will get "The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?" I know that you can get rid of this if you put something in the action="" but when I do put something in there, the mail doesn't even get sent out. Does this information help? – josephT Oct 04 '19 at 07:57
  • this is because you used if($_POST['submit]) instead use if(isset($_POST['submit']) and it will work as expected – bob_1982 Oct 04 '19 at 14:33
  • I did if(isset($_POST['submit'])) { do stuff }, but it still is giving em that error of resubmission. but the mail itself does get sent out. – josephT Oct 07 '19 at 07:13
  • pl debug more and send me your code so that i can check it – bob_1982 Oct 07 '19 at 17:28
0

yes @Kamil007 is right you need host and host must be capable of sending email

check if mail function succeed or not using below code

var_dump(mail($recipient, $subject, $mailbody));
bob_1982
  • 715
  • 6
  • 16
  • This is all done online, not on my localhost. I had it working before, thats the frustrating part. Let me give that line of code a try! Thanks – josephT Oct 04 '19 at 06:13
  • I just did it, but how will I know if it succeeded or not? I am still not receiving the email. – josephT Oct 04 '19 at 06:16
  • @josephT what got printed like true,false – bob_1982 Oct 04 '19 at 06:17
  • on the DOM? Nothing is being printed. I'm also noticing that my page is refreshing after the submit button is hit. I know that will make the page refresh, but if I change it to a button instead, it breaks my code. I think maybe since it's refreshing, I'm not seeing anything on the DOM – josephT Oct 04 '19 at 06:19
  • 1
    try to run the code with hardcoded values first to see if mail is working fine i.e only php part not html – bob_1982 Oct 04 '19 at 06:21
  • So I manually wrote in some dummy text as the values and commented out the inputs in the form. So that the form is only the submit button, I clicked submit and the page refreshed again, but I didn't receive an email. I don't think I'm actually getting to the mail() funciton – josephT Oct 04 '19 at 06:30