Cannot post my php file, using xampp - php

I'm trying to set up an HTML contact form on my website that will use PHP to send the information entered into the form to my email address.
Unfortunately, I keep getting an error that says Cannot POST /form.php and I can't figure out why.
I looked through similar questions that were asked here on StackExchange and the number one cause seemed to be that the PHP file wasn't in the same folder as the index.html file. However, both of my files are in the same folder; newwebsite/index.html and newwebsite/form.php.
So, why could this be happening?
Here's my index.html form:
<form method='post' action='form.php'>
<label>Name: </label>
<input type='text' name='vistorName' id='form-name' placeholder="Type your name.." required>
<br>
<label>Email: </label>
<input type='email' name='vistorEmail' id='form-email' placeholder="Type your email.." required>
<br>
<label>Msg: </label>
<textarea type='text' name='vistorMsg' id='form-msg' placeholder="Type your message here..." required></textarea>
<br>
<input type='submit' action="submit" value='Submit' id='submit-button' required><br>
</form>
And here's my form.php file:
<?php
//Pulling values that were entered into the form.
$name = $_POST['visitorName'];
$email = $_POST['visitorEmail'];
$message = $POST['visitorMsg'];
//Structure of email that I will receive with the form info
$email_from = "myemail#email.com";
$email_subject = 'New Contact Form Message';
$email_body = "You have received a new message from $name. \n".
"Here is the message: \n $message".
//Sending to my email address and using the mail function
$to = "zcericola#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
//Validating the form
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(\%0A+)',
'(\%0D+)',
'(\%08+)',
'(\%09+)',
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
if(IsInjected($email))
{
echo "Bad email value!";
exit;
}
?>
//HTML confirmation that message was sent.
<html lang='en'>
<h1>Your email has been sent. Thank-you!</h1>
</html>

With a few modifications, your code worked on my server.
Per Akintunde's comment, your form has typos in the field names:
<html>
<form method='post' action='form.php'>
<label>Name: </label>
<input type='text' name='visitorName' id='form-name' placeholder="Type your name.." required>
<br>
<label>Email: </label>
<input type='email' name='visitorEmail' id='form-email' placeholder="Type your email.." required>
<br>
<label>Msg: </label>
<textarea type='text' name='visitorMsg' id='form-msg' placeholder="Type your message here..." required></textarea>
<br>
<input type='submit' action="submit" value='Submit' id='submit-button' required><br>
</form>
Your "form.php" checks the e-mail address after attempting to send the e-mail. Also, html comments are identified differently than php comments. I made those two changes.
<?php
//Pulling values that were entered into the form.
$name = $_POST['visitorName'];
$email = $_POST['visitorEmail'];
$message = $POST['visitorMsg'];
if(IsInjected($email))
{
echo "Bad email value!";
exit;
}
//Structure of email that I will receive with the form info
$email_from = "myemail#email.com";
$email_subject = 'New Contact Form Message';
$email_body = "You have received a new message from $name. \n".
"Here is the message: \n $message".
//Sending to my email address and using the mail function
$to = "zcericola#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
//Validating the form
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(\%0A+)',
'(\%0D+)',
'(\%08+)',
'(\%09+)',
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
<html lang='en'>
<!-- HTML confirmation that message was sent. -->
<h1>Your email has been sent. Thank-you!</h1>
</html>

Related

HTML form PHP email not sending [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am trying to send an email upon submiting completed form from my site. however when ever i hit submit it only displays my php file as text and does not send the email.
below is all of my scripts could you please help me figure out why it wont send the email and display the thank you page?
My HTML File
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'>Enter Name: </label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Enter Email Address:</label>
<br>
<input type="text" name="email">
</p>
<p>
<label for='message'>Enter Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">
</form>
<br>
<br>
<br>
<br>
<script language="JavaScript">
var frmvalidator = new Validator("myemailform");
frmvalidator.addValidation("name", "req", "Please provide your name");
frmvalidator.addValidation("email", "req", "Please provide your email");
frmvalidator.addValidation("email", "email", "Please enter a valid email address");
</script>
My php File
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'nmckinney#chaoscomputerpro.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "nicholas.mckinney#outlook.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>

Strange Result With HTML contact/mail Form Using PHP File

I have a problem getting the data from my mail form being sent correctly in an email.
It does something strange with the email message.
Original form message:
Enter Name: Fake Name
Enter Email Address: fake.name#doesnotexist.com
Enter Message: Email test number 4(four).
Submit
_____________________________________________
The email message I received:
From: <fake.name#doesnotexist.com>
Subject: <New Form submission>
Reply to: <warnerheston#sungraffix.net>
To: Me <web289portfolio#sungraffix.net>
_____________________________________________
You have received a new message from the user Fake Name.
Here is the message:
Email test number 4(four).web289portfolio#sungraffix.net
_____________________________________________
The email in the message body is "(four).web289portfolio#sungraffix.net" and is underlined/hyperlinked... STRANGE!!
IT IS NOT SUPPOSED TO HAVE ANY EMAIL ADDRESS IN THE MESSAGE BODY AT ALL. THE USER DID NOT TYPE AN EMAIL ADDRESS AT THE END OF THE MESSAGE. SOMEHOW, THE PHP CODE IS DROPPING THE DESTINATION EMAIL ADDRESS INTO THE MESSAGE BODY AND ATTACHING THE LAST "WORD" OF THE USER'S MESSAGE TO THAT EMAIL ADRESS WHEN IT SENDS IT.
Can someone tell me what I am doing wrong?
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email)||empty($message))
{
header('Location: contact-form-incomplete.html');
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = $_POST['email']; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "web289portfolio#sungraffix.net";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: contact-thankyou.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
This is the HTML Form:
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'><span class='form'>Enter Name:</span></label><br>
<input type="text" name="name" size="36">
</p>
<p>
<label for='email'><span class='form'>Enter Email Address:</span></label><br>
<input type="email" name="email" size="36">
</p>
<p>
<label for='message'><span class='form'>Enter Message:</span></label> <br>
<textarea name="message" cols='48' rows='9' maxlength='300'></textarea>
</p>
<input type="submit" name='submit' value="submit"> <input type="reset">
</form>
Look carefully at your code on $email_body, you actually concatenate the $email_body with $to. the concatenation . at the end of your $email_body should be ;.

E-Mail Form and form values - php

I am trying to set a php mail form into my web site. First off all, I wanted to keep form values after submitting. if a user leave empty fields and press submit button, that user will has to write same things again. I was managed to do it with the structure <?php if (isset($_POST['Fields'])){echo htmlentities($_POST['Fields']); }?> However, I linked my form and php mail codes each other, after submitting form values are disappearing. I haven't managed to do it yet. I don't have knowledge about php. There are the codes:
My Form (inside contact.php):
<form method="post" action="mail.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" value="<?php if (isset($_POST['Name'])){echo htmlentities($_POST['Name']); }?>" />
<label for="Subject">Subject:</label>
<input type="text" name="Subject" id="Subject" value="<?php if (isset($_POST['Subject'])){echo htmlentities($_POST['Subject']); }?>"/>
<label for="Phone">Phone:</label>
<input type="text" name="Phone" id="Phone" value="<?php if (isset($_POST['Phone'])){echo htmlentities($_POST['Phone']); }?>"/>
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" value="<?php if (isset($_POST['Email'])){echo htmlentities($_POST['Email']); }?>"/>
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message">
<?php if (isset($_POST['Message'])){echo htmlentities($_POST['Message']); }?>
</textarea>
<input type="submit" name="submit" value="Send" class="submit-button" />
</form>
My php file (mail.php):
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['Name'];
$subject = $_POST['Subject'];
$phone = $_POST['Phone'];
$visitor_email = $_POST['Email'];
$message = $_POST['Message'];
//Validate first
if(empty($name)||empty($visitor_email)||empty($message))
{
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
exit;
}
if(IsInjected($visitor_email))
{
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
exit;
}
$email_from = 'updatedmail#mail.com';//<== update the email address
$email_subject = "Email from your web site";
$email_body = "You have received a new message via the web mail form \n".
"Name: $name \n".
"E-Mail: $visitor_email \n".
"Subject: $subject \n".
"Message: $message \n".
"Phone Number: $phone \n".
$to = "mymail#mymail.mail";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
you have to include mail.php in contact.php. So in contact.php, on top write
<?php include("mail.php");?>
And if mail.php inside some folder include that folder name too like below:
<?php include("foldername/mail.php");?>
And remove mail.php from action="mail.php"
Let me know, if it's not working. :)

Sending form details to Email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I am first to try this, I have given $to:my gmail when I click on submit it didn't show any error but I am not receiving any mail to my gmail. What's wrong? I did:
<html>
<body>
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'>Enter Name: </label><br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Enter Email Address:</label><br>
<input type="text" name="email">
</p>
<p>
<label for='message'>Enter Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">
</form>
</body>
</html>
PHP:-
form-to-email.php
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'tom#amazing-designs.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "kasani.prabha#GMAIL.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
And what about $email_from? It should be a valid email; I mean if I enter abc#abc.ac some wrong email while submitting, I will not receive the mail?
email from is not validated by the SMTP server but the problem will start when the end user is trying to reply back it will be delivered or bounce from to the email address added in email_from. SMTP provider also verify that you should be the owner of the email domain like in the case of
abc#abc.com
you should be the owner of the
abc.com
For this they give some kind of key which need to be added in domain DNS entry . The reason is to avoid the Spam.

very simple php issue I need assistance with

I am sorry for what is a very basic question but I am very new to web design and I am just trying to get my form to work with php.
The form fills in fine, and I receive an email but I don't get any body text. Instead of telling me what the message is I get this:
You have received a new message from the user fgafdfa.
Here is the message:
info#sodium3.com
As you can see the name is there (just typed gibberish) however the message is missing, instead I get my own email address appearing...
Here is the HTML for the form:
<div id="form">
<form action="php/send_form_email.php" method="post" >
<span>Name</span>
<input type="text" name="name" class="name" />
<span>Email</span>
<input type="text" name="email" class="email"/>
<span>Message</span>
<textarea class="message"></textarea>
<input type="submit" name='submit' value="submit" class="submit">
</form>
</div>
Here's the php:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'info#sodium3.com';//
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "info#sodium3.com";//
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: ../index.htm');
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
Can anyone help?
Two errors:
See the missing name in the HTML for the textarea. Corrected code:
<textarea name="message" class="message"></textarea>
For the code you don't end your assignment to $email_body with a semicolon but appended to it by a dot. Just a bit of luck that you didn't end up with a parse error. Corrected code:
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "info#sodium3.com";

Categories