PHP Form Not Working
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
isn't it what I said 😜
Copy link to clipboard
Copied
https://forums.adobe.com/people/B+i+r+n+o+u wrote
isn't it what I said 😜
I didn't see your post or you may have posted shortly before I did - 00:29/00:47. 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.
Copy link to clipboard
Copied
osgood_ a écrit
The php code won't work unless the 'safe' function is before the call to it.
well, I'm not a PHP guru, I'm most based on a JavaScript cleint/server approach, but writing and using the code downbelow never cause me any trouble ?
<?php
echo foo();
function foo() {
return 'hello World';
}
?>
Copy link to clipboard
Copied
https://forums.adobe.com/people/B+i+r+n+o+u wrote
osgood_ a écrit
The php code won't work unless the 'safe' function is before the call to it.well, I'm not a PHP guru, I'm most based on a JavaScript cleint/server approach, but writing and using the code downbelow never cause me any trouble ?
<?php
echo foo();
function foo() {
return 'hello World';
}
?>
I always write the function first and as I saw the function, in this particluar case, had been written after the call to it I moved the function to above the call and the script started to work as expected. Why that would be I don't know IF as you say a function can be written before or after. I just use the workflow that I always use and if it works it works. I don't really have time to consider how anyone else writes his/her code.
Edited.
I dont quite know what the exact problem is because if you wrap the php code to determine if the form submit button has been clicked the script doesnt run as expected if the 'safe' function is in its current location, however if you move the safe function to another location the script then runs as expected.
if(isset($_POST['submit'])) {
}
This may not be a problem for the OP as they have the script in a seperate file and the script runs if its in a seperate file without being wrapped:
if(isset($_POST['submit'])) {
}
Who knows, any php coders in this forum that can explain why wrapping the php script messes with the function in its current position?
Copy link to clipboard
Copied
what I said, is no matter if the function is wrote before or after being called in both case the script respond and works as expected
Copy link to clipboard
Copied
https://forums.adobe.com/people/B+i+r+n+o+u wrote
what I said, is no matter if the function is wrote before or after being called in both case the script respond and works as expected
Not for me it doesnt in the concept I described in my post.
Copy link to clipboard
Copied
yes but the OP script works either when the function is after...
Copy link to clipboard
Copied
https://forums.adobe.com/people/B+i+r+n+o+u wrote
yes but the OP script works either when the function is after...
It does seem to but not if you include the script in the same page as the form and have a need to wrap it in:
if(isset($_POST['submit'])) {
}
I cant understand why introducing that bit of code results in the function not working. If I remove the function it works or if I move the function it works....as soon as I re-introduce the function to its original position it fails again. Its not a big deal, I'm just puzzled.
Copy link to clipboard
Copied
is it not because the function is declared within a conditional test that it is not scanned upstream, as it would be if it were declared at the root of the code
Copy link to clipboard
Copied
https://forums.adobe.com/people/B+i+r+n+o+u wrote
is it not because the function is declared within a conditional test that it is not scanned upstream, as it would be if it were declared at the root of the code
Well I suppose it could be an answer. Maybe I've run into the issue in the past and I've just associated it with the function coming after the call to it so now I always write the function first and don't seem to have any issues. This stuff acts pretty weird at times.

