PHP Undefined variable for HTML Form [duplicate] - php

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 5 years ago.
I am trying to run a contact form PHP script and HTML form and when I run it I get the following errors.
Notice: Undefined variable: name in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 17
Notice: Undefined variable: p in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 18
Notice: Undefined variable: message in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 18
Notice: Undefined variable: visitor_email in /storage/ssd4/333/4197333/public_html/heros51/confirm.php on line 21
<form action="confirm.php" method="POST" >
<label>Name >></label><input id="name" type="text" /><br />
<label>Email >></label><input id="email" type="text" /><br />
<label>PRI >></label><input id="p" type="text" /><br />
<label>Message >></label><textarea id="message" rows="1"></textarea><br /><br /><br />
<input type="submit" value="submit" />
<a class="button" alt="" href="index.html">Cancel</a>
</form>
The PHP which is linked looks like this:
if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['p']) && isset($_POST['message']) ){
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$p = $_POST['p'];
$email_from = 'default03#securemail.com';
$email_subject = "New Form submission $name ";
$email_body = "You have received a new message from the user $p.\n". "Here is the message:\n $message".
$to = "erixom#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
}
else
echo " ISSET FAIL";
$email_from = 'default03#securemail.com';
$email_subject = "New Form submission $name ";
$email_body = "You have received a new message from the user $p.\n". "Here is the message:\n $message".
$to = "test#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>

instead of id="name" you must use name="name". The id attribute is used for JavaScript manipulation while the name attribute is used to name your POST variables
I would like to add that you should sanitize user submitted inputs. Even tho this might not be a internet facing project, it's never too early too adopt this as a habbit :)

Related

php mail function not sending mail in html website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
i have a website which is built in HTML and PHP . now to send mail I have made a contact form which looks like the following:
<form class="" method="post" dat-email="" action="">
<input type="text" class="form-control" name="name" placeholder="Name" required>
<input type="text" class="form-control" name="email" placeholder="Email" required>
<textarea class="form-control" name="message" placeholder="Your Message" required></textarea>
<input name="submit" class="def-btn" type="submit" value="submit">
</form>
for the form to work I have added the following PHP mail function:
<?php
if(isset($_POST['submit'])){
$headers .= "Return-Path: The Sender <zubairnazer#gmail.com>\r\n";
$headers .= "From: The Sender <zubairnazer#gmail.com>\r\n";
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
$to = "zubairnaze#gmail.com";
$from = $_POST['email'];
$first_name = $_POST['name'];
$last_name = $_POST['message'];
$subject = "The Nosh Bistro Contact";
$message = $first_name . " has sent the following message. " . "\n\n" . $last_name. "\n\n"."Email " .$from. "\n\n" ;
mail($to,$subject,$message,$headers);
echo "<script type='text/javascript'>alert('Message Sent Successfully!')</script>";
}
?>
before it was working fine when I used only one header, but now I added these many headers so that my mail doesn't goes to spam folder. but now the mail function is not sending mails to my gmail id and I am getting the following error:
Notice: Undefined variable: headers in /hermes/walnacweb05/walnacweb05aa/b185/as.moluguz/nosh/index.php on line 1785
can anyone please tell me what could be wrong here

Why the output is showing undefined "submit" in 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 2 years ago.
Actually I was creating a simple form to send mail to my email ID.
Here's my code:
<!doctype html>
<html>
<body>
<form>
<input type='text' name='name'>
<input type='email' name="email">
<input type="number" name="mobile">
<input type="text" name="message">
<input type="submit" name='submit'>
</form>
</body>
</html>
php code:
<?php
if($_POST['submit']!="")
{
$cname = $_POST['name'];
$email = $_POST['email'];
$mob = $_POST['mobile'];
$query = $_POST['message'];
$subject="Enquiry";
$message="Name : $cname \n email : $email \n Message : $query \n";
$email_from = '<name#gmail.com.com>';
$subject = "registration";
$message = "You have received a new message from the user <b> $cname. </b> \n". "Here is the message:\n $message".
$to = "name<name#gmail.com>";
$headers = "From: $email_from \r\n";
$headers.= "Reply-To: $email \r\n";
if(mail($to, $subject, $message, $headers))
{
echo "<script>alert('Dear User, You Are Successfully Registered')</script>";
}
}
?>
Note: In this question I have changed my actual email id with "name", in my actual code, the id is putten correctly.
The error is in the first if statement that the 'submit' is undefined.
If you load the page, $_POST is empty and $_POST["submit"] is undefined. If you click the submit button, $_POST["submit"] is set. So, instead of checking if the content of $_POST["submit"] is an empty string, check if the variable is set:
if( isset($_POST["submit]) ){
// Form submitted, continue
}
Also, you forgot to set the method in your form:
<form method="post">

PHP Notice: Undefined index - Contact Form not working [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.
so this is a Contact Form php script with basic validations. This script was running absolutely fine without any issues or errors. Until recently I transferred the file to another web hosting.
The previous web host had PHP version 5.4.35
While the new web host has PHP version 5.4.45
I don't know much about PHP so I don't know what's going on. Here's what the error_log had logged everytime someone submitted the contact form.
[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: name in /home/domain/public_html/contact.php on line 70
[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: email in /home/domain/public_html/contact.php on line 76
[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: message in /home/domain/public_html/contact.php on line 82
In order to solve this I initialized the error variables ($name,$email,$message) as null in that case there was no more errors but the contact form failed to work.
Please help me! I don't know why this problem is occuring.
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$to = 'example#domain.com';
$subject = 'Contact Form';
$header = "From:contact#domain.com \r\n";
$header = "Cc:contact2#domain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
if ($human !== 2) {
$errHuman = 'Your anti-spam is incorrect';
}
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail($to, $subject, $body, $header)) {
$result='Thank You! Your message will be replied soon!';
} else {
$result='Sorry there was an error sending your message.';
}
}
}
?>
<form class="col l12" method="post" action="contact.php">
<input id="name" name="name" type="text" class="validate" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='red-text'>$errName</p>";?>
<label for="name">Name</label>
<input id="email" name="email" type="email" class="validate" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='red-text'>$errEmail</p>";?>
<label for="email">Email</label>
<textarea name="message" class="materialize-textarea"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='red-text'>$errMessage</p>";?>
<label for="message">Message</label>
<label for="human"><strong>AntiSPAM Check:</strong> 5 - 3 = ?</label>
<input id="human" name="human" type="text" class="validate">
<?php echo "<p class='red-text'>$errHuman</p>";?>
<p class="left-align"><button class="blue darken-1 btn-large waves-effect waves-light" id="submit" type="submit" style="font-weight:500;" name="submit">Send</button>
<?php echo $result; ?>
</form>
Those errors are telling you that your $_POST array doesn't have name or email or message. Someone submitted an empty form.
Now, you check for missing values later on in your script, but not until after you've already tried to access these missing array values.
Best thing would be to move your validation code...
if (!$_POST['name']) {
...
...to the top so it runs first. Then, only do $name = $_POST['name']; once you're sure that it exists.

php throwing "undefined index" or not sending email in email form [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 7 years ago.
I am trying to put a basic email form on a website and have been having trouble with "unidentified index". I read around and found that "isset()" had solved this issue. I found that
$... = isset($_POST['...']);
does get rid of the error message, but my code does nothing. The page doesn't even refresh or throw another error. Here is my html:
<form method="post" action="index.php">
<h1>Send Us an Email</h1>
<header class="emailbody">
<label>Name</label>
<input name="name" placeholder="Type Here">
</header>
<section class="emailbody">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
</section>
<footer class="emailbody">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea><br>
</footer>
<div class="submitbutton">
<input id="submit" type="submit" value="Send">
</div>
</form>
And here is my php:
<?php
$name = isset($_POST['name']);
$email = isset($_POST['email']);
$message = isset($_POST['message']);
$from = 'From: SiteDemo';
$to = 'exemail#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
I have tried it with and without the "isset()" around the different indexes. Without, it throws an error and with, the code doesn't do anything.
Two things need to be corrected:
1) You have not written mail() function statement.
2) isset() returns only TRUE or FALSE depending upon whether variable is set or not. It does not return variable if variable is set.
Correct it to:
$name = isset($_POST['name']) ? $_POST['name'] : '';
Same for other variables.
Yes, you can use isset() here for checking either value set or not like that:
if(isset($_POST['submit']))
{
// if you dont want to send empty email.
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']))
{
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$header = "From: SiteDemo";
$to = 'exemail#gmail.com';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail($to,$subject,$body,$header);
}
}
And add name attribute in submit button as:
<input id="submit" type="submit" value="Send" name="submit">

Notice: Undefined Index - 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 7 years ago.
Not sure what is going on here. I am wanting to make a simple e-mail. When the PHP script runs I get three errors:
Notice: Undefined index: subject
Notice: Undefined index: email
Notice: Undefined index: emailcontent
HTML:
<form action="email.php" method="post" class="email-form">
<input type="text" name="subject" placeholder="E-mail Subject"></input>
<input type="text" name="email" placeholder="Your E-mail Address"></input>
<textarea type="text" name="emailcontent"></textarea>
<br>
<button type="submit" name="submit" value="send">Send E-mail</button>
</form>
PHP:
<?php
$emailSubject = $_POST["subject"];
$emailAddress = $_POST["email"];
$emailContent = $_POST["emailcontent"];
$headers = "From: " + $emailAddress;
$myEmail = "myemail";
mail($myEmail, $emailSubject, $emailContent, $headers);
?>
I think you should first check if fields are set or not. When page loads no value is and a notice is generated
<?php
if(isset($_POST['submit']))
{
$emailSubject = $_POST["subject"];
$emailAddress = $_POST["email"];
$emailContent = $_POST["emailcontent"];
$headers = "From: " + $emailAddress;
$myEmail = "myemail";
mail($myEmail, $emailSubject, $emailContent, $headers);
}
?>

Categories