Notice: Undefined index $_POST [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)
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I am new to php and I made a contact form but i get a
Notice: Undefined index: name in
C:\xampp\htdocs\portfolio\portfolio\html\contact.php
for the lines $_POST - Name, email, message and human.
How can I get take the error away??
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From:';
$to = '86376#ict-idcollege.nl';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['submit'])) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*Wat is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>

By using isset or !empty:
<?php
$name = (isset($_POST['name']) ? $_POST['name'] : '');
$email = (isset($_POST['email']) ? $_POST['email'] : '');
$message = (isset($_POST['message']) ? $_POST['message'] : '');
?>

Just Make Changes this in your code :
if (isset ($_POST['name'])) {
$name = $_POST['name'];
}
if (isset ($_POST['email'])) {
$email = $_POST['email'];
}
if (isset ($_POST['message'])) {
$message = $_POST['message'];
}
$from = 'From:';
$to = '86376#ict-idcollege.nl';
$subject = 'Hello';
if (isset ($_POST['human'])) {
$human = $_POST['human'];
}

place the code you have above inside a test block as follows
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
...
until the end of ?>
this is because when ever the page loads without the form submission. (because you have the control logic and display logic in the same file) it executes the code on the top.

Related

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 Script Dies due to Isset(). Other options?

I have a contact form I'm trying to run, but for whatever reason the script will die every time I attempt to submit it. Not sure why. Is there anything blantanly wrong with the code below? I wasn't using isset() before, but someone here suggested I do and now it breaks the page.
FORM
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="email" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="number" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = 'thatbraxjohnsonguy#gmail.com';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if (isset($_POST['submit'] && $captcha == 4)) {
if (mail ($to, $subject, $body)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click here to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
} else {
echo '<p>Something went wrong, try again later!</p>';
}
?>
ERROR_LOG
[09-Feb-2017 12:15:46 America/New_York] PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /home/yourerlv/public_html/hanksmith.com/php/contact.php on line 17
You're doing it wrong. You need to move the parenthesis over so it's only evaluating the one variable:
if (isset($_POST['submit']) && $captcha == 4) {
^
Change code:
if (isset($_POST['submit'] && $captcha == 4)) { // Original
if (isset($_POST['submit']) && $captcha == 4 ) { // Changed

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">

undefined index $_POST php contact form [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
Can anyone help me, please. I have this code and I don't know why I've got these errors.
"Undefined index name, email and message"
I've tried to add an action in form :
<form action="?" method="post"> but doesn't work.
<?php
$to = 'myemail#something.else';
$subject = 'From Site';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$header = '$email';
if ($_POST) {
mail($to, $subject, $message, $header);
}
?>
<html>
<fieldset>
<legend>Contactat US</legend>
<form method="post">
<label for="name">Name:</label><br>
<input type="text" name="name" id="name" /><br>
<label for="email">E-mail:</label><br>
<input type="text" name="email" id="email" /><br>
<label for="message">Message:</label><br>
<textarea id="message" name="message" id="message" cols="42" rows="9"></textarea><br><br>
<input type="submit" value="Send" />
</form>
</fieldset>
</html>
if(isset($_POST['email'])){
$to = 'myemail#something.else';
$subject = 'From Site';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$header = '$email';
mail($to, $subject, $message, $header);
}
REPLACE TO THIS:
if(isset($_POST['email'])){
$to = 'myemail#something.else';
$subject = 'From Site';
$name = filter_input(INPUT_POST, 'name');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');
$header = '$email';
mail($to, $subject, $message, $header);
}
if you consult the post array directly, you may have difficulties.
And you see a warning in some editors. (as example Netbeans IDE)

Categories