PHP Form to Email

Discussion in 'Software' started by Scribe17, Apr 13, 2006.

  1. Scribe17

    Scribe17 Private E-2

    I'm making a form, but it's not a normal contact form - it's a feedback form for critting stories. There are four normal type questions, ie name, email, story title and date, and there are 13 writing specific questions. The HTML side of the form is fine. I've done that. But, the php side of it is driving me nuts because I don't know what I'm doing.

    I'm trying to set them up as two separate files ie feedback.html and process.php.

    I've tried going through tutorials, but they don't give enough information where necessary and too much information where it's not necessary. I can get a simple form with name, address and comments to work, but as soon as I add more fields the whole thing caves in on me.

    I want a form with these fields to be emailed to me:

    name
    email
    title
    date
    (from here it's the actual feedback so can the two sections be separated in some way?)
    hook
    protagonist
    antagonist
    technique
    language
    dialogue
    setting
    conflict
    mechanics
    other
    suggestions

    I want them all, except suggestions, to be required fields. I have "thank you" and "error" pages set up too, but can't get them to work either.

    This is one of three forms I'm trying to create (the questions in the second part varies for each form). If someone who knows php can start me off and tell me where I have to add the other fields in order to make it all work, I'd be so grateful.

    Can anyone help me?
     
  2. Raits

    Raits Private E-2

    Well, what errors do you get when you try to add your new fields?
     
  3. Scribe17

    Scribe17 Private E-2

    I wasn't getting anything other than a white screen.

    Since my first post, I found a generator which allowed me to customise 10 fields and I found it easy to add the others I needed. This form is working well, however, I need to find out how to make certain fields "required".

    This is the coding with only the basics (so that it's not too long) and with critical information changed for privacy reasons:

    PHP:
    <?php
    $mymail 
    'myemail@yahoo.com';
    $cc 'Critique Form';
    $BoDy ' ';
    $FrOm 'FROM:' .$_POST['t1'];
    $FrOm .= 'Reply-To:' .$_POST['t1'];
    $FrOm .= 'X-MAILER: PHP'.phpversion();
    $BoDy .= 'Name: ';
    $BoDy .= $_POST['name'];
    $BoDy .= "\n";
    $BoDy .= 'Email: ';
    $BoDy .= $_POST['email'];
    $BoDy .= "\n";
    $BoDy .= "_______________________________________________\n";
    $BoDy .= "\n";
    $BoDy .= 'Story Title: ';
    $BoDy .= $_POST['title'];
    $BoDy .= "\n";
    $BoDy .= 'Date of Critique: ';
    $BoDy .= $_POST['date'];
    $BoDy .= "\n";
    $BoDy .= "_______________________________________________\n";
    $BoDy .= "\n";
    $BoDy .= 'COMMENTS: ';
    $BoDy .= "\n";
    $send mail("$mymail""$cc""$BoDy""$FrOm");
    ///Redirect user to the website....
    if($send)
    {
    echo 
    '<html><head>';
    echo 
    '<meta http-equiv="refresh" content="0;URL=http://www.mywebspace.com">';
    echo 
    '</head><body>Email send....';
    echo 
    '</body></html>';
    }
    ?>
    Questions:
    1. How do I make selected fields "required" ie name, email and comments?
    2. How do I add the "stripslashes" coding?
    3. How can I change the coding so that the sender's name or email address comes up in my inbox instead of "Reply-To"?

    Thank you in advance for your help. :)
     
  4. Raits

    Raits Private E-2

    The check could be done like this:
    PHP:
    <?php 
    /*
    Checks if the name variable is longer than
    1 and the email variable longer than 5 characters
    */
    if(strlen($_POST['name']) >= && strlen($_POST['email']) >= 5
    {
    $mymail 'myemail@yahoo.com'
    $cc 'Critique Form'
    $BoDy ' '
    $FrOm 'FROM:' .$_POST['t1']; 
    $FrOm .= 'Reply-To:' .$_POST['t1']; 
    $FrOm .= 'X-MAILER: PHP'.phpversion(); 
    $BoDy .= 'Name: '
    $BoDy .= $_POST['name']; 
    $BoDy .= "\n"
    $BoDy .= 'Email: '
    $BoDy .= $_POST['email']; 
    $BoDy .= "\n"
    $BoDy .= "_______________________________________________\n"
    $BoDy .= "\n"
    $BoDy .= 'Story Title: '
    $BoDy .= $_POST['title']; 
    $BoDy .= "\n"
    $BoDy .= 'Date of Critique: '
    $BoDy .= $_POST['date']; 
    $BoDy .= "\n"
    $BoDy .= "_______________________________________________\n"
    $BoDy .= "\n"
    $BoDy .= 'COMMENTS: '
    $BoDy .= "\n"
    $send mail("$mymail""$cc""$BoDy""$FrOm"); 
    ///Redirect user to the website.... 
    if($send

    echo 
    '<html><head>'
    echo 
    '<meta http-equiv="refresh" content="0;URL=http://www.mywebspace.com">'
    echo 
    '</head><body>Email send....'
    echo 
    '</body></html>'

    }
    else
    {
    echo 
    '<html><head>'
    echo 
    '<meta http-equiv="refresh" content="0;URL=http://www.mywebspace.com/error.html">'
    echo 
    '</head><body>Error sending email: name or email field empty or not valid'
    echo 
    '</body></html>'
    }
    ?> 
    Didn't see a variable for comments so didn't add that, also havn't tested if this works. About stripslashes, you can just add:
    $name = stripslashes($_POST['name']);
    but be sure to change the $_POST['name'] variable for the $name variable in the script that comes after striping. As for the third question i don't quite understand what you mean.
     
  5. Hate_Phones

    Hate_Phones Private E-2

    Try this out...

    save the following as contact.php

    now save this as sendmail.php
    Just remember to add your e-mail address... you should be all set. If you want some help feel free to e-mail me at dmellon@douglasmellon.com.
    Hope that this helps,
    Doug Mellon
     
  6. covert215

    covert215 Private E-2

    PHP:
    <?php

    //stores info from fields to the .php file
    $name Trim(stripslashes($_POST['name']));  
    $email Trim(stripslashes($_POST['email']));  
    $title Trim(stripslashes($_POST['title']));  
    $date Trim(stripslashes($_POST['date']));  
    $hook Trim(stripslashes($_POST['hook']));  
    $protagonist Trim(stripslashes($_POST['protagonist']));  
    $antagonist Trim(stripslashes($_POST['antagonist']));  
    $technique Trim(stripslashes($_POST['technique']));  
    $language Trim(stripslashes($_POST['language']));  
    $dialogue Trim(stripslashes($_POST['dialogue']));  
    $setting Trim(stripslashes($_POST['setting']));  
    $conflict Trim(stripslashes($_POST['conflict']));  
    $mechanics Trim(stripslashes($_POST['mechanics']));  
    $other Trim(stripslashes($_POST['other']));  
    $suggestions Trim(stripslashes($_POST['suggestions']));  

    //other necessary variables
    $EmailTo "THE EMAIL YOU WANT THIS SENT TO";
    $Subject "THE SUBJECT THAT YOU WANT IT TO HAVE WHEN YOU RECEIVE IT";

    // validation
    $validationOK=true;
    if (
    Trim($name)==""$validationOK=false;
    if (
    Trim($email)==""$validationOK=false;
    if (
    Trim($title)==""$validationOK=false;
    if (
    Trim($date)==""$validationOK=false;
    if (
    Trim($hook)==""$validationOK=false;
    if (
    Trim($protagonist)==""$validationOK=false;
    if (
    Trim($antagonist)==""$validationOK=false;
    if (
    Trim($technique)==""$validationOK=false;
    if (
    Trim($language)==""$validationOK=false;
    if (
    Trim($dialogue)==""$validationOK=false;
    if (
    Trim($setting)==""$validationOK=false;
    if (
    Trim($conflict)==""$validationOK=false;
    if (
    Trim($mechanics)==""$validationOK=false;
    if (
    Trim($other)==""$validationOK=false;


    //if one of the validation is false, it will redirect the your error page
    if (!$validationOK) {
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR ERROR PAGE.html\">";
      exit;
    }


    //CREATES AN EMAIL THAT WILL BE SENT TO YOU


    $ip $_SERVER['REMOTE_ADDR']; //this posts the ip address of the user as well

    $Body "";
    $Body .= "IP: ";
    $Body .= $ip;
    $Body .= "\n";
    $Body .= "Name: ";
    $Body .= $name;
    $Body .= "\n";
    $Body .= "E-Mail: ";
    $Body .= $email;
    $Body .= "\n";
    $Body .= "Title: ";
    $Body .= $title;
    $Body .= "\n";
    $Body .= "Date: ";
    $Body .= $date;
    $Body .= "\n";
    $Body .= "Hook: ";
    $Body .= $hook;
    $Body .= "\n";
    $Body .= "Protagonist: ";
    $Body .= $protagonist;
    $Body .= "\n";
    $Body .= "Antagonist: ";
    $Body .= $antagonist;
    $Body .= "\n";
    $Body .= "Technique: ";
    $Body .= $technique;
    $Body .= "\n";
    $Body .= "Language: ";
    $Body .= $language;
    $Body .= "\n";
    $Body .= "Dialogue: ";
    $Body .= $dialogue;
    $Body .= "\n";
    $Body .= "Setting: ";
    $Body .= $setting;
    $Body .= "\n";
    $Body .= "Conflict: ";
    $Body .= $conflict;
    $Body .= "\n";
    $Body .= "Mechanics: ";
    $Body .= $mechanics;
    $Body .= "\n";
    $Body .= "Other: ";
    $Body .= $other;
    $Body .= "\n";
    $Body .= "Suggestions: ";
    $Body .= $suggestions;
    $Body .= "\n";



    // sends email with all the information
    $success mail($EmailTo$Subject$Body"From: <$email>");

    // redirect to thank you page 
    if ($success){*/
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=THANK YOU PAGE.html\">";
    //}

    ?>
    just pick the email you want it sent to, the subject line for your messages, the error page, and the thank you page

    wherever your form is, put the following html in it:
    Code:
    <form method="POST" action="WHATEVER YOU NAME IT.php">
    <input type="hidden" name="success" value="THANK YOU PAGE.html">
     
  7. Scribe17

    Scribe17 Private E-2

    Thank you so much, Covert215. This is exactly what I was after. I appreciate your time and effort.

    All the other suggestions didn't work. I think I went in circles and confused myself.

    Thank you again.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds