SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast TheEngine's Avatar
    Join Date
    Sep 2003
    Location
    Sunnyvale Trailer Park
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php contact form question

    Hello,

    Trying to get my php form to send me the info from the completed fields. I'm using a form which I found here and you can see all the code.

    Form works fine, but problem I am having is when I try to add new fields, for example:
    Code:
    $customer_name = $_REQUEST['customer_name'] ;
    I would think I would need to add the $customer_name value in the following code along with the $comments that was already there, so I receive that info, but when I test the form it does not work - do not receive an email at all.

    Code:
    // If we passed all previous tests, send the email then redirect to the thank you page.
    else {
    mail( "$webmaster_email", "Feedback Form Results",
      $customer_name, $comments, "From: $email_address" );
    header( "Location: $thankyou_page" );
    }
    ?>
    Any idea what's going on?
    Thanks

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    *wildguess*

    change to:
    PHP Code:
    mail"$webmaster_email""Feedback Form Results",
      
    $customer_name' said: ' $comments"From: $email_address);
    header"Location: $thankyou_page);

    although it sounds as if something else is doing some cleansing (lets hope so )

    If we passed all previous tests

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Location
    UK
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at the PHP: Mail - Manual, it should be:

    PHP Code:
    bool mail string $to string $subject string $message [, string $additional_headers [, string $additional_parameters ]] ) 
    So in your code it's:
    PHP Code:
    mail"$webmaster_email"// To - you don't need the " in this line as it just slows down execution times.
    "Feedback Form Results"// Subject
     
    $customer_name,  // Message
    $comments// Additional Headers - I doubt $comments is header data
    "From: $email_address// Additional Parameters // this is also not an additional parameter.
    ); 
    Your probably better off with something like this:

    PHP Code:
    // $customer_name = $_REQUEST['customer_name'] ; // I'm not a fan of _REQUEST, I prefer $_POST it's more specific. 
    $customer_name $_POST['customer_name'];

    // Run tests.

    $to  $webmaster_email;
    $subject 'Feedback Form Result'// Also single quotes are faster then double quotes.
    $message $customer_name."\r\n".
    $comments."\r\n".
    "From: ".$email_address// \r\n creates a new line.

    $headers 'From: webmaster@example.com' "\r\n" // These extra headers reduce the odds it will end up in the spam bin.
        
    'Reply-To: webmaster@example.com' "\r\n" .
        
    'X-Mailer: PHP/' phpversion();

    mail($to$subject$message$headers);

    header'Location: '.$thankyou_page );
    die(); 
    // it's a good idea to kill the script once you send a location header as some browsers disregard them. 
    My Blog/Site: Full On Design

  4. #4
    SitePoint Enthusiast TheEngine's Avatar
    Join Date
    Sep 2003
    Location
    Sunnyvale Trailer Park
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cups & Rogem. I am receiving all the values now. However, I am now trying to get my checkbox results displayed. I am able to get one selection the user chose to display, but if multiple checkbox items are selected, only displays one item, not all.

    I've been researching and playing around with some code, but can't get to work with my php send mail.

    Thinking I need to do something like this ??

    {
    $N = count($aDoor);

    echo("You selected $N door(s): ");
    for($i=0; $i < $N; $i++)
    {
    echo($aDoor[$i] . " ");
    }
    }

    and my html page would look like this:

    <input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
    <input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
    etc...

    I'm not much of a programmer... Maybe there's an easier way. Doesn't have to be pretty, just need to see all checkbox items selected.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this:
    Code:
    <input type="checkbox" name="formDoor[0]" value="A" />Acorn Building<br />
    <input type="checkbox" name="formDoor[1]" value="B" />Brown Hall<br />
    Then alter it and try this
    Code:
    // NB do not quote array keys in your html, even non-numeric !!
    
    <input type="checkbox" name="formDoor[A]" />Acorn Building<br />
    <input type="checkbox" name="formDoor[B]" />Brown Hall<br />
    Just use
    PHP Code:
    var_dump($_POST); // or $_GET 
    on the postback to see what is being passed around between html and php

  6. #6
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I'm struggling with making an application form for an account on a site, information i want to receive is the applicants first name, last name, email, and a brief description. When I have had a go at writing the php code to email me the info, it sends an error message saying that 'we encountered an error'...???

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    When starting out, strip everything right down to the bare bones. Forget sending emails, add that later when you are confident that your form is being handled. Strip out styling too. Keep it really simple, and slowly build it up.

    Make a simple form, say using the POST method, when the submit button is hit, just display the output of
    PHP Code:
    var_dump($_POST); 
    and check that against your expectations. Then fiddle with the form again, and repeat.

  8. #8
    SitePoint Enthusiast TheEngine's Avatar
    Join Date
    Sep 2003
    Location
    Sunnyvale Trailer Park
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I seem to have screwed something up that I need to fix before I worry about the checkbox selections.

    1) In my php mail script, email address and comment is currently defined as mandatory fields as is noted in the script. However, for some reason it is making phone_number and comments as mandatory when I test the form.

    Code:
    // If the form fields are empty, redirect to the error page.
    elseif (empty($email_address) || empty($comments)) {
    header( "Location: $error_page" );
    }
    I've defined my fields as follows, and this is the order they appear on the form itself and are defined by the same name.

    Code:
    */
    $customer_name = $_REQUEST['customer_name'] ;
    $email_address = $_REQUEST['email_address'] ;
    $phone_number = $_REQUEST['phone_number'] ;
    $comments = $_REQUEST['comments'] ;
    
    /*
    2) Finally, for the following, when I receive the email it is not displaying the customer name as the From - is just blank.

    Code:
    // If we passed all previous tests, send the email then redirect to the thank you page.
    else {
    mail( "$webmaster_email", "Feedback Form Results",
      $customer_name. ' said: ' . $comments, "From: $customer_name" );
    header( "Location: $thankyou_page" );
    }
    Any assistance greatly appreciated...I'm going crazy


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
  •