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.
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 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 :)
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';
}
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 6 years ago.
I want to make a contact form which I have done in HTML like this.
none of this matters i found a better way to handle this using $_SERVER["RESQUEST_METHOD"]
I'll leave it up in case anyone is as new as me and making stupid mistakes.
<!DOCTYPE html>
<html lang="en">
<body>
<h4>Contact me!</h4>
<form method="post" action="test_form.php" autocomplete="on">
Subject:
<br>
<input type="text" name="subject" required>
<br>name:
<br>
<input type="text" name="name" required>
<br>Email:
<br>
<input type="email" name="email" required>
<br>Message:
<br>
<textarea name="message" rows="5" maxlength="500"></textarea>
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
then after I add in the PHP to a separate form called test_form.php this is where I am stuck I have gone over three different layouts and formats what not and still returning the fatal error. this is my .php file I apologise for the layout. (just to add I have tried it with my variables inside the isset and I have tried them after.
<?php
$submit = $_post['submit'];
$to = "no#way.co.uk";
$header = "From:$name Email:$email /n subject:$subject";
$formcontent = "Name:$name /n Message:$message";
if (isset('submit')){
if (empty($_post['subject'])){
$subjectErr = "Subject is required!"
}else{
$subject = valid ($_post['subject']);
}
if(empty($_post['name'])){
$nameErr = "Name is required!";
}else{
$name = valid ($_post ['name']);
}
if(empty($_post['email'])){
emailErr = "Email is required!";
}else{
$email = valid ($_post['email']);
}
if(empty($_post['message'])){
$message = "";
}else{
$message = valid ($_post['message']);
}
//function to validate data
function valid ($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
mail ($to, $subject, $formcontent, $header) or die ('error');
echo "thank you";
}
?>
My question is where am I going wrong? I haven't even finished it yet I was just testing it to see if it was working.
According to a manual:
isset — Determine if a variable is set and is not NULL
See: variable. Do you have any variable in this:
isset('submit')
No, here's only a string "submit".
Your proper code is:
if (isset($submit))
Or simplified:
if (isset($_POST['submit']))
And by the way - it's $_POST, not $_post.
And another by the way - defining
$header = "From:$name Email:$email /n subject:$subject";
before $name, $email and $subject are defined - results in empty strings instead of real values.
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.