Contact Form is not displaying in php script - php

I have created a code snippet (given below) in php script for CONTACT Form. The Form is not displaying itself when there is no error shown in the Console. One issue is raised saying that "Audit usage of navigator.userAgent,navigator.appVersion, and navigator.platform".
I am using Windows 10 and sublime text editor. React JS is also installed in my computer.
Please help me to resolve the issue.
<?php
if(isset($_POST['send'])) {
$to_mail = $_POST['tomail'] ;
$subject = $_POST['subject'] ;
$body = $_POST['message'] ;
$from_mail = $_POST['frommail'];
$header = "From: $from_mail" ;
mail($to_mail, $subject, $body, $header) ;
if(mail($to_mail, $subject, $body, $header)){
echo "We have sent a mail to your mail id. Kindly check and confirm." ;
}else{
echo "No mail could be sent to you." ;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include 'header.php' ; ?>
</head>
<body>
<div class="container-fluid" id="container-contact">
<div class="row">
<div class="col col-12">
<form class="cform" action="<?php echo htmlentities($_SERVER('PHP_SELF')) ;?>" method = "POST">
<div class="form-group">
<label>To</label>
<input type="email" class="form-control" name="tomail"> <br><br>
<label>Subject</label>
<input type="text" class="form-control" name="subject"> <br><br>
<label>Message</label>
<textarea type="text" class="form-control" name="message" `enter code here`cols="40" rows="15"></textarea> <br><br>
<label>From</label>
<input type="email" class="form-control" name="frommail"> <br><br>
<button><input type="submit" name="send" value="Send"></button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

#Subhajit
line number 37 : you have use the small bracket , but $_SERVER is the array and you have to use the big bracket [ and ]
<?php echo htmlentities($_SERVER('PHP_SELF')) ;?>
solution e.g echo htmlentities($_SERVER['PHP_SELF']) ;
there was the error "fatal error Function name must be a string" that due to you have used the server variable properties with small brackets.

Related

HTTP ERROR 405 on submitting HTML form when using PHP mail

I am trying to send mail to my account using PHP but it show HTTP ERROR 405 when i submit my HTML form.
Here's my HTML form:
<form action="spledmailer.php" method="post" enctype="text/plain" class="needs-validation" novalidate>
<div class="container text-center w-50">
<input type="text" class="form-control" placeholder="Enter Your Name" name="name" required><br>
<input type="email" class="form-control" placeholder="Enter Your E-mail" id="email" name="Email" required><br>
<textarea class="form-control" rows="5" placeholder="Tell Us About Your Request" name="query" required></textarea>
</div>
<div class="container" style="margin-top: 3%;">
<div class="row">
<div class="col text-center">
<button type="submit" class="btn btn-outline-dark">Send Query</button>
</div>
</div>
</div>
</form>
And here's my PHP code:
if(isset($_POST['submit'])) {
$to = "example#gmail.com";
echo $subject = "Form Tutorial";
echo $name_field = $_POST['name'];
echo $email_field = $_POST['Email'];
echo $message = $_POST['query'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
If you are using a web host, you might want to ask them if they block POSTing to certain files, or just the php mail function in general. This has been a problem for me in the past, so contact their support.
Also, make sure you enable error reporting or look through your error log. That can probably tell you what the problem is. See here: How can I get useful error messages in PHP?

Email not sending with PHPmail function [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
My page structure is the following
-index.php
-include send-form.php
-include contact-us-form.php
-include footer.php
my index.php file is the following (shortened)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<?php include 'navigation-bar.php';?>
<?php include 'contact-us-form.php';?>
<?php include 'footer.php';?>
</body>
below is my contact-us-form
<div class="container">
<div class="row">
<div class="column">
<form method="post">
<label for="name">Name*</label>
<input type="text" id="name" name="name" placeholder="Your name.." required>
<label for="email">Email*</label>
<input type="text" id="email" name="email" placeholder="Your email.." required>
<label for="message">Message*</label>
<textarea id="message" name="message" placeholder="Your Message" style="height:170px" required></textarea>
<div style="text-align: center;">
<input type="submit" value="submit" >
</div> <!-- /submit button -->
</form> <!-- /form -->
<div id="error_message" style="width:100%; height:100%; display:none; ">
<h4>Error</h4>
Sorry there was an error sending your form.
</div>
<div id="success_message" style="width:100%; height:100%; display:none; ">
<h2>Success! Your Message was Sent Successfully.</h2>
</div>
</div><!-- /column -->
<div class="column2">
</div> <!-- /column2 -->
</div> <!-- /row-->
</div> <!-- /container -->
below is my send-form.php
if (isset($_POST["submit"])){
$to = '***************#gmail.com';
$subject = 'Message from website';
$message = 'MESSAGE: ' . $_POST [ "message" ] . ' </n> NAME: ' . $_POST['name'] ;
$from = $_POST[ "email" ];
if(mail($to, $subject, $message)){
echo 'message sent';
} else{
echo 'Unable to send email. Please try again.';
}
}
For some reason the email isn't sending. I can get the email to send if I put a action="/send-form.php" in the contact form but this bring me to another page.. What I am trying to do is for the user to send the form and stay on the same page where the message which is hidden will then be unhidden when clicked.. Any help on why my email isn't sending would be greatly appreciated!
I see you used $_POST["submit"] in your code. But you didn't define it on your form.
<input type="submit" value="submit">
Just put name="submit" on it and the problem must be solved.
<input type="submit" name="submit" value="submit">

Still Can't Get an HTML Form to Submit to Email with PHP [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I've been reading and attempting solutions for the past 4 hours, but I'm still no closer to figuring it out. I've got an HTML form, and I want it to be submitted as an email to me. PHP seems like the only way to do that, but I can't figure it out for the life of me. I've tried making < form action="form-to-email.php >, I've tried making the HTML document itself PHP and < form action="" >. I've tried using the PHP code in the HTML, and vice versa. I've tried at least ten different versions of PHP code to take the information from the form and send an email, but the only time I was able to get the email to send, it wouldn't take any info from the form. Can somebody please tell me what I'm doing wrong and how to fix it?
HTML
<div class="row contact-form">
<form action="form-to-email.php" method="POST" enctype="text/plain" id="contact-form">
<div class="col-md-4">
<label for="name" class="required">Name:</label>
<input name="name" type="text" id="name" maxlength="40" />
</div> <!-- /.col-md-4 -->
<div class="col-md-4">
<label for="email" class="required">Email:</label>
<input name="email" type="text" id="email" maxlength="40" />
</div> <!-- /.col-md-4 -->
<div class="col-md-4">
<label for="subject">Subject:</label>
<input name="subject" type="text" id="subject" maxlength="60" />
</div> <!-- /.col-md-4 -->
<div class="col-md-12">
<label for="message" class="required">Message:</label>
<textarea name="message" id="message" rows="6"></textarea>
</div> <!-- /.col-md-12 -->
<div class="col-md-12">
<div class="submit-btn">
<input class="largeButton contactBgColor" type="submit" value="SEND MESSAGE" />
</div> <!-- /.submit-btn -->
</div> <!-- /.col-md-12 -->
</form>
</div>
PHP
<?php
// Debugging tools. Only turn these on in your development environment.
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
// Special mail settings that can make mail less likely to be considered spam
// and offers logging in case of technical difficulties.
ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);
// The components of our email
$to = 'name#domain.com';
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email = $_POST['email'];
$headers = implode("\r\n", [
'From: email',
'Reply-To: email',
'X-Mailer: PHP/' . PHP_VERSION
]);
$email_from = 'name';
$email_subject = 'subject';
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
// Send the email
$result = mail($to, $subject, $message, $headers);
// Check the results and react accordingly
if ($result) {
// Success! Redirect to a thank you page. Use the
// POST/REDIRECT/GET pattern to prevent form resubmissions
// when a user refreshes the page.
header('contact-form-thank-you.html', true, 303);
exit;
}
else {
// Your mail was not sent. Check your logs to see if
// the reason was reported there for you.
}
At this point I'm just getting a string of garbled error messages, no emails, and a withering sanity. I've read this page up and down, and none of it helped. My test site is here, and the contact form is on the bottom of the landing page. PHP is honestly making less and less sense the more I deal with it. I'm sorry that the previously posted solutions aren't working for me, but I've tried everything I know how to, and I still feel no closer to resolving this. I would really appreciate any advice you can offer to help me through this!
For those asking about the error messages, this is what I was getting:
int(8) string(21) "Undefined index: name" string(41) "/home/lanpoint/public_html/contact us.php"
This should work:
HTML
<div class="row contact-form">
<form action="form-to-email.php" method="POST" id="contact-form">
<div class="col-md-4">
<label for="name" class="required">Name:</label>
<input name="name" type="text" id="name" maxlength="40" />
</div> <!-- /.col-md-4 -->
<div class="col-md-4">
<label for="email" class="required">Email:</label>
<input name="email" type="text" id="email" maxlength="40" />
</div> <!-- /.col-md-4 -->
<div class="col-md-4">
<label for="subject">Subject:</label>
<input name="subject" type="text" id="subject" maxlength="60" />
</div> <!-- /.col-md-4 -->
<div class="col-md-12">
<label for="message" class="required">Message:</label>
<textarea name="message" id="message" rows="6"></textarea>
</div> <!-- /.col-md-12 -->
<div class="col-md-12">
<div class="submit-btn">
<input class="largeButton contactBgColor" type="submit" value="SEND MESSAGE" />
</div> <!-- /.submit-btn -->
</div> <!-- /.col-md-12 -->
</form>
</div>
Form-to-email.php
<?php
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$subject = strip_tags($_POST['subject']);
$message = strip_tags($_POST['message']);
$from = $email;
$to = 'youremail#address.com;
$body = "From: $name\n Subject: $subject\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Success!';
} else {
echo 'Failure';
}
}
?>

don't send the form message if input is empty

i do have a little problem with my contact.php :(
I needed for my page a contact form (very simple, just name, email and message).
It works perfectly but it sends the message even if the inputs are empty. My inputs do have "required" but it doesn't seem to work. My php skills are not that great. But you understand for sure where the problem is.
If there are empty fields, the form should not send the message and show an alert.
My current code:
HTML
<form method="post" id="myform" action="contact.php">
<div class="field half first">
<label for="name">Ihr Name</label>
<input type="text" name="name" id="name" required="required">
</div>
<div class="field half">
<label for="email">Ihre E-Mail</label>
<input type="text" name="email" id="email" required="required">
</div>
<div class="field">
<label for="message">Ihre Nachricht</label>
<textarea name="message" id="message" rows="5" required="required"></textarea>
</div>
<ul class="actions">
<li>
Senden
</li>
</ul>
</form>
contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'mail#gmail.com';
$subject = 'Nachricht von '.$field_name;
$body_message = 'Von: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Nachricht: '.$field_message;
$headers = 'Von: '.$field_email."\r\n";
$headers .= 'Antworte an: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Ich bedanke mich für Ihre Nachricht. Bald werde ich Sie kontaktieren./ Thank you for your message.');
window.location = 'http://';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Senden fehlgeschlagen./ Could not send the message');
window.location = 'http://';
</script>
<?php
}
?>
Any ideas, how to fix that small problem? :(
I think if your goal is to prevent someone send a blank message/contact, just use "required" in the end of your input tag like this :
<input type="text" name="something" required/>
or in your case, it should be like this :
<form method="post" id="myform" action="contact.php">
<div class="field half first">
<label for="name">Ihr Name</label>
<input type="text" name="name" id="name" required>
</div>
<div class="field half">
<label for="email">Ihre E-Mail</label>
<input type="text" name="email" id="email" required>
</div>
<div class="field">
<label for="message">Ihre Nachricht</label>
<textarea name="message" id="message" rows="5" required></textarea>
</div>
<ul class="actions">
<li>
Senden
</li>
</ul>
</form>
The "required" is a simple html5 tag attribute that helps you prevent someone send a blank form. I hope it works on you, make sure you are working with html5.
There is a function in PHP called empty(). You can combine this with the $_POST function like this example:
<?php
// The form is submitted.
if(isset($_POST["submit_button"])) {
// Now check if the posted input element is empty, if empty stop by echo a error message
// Otherwhise continue executing script
if(empty($_POST["form_name"])) {
echo "You forgot to fill in this form-element.";
}else{
// Continue
}
}
?>
Read more about this function: http://php.net/manual/en/function.empty.php
Check if the form is submitted using isset() function:
isset — Determine if a variable is set and is not NULL
Then, check if the fields are filled using empty() function:
empty — Determine whether a variable is empty
// this will return true if the Submit Button is clicked
if(isset($_POST["submit_button"])) {
if(empty($_POST['name']) {
echo "Name is not filled";
} else if (empty($_POST['email']) {
echo "Email is not filled";
} else if (empty($_POST['message']) {
echo "Message is not filled";
} else {
// Your original code
}

Unable to get informations from a Html5 CSS and php form

I try to build a html5 form and send informations to form.php
<h3>Contact Me</h3>
<p></p>
<form method="post" action="form.php">
<div class="row uniform">
<div class="6u 12u(xsmall)">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u 12u(xsmall)">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row uniform">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row uniform">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row uniform">
<div class="12u">
<ul class="actions">
<li>
<input type="submit" class="special" value="Send Message" />
</li>
<li>
<input type="reset" value="Reset Form" />
</li>
</ul>
</div>
</div>
</form>
At the same place I have form.php
<?
if (isset($_POST['name']) && isset($_POST['email'])) {
echo 'name '.$_POST['name'].' mail '.$_POST['email'];
}
?>
When I click on submit form.php page is open but blank.
<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
echo 'name '.$_POST['name'].' mail '.$_POST['email'];
}
?>
I think you missed the opening of PHP tag its <?php and not <? .
Yes sorry, now I can send by mail.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: lespizz';
$to = 'jim#fdfrdsfg.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body, $from)
?>
do you know how to get back on index.html automatically ?
Thanks
Using PHP you may redirect to another page with this code :
header("Location: index.html");
die();
But beware - it will work only if there was no output before!
Or if you have to give some output earlier you may just echo Javascript to redirect :
echo '<script type="text/javascript">
window.location = "http://www.google.com/"
</script>';

Categories