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 have a form in which once filled in it sends an email to the user and should give a price based on what option they selected within the dropdown.
I have an if statement to see what option has been selected within the drop down. Within the if statement it should set the $Price variable to $Price = '£300';
The $Price variable is then added into the $body variable to be sent. The value of the $Price variable doesn't show within the email but when I take the $Price = '£300'; out of the if statement it is visible within the email.
I have made sure that the problem isn't the options dropdown selection as it works when the variable is placed within the echo 'Sent' , $Price;
PHP:
if ($Property_Value == '1to2') {
$Price = '£300';
}
$name = $_POST['name'];
$email = $_POST['email'];
$from = 'From: BungeeDesign.com';
$to = $email;
$subject = 'Email Inquiry';
$Property_Value = $_POST['Property_Value'];
$body = "From: $name\n E-Mail: $email\n Price:\n $Price\n Selection:\n $Property_Value";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Sent';
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
HTML:
<div class="quoteForm">
<form action="formAct.php" method="post">
<li class="liFeilds">Name*:</li>
<input type="text" name="name" placeholder="e.g. John Appleseed" required="yes" />
<li class="liFeilds">Email*:</li>
<input type="email" name="email" placeholder="e.g. email#email.com" required="yes" />
<li class="liFeilds">Phone:</li>
<input type="number" name="phone" placeholder="e.g. 07789236519" />
<li class="liFeilds">Postcode*:</li>
<input type="text" name="pc" placeholder="e.g. BN2 0AQ" required="yes" />
<li class="liFeilds">Building Name/Number*:</li>
<input type="text" name="addr" placeholder="e.g. 24 Lyndhurst Road / Palm Court" required="yes" />
<li class="liFeilds">Property Value*:</li>
<select class="propVal" name="Property_Value" required>
<option value="1to2">£100,000 - £200,000</option>
<option value="2to3">£200,001 - £300,000</option>
<option value="3to4">£300,001 - £400,000</option>
<option value="4to5">£400,001 - £500,000</option>
<option value="5to6">£500,001 - £600,000</option>
<option value="6to7">£600,001 - £700,000</option>
<option value="7to8">£700,001 - £800,000</option>
<option value="8up">Over £800,000</option>
</select>
<button name="submit" type="submit" value="Submit">Get Estimate</button>
</form>
</div>
You are checking $Property_Value before setting any value to the variable
You have to first set the value, then check for any condition on that variable like:
$Property_Value = $_POST['Property_Value'];
if ($Property_Value == '1to2') {
$Price = '£300';
}
Related
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">
This question already has answers here:
Using $_POST to get select option value from HTML
(8 answers)
Closed 3 years ago.
I've followed a PHP contact form tutorial from https://1stwebdesigner.com/php-contact-form-html/, everything prints out fine in the email that's sent to the recipient, except the selected value in the dropdown box.
So how exactly could I get one of the 4 values to be printed in the email?
Here is a snippet from the HTML file:
<form action="emailform.php" method="POST">
<p>Full Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Subject</p>
<select name="list">
<option value="booking">Booking Query</option>
<option value="payment">Request Payment Details</option>
<option value="error">Report Website Error</option>
<option value="other">Other</option>
</select>
<br />
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send">
</form>
And the PHP file:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$list = $POST['list'];
$message = $_POST['message'];
$formcontent="From: $name \n Subject: $list \n Message: $message";
$recipient = "email#example.com";
$subject = "Aeron Valley Classic Hire";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#B22222;'> Return Home</a>";
?>
I'm not very good at this, but although I've followed every step of the tutorial I do wonder how the PHP code actually knows what the values are and how to display them?
Any help would be appreciated.
u typoed ur POST request for the list
$list = $POST['list'];
should be
$list = $_POST['list'];
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.
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">
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 have just started coding in PHP and I am having some issues to make this code work. I have tried to change from $_POST to $HTTP_POST_VARS , but I still cant get the value, anybody could point me what am I doing wrong and how to fix this code?
<?php
$email_address = "email#email.com";
// your e-mail address goes here
$email_subject = "Online Enquiry";
// the subject line for the e-mail goes here
$from_email_name = "email#email.com";
// the from address goes here
$redirect_to_page = "thankyou.html";
// enter the web address where the user should be sent after completing the form
//*********************************
// DO NOT EDIT BELOW THIS LINE!!!**
//*********************************
$mailTo = "$email_address";
$mailSubject = "$email_subject";
$mailBody = "The form values entered by the user are as follows: \n\n";
foreach($_POST as $key=>$value)
{
$mailBody .= "$key = $value\n";
}
$mailBody .= "\n\n";
$fromHeader = "From: $from_email_name\n";
if(mail($mailTo, $mailSubject, $mailBody, $fromHeader))
{
print ("<B><br></b>");
}
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$redirect_to_page\">";
?>
My HTML form.
<form action="contact.php" method="post">
<input type="text" class="textbox" value=" Your Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Name';}">
<input type="text" class="textbox" value="Your E-Mail" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your E-Mail';}">
<div class="clear"> </div>
<div>
<textarea value="Message:" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Message ';}">Your Message</textarea>
</div>
<div class="submit">
<input type="submit" value="SEND " />
</div>
</form>
See var_dump($_REQUEST). Default form submit uses $_GET object.