1. This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
    For information on cookies and how you can disable them visit our Cookie Usage page.
    Dismiss Notice

PHP contact form

Discussion in 'Web Development' started by arrigo, May 30, 2014.

  1. arrigo

    arrigo New Member

    Hi,
    in my first effort to create a web site I modified the template "law firm" for our NGO. I'm stuck with one problem to resolve: I have yet to find out how to modify the source code of the contact form in order to direct our future "clients'" messages to our email address. Excuse my ignorance...

    Greetings

    Arrigo
     
  2. Mimoun

    Mimoun Administrator Staff Member Director Verified Member

  3. arrigo

    arrigo New Member

    With Wix I couldn't save my work (I already have domain+hosting) nor copy and paste source-code (+ how-to-vid didn't work). I was hoping to use the contact form of the "law firm" template, maybe by inserting the necessary php-coding (I found some free contact forms in the net)?
     
  4. Mimoun

    Mimoun Administrator Staff Member Director Verified Member

    @arrigo if you know a little of PHP it's easy to get it to work. Please share with us the contact form you found and if you got it to work or not. We can help you out if you have problems getting it to work.
     
  5. Recidivist

    Recidivist VIP Member

    Essentially you need to create a HTML form and give the inputs unique names. Then have a button which is linked to a PHP script.

    The PHP script should capture the input content using
    Code:
    $_POST['attribute_name']
    Assign the return value to a variable, e.g.
    Code:
    $attribute = $_POST['attribute_name']
    The method of sending the mail depends on your setup. I like PEAR.

    PHP5 has a mail function, IIRC there's some -/+'s to it.
    http://www.w3schools.com/php/php_mail.asp

    Some tuts:
    http://www.freecontactform.com/email_form.php
    http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/

    Also to consider:
    http://www.w3schools.com/php/php_secure_mail.asp
     
    Mimoun likes this.
  6. Gonzoide

    Gonzoide New Member

    Here is a complete solution.

    • It checks if all parameters are filled
    • It checks that email has a legitimate pattern
    • It allows you to branch to several departments
    • It informs if message was not correctly sent
    Layout is extremely basic, but page is fully fonctional once you have addressed the two little red comments

    <h1>Contact</h1>

    <?php

    if (isset ($_POST)) {
    if (isset ($_POST['dest']) && isset ($_POST['email']) && isset ($_POST['subject']) && isset ($_POST['body']) && !empty ($_POST['dest']) && !empty ($_POST['email']) && !empty ($_POST['subject']) && !empty ($_POST['body'])) {
    $emails = array (
    1 => "sales@mysite.com",
    2 => "support@mysite.com"
    );

    if (($_POST['dest'] > 0) && ($_POST['dest'] < 3)) {
    $email = $emails[$_POST['dest']];
    $dest = strtolower($_POST['email']);

    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $dest)) {

    $subject = $_POST['subject'];
    $body = $_POST['body']."\n(Message sent by ".$dest.")";

    // Here I use a custom "sendMail" function, feel free to use yours
    $result = sendMail($email, $subject, $body);

    if ($result) {
    echo "<p>Message sent</p>";
    } else {
    echo "<p>Problem while sending</p>";
    }
    } else {
    echo "<p class='alert'>Invalid email address</p>";
    }

    }
    } else
    if (count($_POST) > 0) {
    echo "<p>Incomplete formular</p>";
    }
    }

    // here the action should contain the actual page name

    <form action="contact.php" method='post'>
    <p>Vous wish to contact (*) :<br/>
    <?php

    echo "<select name='dest'>\n";

    echo "<option value='1' >Sales department</option>\n";
    echo "<option value='2'>Support department</option>\n";
    ?>
    </select>
    </p>
    <p>Your email (*) :<br/>
    <input type='text' name='email' size='45'/>
    </p>
    <p>
    Subject (*) :<br/>
    <input type='text' name='subject' size='45'/>
    </p>
    <p>
    Message (*):<br/>
    <textarea name="body" cols="80" rows="8"></textarea>
    </p>
    <p>
    (*) All fields mandatory !
    </p>
    <p>
    <input type="submit" value="Send"/>
    </p>
    </form>
     
  7. Miguel

    Miguel Migz, Web Developer

    @arrigo can you please post the link of your contact page so we can check what is wrong with it?
     
    Dawn and Ruthe like this.
  8. arrigo

    arrigo New Member

    Hi there, thank you for your consideration. I got the contact form to work, except for the captcha - the script doesn't fit. The link to my contact form: http://sosrassismus.ch/contact_engl.php.

    Regards

    Arrigo
     
  9. Miguel

    Miguel Migz, Web Developer

    Hi, Glad to hear that the form works. :cool: I am checking your link. I didn't see your script.

    Where do you plan to put your script? Can you elaborate more about the script doesn't fit?

    Cheers,

    Miguel
     
  10. arrigo

    arrigo New Member

    Hi Miguel,
    thanks for the prompt answer. Guess the problem with my php script is that the captcha-script doesn't fit with the one of the form. Here's the script:
    <?php

    $zieladresse = 'info@sosrassismus.ch';

    $absenderadresse = 'ihrAndererName@EXAMPLE.com';

    $absendername = 'SOS Kontakt';

    $betreff = 'Kontakt';

    $urlDankeSeite = 'http://www.sosrassismus.ch';

    $trenner = ":\t"; // Doppelpunkt + Tabulator

    if ($_SERVER['REQUEST_METHOD'] === "POST") {
    $header = array();
    $header[] = "From: ".mb_encode_mimeheader($absendername, "utf-8", "Q")." <".$absenderadresse.">";
    $header[] = "MIME-Version: 1.0";
    $header[] = "Content-type: text/plain; charset=utf-8";
    $header[] = "Content-transfer-encoding: 8bit";

    $mailtext = "";
    foreach ($_POST as $name => $wert) {
    if (is_array($wert)) {
    foreach ($wert as $einzelwert) {
    $mailtext .= $name.$trenner.$einzelwert."\n";
    }
    } else {
    $mailtext .= $name.$trenner.$wert."\n";
    }
    }
    mail(
    $zieladresse,
    mb_encode_mimeheader($betreff, "utf-8", "Q"),
    $mailtext,
    implode("\n", $header)
    ) or die("Die Mail konnte nicht versendet werden.");
    header("Location: $urlDankeSeite");
    exit;
    }
    header("Content-type: text/html; charset=utf-8");
    ?><label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here"><?php
    if ($_POST['submit'] && $human == '4') {
    if (mail ($to, $subject, $body, $from)) { //I think the trouble is here: no correspondence to "$body"//
    echo '<p>Your message has been sent!</p>';
    } else {
    echo '<p>Something went wrong, go back and try again!</p>';
    }
    } else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    ?>

    ... if you have a solution that makes this captcha work you could also use my contact form to answer :)

    cheers,

    Arrigo
     
  11. Miguel

    Miguel Migz, Web Developer

    I see, so its a php script. Sorry i was thinking of a jquery script.
    Oh i think i figure out your problem. You didn't put your captcha inside the form.

    In your html :

    HTML:
    <section class="body">
    <label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">  
            <form action="" method="post">....</form>
    Try this :
    HTML:
    <section class="body">
        <form action="" method="post">
            <label>*What is 2+2? (Anti-spam)</label>
            <input type="text" name="human" placeholder="Type Here">
    ...
        </form>
    ...
    The $human on your php script will not be recognize as variable because it is not inside the form tag.

    Hope it works!

    -Miguel
     
    Last edited: Aug 8, 2014
  12. Kanav Arora

    Kanav Arora New Member

    Hope the following code will help you.

    <?php
    $action=$_REQUEST['action'];
    if ($action=="") /* display the contact form */ { ?> <form action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php }
    else /* send the submitted data */ { $name=$_REQUEST['name']; $email=$_REQUEST['email']; $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
    {
    echo "All fields are required, please fill <a href=\"\">the form</a> again.";
    }
    else{ $from="From: $name<$email>\r\nReturn-path: $email"; $subject="Message sent using your contact form"; mail("youremail@yoursite.com", $subject, $message, $from);
    echo "Email sent!";
    }
    } ?>
     
  13. CMB Solutions

    CMB Solutions New Member

    Hi Guys

    I download the astronomy web template but have no clue how to get the contact.html page's contact form working. I removed the "subscribe for newsletter" link but need to get the contact form working as I particularly like the look of this contact form.

    The (html) code looks like this in the contact.html:
    Code:
    <form action="index.html" method="post">
                        <ul>
                            <li>
                                <label>Enter your full name here.</label>
                                <input type="text" value="Full Name" onBlur="javascript:if(this.value==''){this.value=this.defaultValue;}" onFocus="javascript:if(this.value==this.defaultValue){this.value='';}">
                            </li>
                            <li>
                                <label>Enter your email address here.</label>
                                <input type="text" value="Email Address" onBlur="javascript:if(this.value==''){this.value=this.defaultValue;}" onFocus="javascript:if(this.value==this.defaultValue){this.value='';}">
                            </li>
                            <li>
                                <label>Enter the Subject message here.</label>
                                <input type="text" value="Subject" onBlur="javascript:if(this.value==''){this.value=this.defaultValue;}" onFocus="javascript:if(this.value==this.defaultValue){this.value='';}">
                            </li>
                            <li>
                                <label class="msg">Enter your Message here.</label>
                                <textarea onBlur="javascript:if(this.value==''){this.value=this.defaultValue;}" onFocus="javascript:if(this.value==this.defaultValue){this.value='';}">Message</textarea>
                                <div class="checkbox">
                                    <label for="terms">
                                        <input type="checkbox" id="terms">
                                        I agree to the Terms and Conditions</label>
                                    <br>
                                    </div>
                                <input type="submit" value="Send Now" class="btn3">
                            </li>
                        </ul>
                    </form>
     
  14. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    try reading this entire post, you just might find out how it is done.
     
  15. Php Contact Form Program -

    <?php
    $action=$_REQUEST['action'];
    if ($action=="") /* display the contact form */
    {
    ?>
    <form action="" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="name" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php
    }
    else /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
    {
    echo "All fields are required, please fill <a href=\"\">the form</a> again.";
    }
    else{
    $from="From: $name<$email>\r\nReturn-path: $email";
    $subject="Message sent using your contact form";
    mail("abc@gmail.com", $subject, $message, $from);
    echo "Email sent!";
    }
    }
    ?>

    - i strongly suggest sites improve your php knowledge w3schools.com .