Skip to main content
Known Participant
June 6, 2019
Question

PHP Form Not Working

  • June 6, 2019
  • 2 replies
  • 417 views

Hi, i'm creating a simple html/php form that shoots the message to my email.

However, whenever i submit the form, i do not receive an email. Here is the html and php code.

     <form id="contact-form" method="post" action="MailHandler.php">

                    <div class="row">

                        <div class="col-md-6">

                            <div class="form-group user-name">

                                <label name="nameFive" class="sr-only">Name</label>

                                <input type="text" class="form-control" required id="nameFive-first" placeholder="First Name">

                            </div>

                            <div class="form-group user-email">

                                <label name="emailFive" class="sr-only">Email</label>

                                <input type="email" class="form-control" required id="emailFive" placeholder="Email Address">

                            </div>

                            <div class="form-group user-phone">

                                <label name="websiteOne" class="sr-only">Website</label>

                                <input type="text" class="form-control" required id="websiteOne" placeholder="Phone">

                            </div>

                        </div><!-- /.col-md-6 -->

                        <div class="col-md-6">

                            <div class="form-group user-message">

                                <label name="messageOne" class="sr-only">Message</label>

                                <textarea class="form-control" required id="messageOne" placeholder="Write Message"></textarea>

                            </div>

                        </div><!-- /.col-md-6 -->

                    </div><!-- /.row-->

                    <button type="submit" class="btn btn-primary">Send Message</button>

                </form>

<?php

$errors = array();

if(empty($_POST['nameFive'])){

$errors[] = 'Please enter your name';

} else {

echo  "";

}

if(empty($_POST['emailFive'])){

$errors[] = 'Please enter your email';

} else {

echo "";

}

if(empty($_POST['websiteOne'])){ //thephonenumber

$errors[] = 'Please enter your phone';

} else {

echo  "";

}

if(empty($_POST['messageOne'])){

$errors[] = 'Please enter your comments';

} else {

echo  "";

}

  $emailPattern = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';

// $to = "[email address removed by moderator]";

//$to ="[email address removed by moderator]"

$to ="[email address removed by moderator]";

$subject = 'Advantage Analytical';

$from = 'Advantage Analytical';

$nameFive =  safe(stripslashes( $_POST['nameFive']) );

$emailFive =  safe($_POST['emailFive'] );

$websiteOne = safe($_POST['websiteOne'] );

//$cname =  safe ($_POST['cname']);

$messageOne = safe(stripslashes($_POST['messageOne']) );

$headers = "From: ". $from . "<" . $to. ">\r\n";

$headers .= "Reply-To: " . $emailFive . "\r\n";

$headers .= "Return-path: ". $emailFive;

  $message .= "Name:  " . $nameFive . "\n";

$message .= "Email: " . $email . "\n\n";

$message .= "Phone Number: " . $websiteOne . "\n\n\n";

//$message .= "Company Name:  " . $cname . "\n\n\n\n";

$message .= "Comments: " . $messageOne . "\n\n\n\n\n\n\n\n";

if (count($errors) < 1){

        mail($to,$subject,$message,$headers);

  echo "Thank you so much for your Inquiry. I'm looking forward in serving you soon!";

} else {

  echo "Please go back and enter the requested information";

}

function safe($string)

{

$pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";

return preg_replace($pattern, '', $string);

}

?>

Thanks for your assistance in advanced!

This topic has been closed for replies.

2 replies

Legend
June 7, 2019

Try moving your 'safe' php function code to just below the $emailPattern string:

$emailPattern = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';

function safe($string)

{

$pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";

return preg_replace($pattern, '', $string);

}

Also you need to add a 'name' attribute to each of your form fields so php can get the information typed into the input boxes:

<input type="text" name="nameFive" class="form-control"  id="nameFive-first" placeholder="First Name">

<input type="email" name="emailFive" class="form-control"  id="emailFive" placeholder="Email Address">

<input type="text" name="websiteOne" class="form-control"  id="websiteOne" placeholder="Phone">

<textarea class="form-control" name="messageOne"  id="messageOne" placeholder="Write Message"></textarea>

See if that helps.

B i r n o u
Legend
June 7, 2019

isn't it what I said ;p

Legend
June 7, 2019

https://forums.adobe.com/people/B+i+r+n+o+u  wrote

isn't it what I said ;p

I didn't see your post or you may have posted shortly before I did - The php code won't work unless the 'safe' function is before the call to it. At the moment no form error messages appear, so it appears that the script is compeltely broken, a blank page. Re-positioning the function solves that issue but as you point out unless each input has a 'name' attribute associated with the php variable its being stored in nothing will get sent when the form is submitted.

As for the mail() function, its a good point, one should always check to see if the host supports it.

B i r n o u
Legend
June 7, 2019

before going further deep in the PHP code, your HTML is wrong.

associating a LABEL tag and an INPUT tag rely on the FOR attribute for the first one and the ID attribute for the second one, then concerning the PHP transmit/recieve, one use the NAME attribute of the INPUT element...

second hand in your code, you didn't use the same relation you point at nameFive and you use nameFive-first

so you should reformt your HTML as

<label for="nameFive" class="sr-only">Name</label>

<input type="text" class="form-control" required id="nameFive" name="nameFive" placeholder="First Name">

<label for="emailFive" class="sr-only">Email</label>

<input type="email" class="form-control" required id="emailFive" name="emailFive" placeholder="Email Address">

<label for="websiteOne" class="sr-only">Website</label>

<input type="text" class="form-control" required id="websiteOne" name="websiteOne" placeholder="Phone">

<label for="messageOne" class="sr-only">Message</label>

<textarea class="form-control" required id="messageOne" name="messageOne" placeholder="Write Message"></textarea>

now going further in the PHP, be aware that if your provider didn't activate the mail() function, you won't get your message send...

you will have to use a SMTP and a PHP mailer.

personnaly I can't tell you if your PHP is cool... it looks like, but I can't test... my provider doesn't accept the mail() function...