Results 1 to 7 of 7

Thread: PHP form emailer

  1. #1
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default PHP form emailer

    Hello,
    Sometimes I just don't get it. When I run it through a PHP debuger No syntax Errors found. No General Errors
    But it doesn't work. Here is the script:
    PHP Code:
    <?php
    //PHP Form Mail - By fileserverdirect
    $message="Hello, Your Website just Had another submisson at (".time()."), Here are the details:<br>\n";
    foreach(
    $_POST as $var -> $val)
    {
    if(
    $var!="emailowner")
    {
    $var ucfirst($var);
    $message.="-----<br>\n".$var." : ".$val."<br>\n";
    }
    if(
    $var=="emailowner")
    {
    $email $val;
    }
    else if(
    $var=="name")
    {
    $subj "Message from " $name;
    }
    }
    /*$message = $_POST['mess'];
    $email = $_POST['email'];
    $name = $_POST['name'];*/
    $sent="false";
    if(
    $_POST['submit'])
    {
    if(
    mail($email$subj$message"From: \"Your Website\""))
    {
    echo 
    "Message sent!";
    $sent "true";
    }
    else
    {
    echo 
    "Error sending message.";
    }
    }
    else
    {
    echo
    "Please fill out the form on this page.";
    }
    echo 
    '<form action="mailit.php" method="post">
    <input type="hidden" name="emailowner" value="me@myemail.com">
    <input type="hidden" name="submit" value="true">
    <textarea name="mess" rows=10 cols=30>
    </textarea><br>
    Email:<input type="text" name="email">
    Name:<input type="text" name="name">
    <input type="submit" value="Submit">
    </form><br>'
    ;
    ?>
    <!-- some other website stuff here-->
    <?
    if($sent=="true")
    {
    echo 
    " The Message \"" $message "\" was sent to \"" $email "\"";
    }
    ?>
    Can someone show me what is wrong with this script?
    Last edited by fileserverdirect; 11-28-2007 at 02:46 AM. Reason: updated script
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Seems ok. Hmm... are you using a godaddy windows server?

    Best way to debug is have it output the message instead, and see if that's right. Then make a simpler page, to just check a simple mail function (no dynamic content, just strings), and see if that works.

    phpinfo() might also tell you a bit more about your configuration.

    Nothing jumped out at me as wrong, though I can't promise that's the case.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Thats what I mean, No syntax errors, I think that it is getting stuck on the foreach-loop, but I don't know why. I think that it is sending the foreach loop into an endless loop, so if there mere only two things being submitted, t $message might look like this:
    Code:
    test1:hello
    ---
    test2:hi
    ---
    test1:hello
    ---
    test2:hi
    ---
    test1:hello
    and so on.
    Im not shure, This was just a guess.
    I will do some more checks on it later, but I will see what you can come up with if you run the script.
    ---
    No I do not use godaddy on a windows server.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You have something like:
    if ($a!=1) {
    ....
    }
    if ($a==1) {
    ...
    }

    You could just use an else for the second one.
    Also, here, $subj = "Message from " . $name; has $name which was not defined.

    Rethink some of that loop and you may find the problem as well.

    [edit]in order to debug somethign like that, especially with loops, th easiest way is just to add checks along the way. see if the variables are what you expect.
    echo $var;
    print_r($array);
    die('it got to line ##');

    Use those logically, line by line, and you'll track it down within a couple minutes.
    Last edited by djr33; 11-28-2007 at 07:20 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I fainally got an error message out of my program:
    Fatal error: Cannot access empty property in C:\my\folder\here\mailit.php on line 6
    echo $message; does not print anywere but before the foreach-loop. Line 6 is the acutal foreach loop. But what does "Cannot access empty property" mean. This is the UPDTAED code:
    PHP Code:
    foreach($_POST as $var -> $val)
    {
    if(
    $var!="emailowner")
    {
    $var ucfirst($var);
    $message.="-----<br>\n".$var." : ".$val."<br>\n";
    }
    elseif(
    $var=="emailowner")
    {
    $email $val;
    }
    else
    {
    $subj "Message from " $val;
    }

    I don't get it
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this:

    Code:
     foreach($_POST as $var => $val)
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Thanks, IT WORKED! -- for a whlie,
    I don't know when because I added a couple of features, and now for whatever reason, the "emailowner", "submit", and "websitename" are being included in the email. But the email is never sent because emailowner (a hidden value) is never set. Why is the foreach loop totally bypassing this line?!?!?
    if($var!="emailowner"||$var!="submit"||$var!="websitename")
    ?!?!
    I just don't get it.
    Edit:
    I figuerd it out:
    if it was emailowner it was not submit so It went through with it. So now the line reads:
    if($var!="emailowner"&&$var!="submit"&&$var!="websitename")
    Last edited by fileserverdirect; 11-29-2007 at 02:24 AM. Reason: Figuerd it out
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

Bookmarks

Posting Permissions

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