Popo
October 16, 2015, 2:40pm
1
HTML:
[code]
<input type="text" name="email" id="email" size="30" placeholder="Enter your e-mail address." required/>
<textarea name="message" id="message" placeholder="Type your message here" required></textarea>
<input type="text" name="captcha" placeholder="House pet that says 'meow'" required/>
<input id="submit" type="submit" name="submit" value="Send" />
[/code]
php:
<?php
$to = "username@gmail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$headers = "From: $from";
$subject = "You have a message sent from your site";
$body = "$name from $from says: $message";
mail($to, $subject, $body, $headers);
?>
I get an email in my box, but it says “from says:” which means the variables aren’t making it to the email. Any ideas?
Your form has no action. Where are the form variables supposed to go?
Popo
October 16, 2015, 2:55pm
3
Adding action=“scripts/contactform.php” to my form didn’t prove any results
@Popo - I’ve removed your actual e-mail address from your post. Presumably you don’t want it on the Internet for all to see.
1 Like
I have this
<!doctype html>
<html>
<head>
<title>asdf</title>
</head>
<body>
<form id="contact" name="contact" method="post" action="testing-form-action.php">
<input type="text" name="name" id="name" size="30" placeholder="Enter your name." required/>
<input type="text" name="email" id="email" size="30" placeholder="Enter your e-mail address." required/>
<textarea name="message" id="message" placeholder="Type your message here" required></textarea>
<input type="text" name="captcha" placeholder="House pet that says 'meow'" required/>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
</body>
</html>
And then this
<?php
$to = "email@email.com";
$from = $_POST['email'];
$name = $_POST['name'];
$headers = "From: ".$from;
$subject = "You have a message sent from your site";
$body = $name." from ".$from." says: ".$message;
echo $to." to<br>";
echo $from." from<br>";
echo $name." name<br>";
echo $headers." headers<br>";
echo $subject." subject<br>";
echo $body." body<br>";
?>
http://www.codefundamentals.com/test/testing-form.php
Seems to echo out fine.
Popo
October 16, 2015, 3:06pm
7
Hm, still didn’t work. still no variables passing
<?php
$to = "easdfadf0@gmail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$subject = "You have a message sent from your site";
$body = $name." from ".$from." says: ".$message;
mail($to, $subject, $body);
?>
[code]
<input type="text" name="email" id="email" placeholder="Enter your e-mail address." required/>
<textarea name="message" id="message" placeholder="Type your message here" required></textarea>
<input type="text" name="captcha" placeholder="House pet that says 'meow'" required/>
<input id="submit" type="submit" name="submit" value="Send" />
[/code]
Popo
October 16, 2015, 3:11pm
8
It’s gotta be HTML problem or something? Could this be server issue? The variables have to be working, im getting the email so that works, the title of the email is there, the varaibles from the HTML aren’t getting passed along for whatever reason
I don’t spot an error as to why variables are not displayed… Just wanted to note that the FROM address SHOULD NOT be something a user puts in the form. It should be a hosted email address for the domain, e.g. info@mydomin.com
You can use a reply to header with users email.
Popo
October 16, 2015, 4:09pm
10
I only use the $from so I know who to contact, which is why its a part of $body. I get the email in my inbox from my server, unless I’m misunderstanding you?
Drummin
October 16, 2015, 4:30pm
11
I was referring to
$headers = "From: $from";
See https://en.wikipedia.org/wiki/Email_spoofing
Most hosts will not allow sending of email from an address other than the domain.
I know until I whitelisted my shared-host domain my Hotmail email account sent them to the Trash (i.e. SPAM) folder.
But Popo is getting emails, only without “message”
Maybe some kind of obscure naming conflict? i.e. would changing “message” to “form_message” work?
Or maybe newlines from the textarea are being considered as being email delimiters? *
* this is my “best guess” as to what the problem is
Drummin
October 16, 2015, 7:28pm
13
Are you sure you have method=“post” on your form tag?
It’s as if POST is not available.
If you add the following to the top of scripts/contactform.php does it show post array?
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;
ralphm
October 16, 2015, 11:41pm
14
What if you change this line:
$body = $name." from ".$from." says: ".$message;
to this?
$body = "$name from $from says: $message";
Of course, the next step is to add some security!
Drummin
October 17, 2015, 12:36am
15
Your suggestion is how he originally had it in OP, plus it’s better not to quote variables.
Seems to me that variables are set as empty strings and POST is not available for some reason.
Are you submitting the form with javascript in some way that is not sending post?
ralphm
October 17, 2015, 4:28am
16
Duh, silly me.
Ah, OK, hadn’t heard that. Any particular reason?
Drummin
October 17, 2015, 5:01pm
17
ralphm:
it’s better not to quote variables.
Ah, OK, hadn’t heard that. Any particular reason?
I’m sure it was here in the forum a few years back that I was told (by a BIG DOG) that I shouldn’t use double quotes on strings and variables should not be quoted. Since that time I have changed my writing style so where in most cases I use single quotes and concatenation around variables
header("location: ".$page);
or
$content = 'This is a ' . $variable . ' that is not in quotes.';
1 Like
felgall
October 17, 2015, 9:23pm
18
That saves PHP having to parse the text looking for variables and is therefore more efficient.
1 Like
Popo
October 17, 2015, 10:44pm
19
Lol, yes, I have it there.I believe the HTML was above unless I am remembering bad
<form id="contact" action="scripts/contactform.php" name="contact" method="post">
edit: I just realized I was replying to the wrong person, but yes, I have post in my method form attribute
Popo
October 17, 2015, 10:54pm
20
If you go to reesecodes.com/scripts/contactform.php you can see the result, it shows Array
(
)
Popo
October 17, 2015, 10:56pm
21
here is my javascript. I wasn’t having any issues with it so I didnt even think to look there.
jQuery.validator.addMethod('answercheck', function (value, element) {
return this.optional(element) || /^\bcat\b$/.test(value);
}, "type the correct answer -_-");
jQuery.validator.addMethod('answercheck', function (value, element) {
return this.optional(element) || /^\bcat\b$/.test(value);
}, "type the correct answer -_-");
// validate contact form
$(function() {
$('#contact').validate({
rules: {
name: {
required: true
},
email: {
required: true,
email: true
},
message: {
required: true
},
answer: {
required: true,
answercheck: true
}
},
messages: {
name: {
required: "Please enter your name."
},
email: {
required: "Please enter your e-mail."
},
message: {
required: "Please write a message"
},
answer: {
required: "Sorry, wrong answer!"
}
},
submitHandler: function(form) {
$(form).ajaxSubmit({
type:"POST",
data: $(form).serialize(),
url:"scripts/contactform.php",
success: function() {
$('#contact :input').attr('disabled', 'disabled');
$('#contact').fadeTo( "fast", 0.15, function() {
$(this).find(':input').attr('disabled', 'disabled');
$(this).find('label').css('cursor','default');
$('#error').fadeIn();
});
},
error: function() {
$('#contact').fadeTo( "fast", 0.15, function() {
$('#success').fadeIn();
});
}
});
}
});
});