Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: control in mailform

  1. #11
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    I can still send without filling in all fields.

  2. #12
    Join Date
    Jul 2008
    Location
    Johannesburg, South Africa
    Posts
    31
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default

    Problem now is a simple typo ... you do need to check that all the values you check for are correctly spelt.... If you look at the HMTL that defines the email text box:

    HTML Code:
    <input type="text" name="email" value=" your email adress" 
    and then the check in the if in PHP

    PHP Code:
    && $_POST['email'] != " your e-mail adress" && strlen(trim($_POST['email']))> 
    Notice the spelling mistake now? Its up to you to choose which one of the two to correct to fix the error....

  3. #13
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Code:
    <?php 
    
    
    if ($_POST["action"] == "send"){ 
    
    $error = false; 
    
    if ($_POST['name'] != " your name" && strlen(trim($_POST['name'])) > 0 
    && $_POST['email'] != " your email adress" && strlen(trim($_POST['email']))> 0 
    && strlen(trim($_POST['message'])) > 0) {  
    mail ("info@site.com", "via website",  
    " 
    Name: ".$_POST['name']." 
    E-mail: ".$_POST['email']." 
    Message: ".$_POST['message']." 
    ", 
    "From: ".$_POST['name']." <".$_POST['email'].">"); 
    $subject = "your message";  
    $msg = " 
    This is an automatically sent email. Please do not reply. 
    Dear $_POST[name], 
    Thanks for your message to Ceci. 
    She will get back to you as soon as possible. 
    This was your message: 
    $_POST[message]  
    ";   
    mail($_POST[email], $subject, $msg);  
    echo ''; 
    } 
    else{ 
    $error = true; 
    } 
    } 
    
    ?> 
    
    <html> 
    <head> 
    </head> 
    <body> <table style="width:350px;" valign="top"> 
    <?php 
    if (isset($error)) 
    { 
    if ($error) 
    { 
    ?> 
    <tr> 
    <td>Please complete all details. Name, email and message are mandatory</td> 
    </tr> 
    <?php 
    } 
    else 
    { 
    ?> 
    <tr> 
    <td>Thank ou for submitting your details. A confirmation email has been sent to the address specified</td> 
    </tr> 
    <?php 
    } 
    } 
    ?> 
    <tr><td align="left" valign="top" style="width:10px;padding-left:10px;"> 
    <script type="text/JavaScript"> 
    function clearDefault(el) { 
    if (el.defaultValue==el.value) el.value = "" 
    } 
    </script> 
    <form name="form1" method="post" action="contacttest.php"> 
    <input type="hidden" name="action" value="send"> 
    <textarea name="message" style="border-top:0px; border-left:1px dotted #0066cc; border-right:0px; border-bottom: 1px dotted #0066cc; width:120px; height:80px" onfocus="clearDefault(this)"> your message</textarea> 
    </td><td align="left" style="width:40px;">&nbsp; 
    <td align="left" valign="top"> 
    <input type="text" name="name" value=" your name" onfocus="clearDefault(this)" style="border-top:0px; border-left: 1px dotted #cc6600; border-right:0px; border-bottom: 1px dotted #cc6600; width:120px"> 
    <br>&nbsp;<br> 
    <input type="text" name="email" value=" your email adress" onfocus="clearDefault(this)" style="border-top:0px; border-left: 1px dotted purple; border-right:0px; border-bottom: 1px dotted purple; width:120px;"> 
    <br>&nbsp;<br>&nbsp;<br> 
    <img src="images/vink1.gif" border="0" style="padding-left:80px;">&nbsp; <input type="submit" value="send" style="border-top:0px; border-left:0px; border-right:0px; border-bottom:0px; background-color: #ffffff;">   
    </form> 
    </td></tr></table>
    Somzetimes it works, sometimes it doesn't. Weird.
    Also, the website construction has changed. Should I replace the whole html code where I made it red ?

  4. #14
    Join Date
    Jul 2008
    Location
    Johannesburg, South Africa
    Posts
    31
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default

    Yes you should and you should also add the closing tags at the end (i.e. </body> and </html>)

    I was just a bit lazy using proper XHTML tags such as the doctype decleration and such.. just add the rest of your HTML in where it goes and all should be fine...

    As for working sometimes .. remember that to the if statement "your email adress" and " your email adress" (see the space after the opening quote) are not the same...

  5. The Following User Says Thank You to GarethMc For This Useful Post:

    chechu (07-21-2008)

  6. #15
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    As for working sometimes .. remember that to the if statement "your email adress" and " your email adress" (see the space after the opening quote) are not the same...
    Where do you see the difference ? Can you color it, please ?

    Also, is it possible, instead of having a text when a field is not filled in, that the border of the input field turns red ?
    Last edited by chechu; 07-21-2008 at 01:55 PM.

  7. #16
    Join Date
    Jul 2008
    Location
    Johannesburg, South Africa
    Posts
    31
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default

    Quote Originally Posted by chechu View Post
    Where do you see the difference ? Can you color it, please ?
    I was just pointing out that string based comparisons are not incredibly reliable. Someone could just take the space out from in front of " your email adress" in the text box and click submit and it will work, because the if is checking for that text with a space...

    Adding a check to see if the submitted text is in a valid email address format would be handy and isn't necessarily hard to do either using regex or just the PHP string functions...

    Try things out for yourself. Play with the code. Half the battle of learning to program is fixing bugs.. it helps remind you in future what could be causing your problems so you don't repeat them.

  8. The Following User Says Thank You to GarethMc For This Useful Post:

    chechu (07-21-2008)

  9. #17
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    I was just pointing out that string based comparisons are not incredibly reliable. Someone could just take the space out from in front of " your email adress" in the text box and click submit and it will work, because the if is checking for that text with a space...
    So if I remove the space, it will always work !
    Adding a check to see if the submitted text is in a valid email address format would be handy and isn't necessarily hard to do either using regex or just the PHP string functions... Try things out for yourself. Play with the code.
    Totally agree, but I know nothing of php, and trust me, have tried to, but I keep mixing/messing things up.

    Is it possible, instead of having a text when a field is not filled in, that the border of the input field turns red ?
    I have this:
    Code:
    <style type="text/css">
    <!--
    div#container {
        margin:0 auto;
        background: #f2f4f7;
    }
    label {
        float: left;
        width: 140px;
        text-align: left;
        padding-top: 5px;
    }
    input, textarea {
        padding: 3px;
        margin: 3px;
        border: 1px solid #bac5d6;
        font: 10px Verdana, sans-serif;
        background: #fff;
    }
    input.fout, textarea.fout {
        border: 1px solid #FF0000;
    }
    label.fout {
        color: #FF0000;
    }
    -->
    </style>
    Last edited by chechu; 07-21-2008 at 02:10 PM.

  10. #18
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    and this:
    [CODE]<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    ob_start();
    session_start();
    $cfg['url'] = "ikke.com";
    $cfg['naam'] = "Ceci Casariego";
    $cfg['email'] = "info@site.com";
    $cfg['spam'] = 0;
    $cfg['text'] = TRUE;
    $cfg['input'] = TRUE;
    $cfg['HTML'] = TRUE;
    $cfg['CAPTCHA'] = TRUE;
    function checkmail($email)
    {
    if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email))
    {
    return TRUE;
    }
    return FALSE;
    }

    $formulier = TRUE;
    if(!isset($_COOKIE['formulier']))
    {
    if(isset($_POST['wis']) && ($_SERVER['REQUEST_METHOD'] == "POST"))
    {
    foreach($_POST as $key => $value)
    {
    unset($value);
    }
    header("Location: ".$_SERVER['PHP_SELF']."");
    }
    if(isset($_POST['verzenden']) && ($_SERVER['REQUEST_METHOD'] == "POST"))
    {
    $aFout = array();
    $naam = trim($_POST['naam']);
    $email = trim($_POST['email']);
    $onderwerp = trim($_POST['onderwerp']);
    $bericht = trim($_POST['bericht']);
    if($cfg['CAPTCHA'])
    {
    $code = $_POST['code'];
    }
    if(empty($naam) || (strlen($naam) < 3) || eregi("[<>]", $naam) )
    {
    $aFout[] = "Er is geen naam ingevuld.";
    unset($naam);
    $fout['text']['naam'] = TRUE;
    $fout['input']['naam'] = TRUE;
    }
    if(empty($email))
    {
    $aFout[] = "Er is geen e-mail adres ingevuld.";
    unset($email);
    $fout['text']['email'] = TRUE;
    $fout['input']['email'] = TRUE;
    }
    elseif(checkmail($email) == 0)
    {
    $aFout[] = "Er is geen correct e-mail adres ingevuld.";
    unset($email);
    $fout['text']['email'] = TRUE;
    $fout['input']['email'] = TRUE;
    }
    if(empty($onderwerp))
    {
    $aFout[] = "Er is geen onderwerp ingevuld.";
    unset($onderwerp);
    $fout['text']['onderwerp'] = TRUE;
    $fout['input']['onderwerp'] = TRUE;
    }
    if(empty($bericht))
    {
    $aFout[] = "Er is geen bericht ingevuld.";
    unset($bericht);
    $fout['text']['bericht'] = TRUE;
    $fout['input']['bericht'] = TRUE;
    }
    if($cfg['CAPTCHA'])
    {
    if(strtoupper($code) != $_SESSION['captcha_code'])
    {
    $aFout[] = "Er is geen correcte code ingevuld.";
    $fout['text']['code'] = TRUE;
    $fout['input']['code'] = TRUE;
    }
    }
    if(!$cfg['text'])
    {
    unset($fout['text']);
    }
    if(!$cfg['input'])
    {
    unset($fout['input']);
    }
    if(!empty( $aFout ))
    {
    $errors = '
    <div id="errors">
    <ul>';
    foreach($aFout as $sFout)
    {
    $errors .= " <li>".$sFout."</li>\n";
    }
    $errors .= "</ul>
    </div>";
    }
    else
    {
    $formulier = FALSE;
    if($cfg['HTML'])
    {
    $headers = "From: \"Contact Formulier\" <".$cfg['email'].">\r\n";
    $headers .= "Reply-To: \"".$naam."\" <".$email.">\n";
    $headers .= "Return-Path: Mail-Error <".$cfg['email'].">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Transfer-Encoding: 8bit\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $bericht = '
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    </head>
    <body>
    <br />
    <b>Naam:</b> '.$naam.'<br />
    <b>Email:</b> <a href=\"mailto:'.$email.'\">'.$email.'</a><br />
    <br />
    <b>Bericht:</b><br />
    '.$bericht.'
    <br />
    <br />
    <br />
    --------------------------------------------------------------------------<br />
    <b>Datum:</b> '.date("d-m-Y @ H:i:s").'<br />
    <b>IP:</b> <a href=\"http://sunny.nic.com/cgi-bin/whois?domain='.$_SERVER['REMOTE_ADDR'].'\">'.$_SERVER['REMOTE_ADDR'].'</a><br />
    <b>Host:</b> '.gethostbyaddr($_SERVER['REMOTE_ADDR']).'<br />
    </body>
    </html>';
    }
    else
    {
    $bericht_wrap = wordwrap ($bericht, 40, "\n", 1);
    $headers = "From: \"Contact Formulier\" <".$cfg['email'].">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/plain; charset='iso-8859-1'\n";
    $message = "Naam: ".$naam." \n";
    $message .= "E-mail: ".$email." \n";
    $message .= "Bericht:\n".$bericht_wrap." \n ";
    $message .= " \n ";
    $message .= "Datum: ".date("d-m-Y H:i:s")." \n";
    $message .= "------------------------------------------------------- \n ";
    $message .= "IP: ".$_SERVER['REMOTE_ADDR']." \n ";
    $message .= "Host: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])." \n ";

    }
    if(mail($cfg['email'], "[Contact] ".$onderwerp, $bericht, $headers))
    {
    if(isset($_POST['stuurkopie']))
    {
    $headers = "From: \"Contact Formulier\" <".$email.">\r\n";
    $headers .= "Reply-To: \"".$naam."\" <".$email.">\n";
    $headers .= "Return-Path: Mail-Error <".$email.">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Transfer-Encoding: 8bit\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    mail($email, "[Contact] ".$onderwerp, $bericht, $headers);
    }
    unset($naam, $email, $onderwerp, $bericht);
    setcookie("formulier", 1, time() + ( $cfg['spam'] * 60 ) );
    echo "
    <p>
    Uw bericht is succesvol verzonden, er word zo snel mogelijk gereageerd.<br />
    <br />
    Met vriendelijke groeten,<br />
    <b>".$cfg['naam']."</b>
    </p>
    ";
    }
    else
    {
    echo "Er is een fout opgetreden bij het verzenden van de email";
    }
    header("refresh:3;url=".$cfg['url']."");
    }
    }
    if($formulier)
    {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <title>Contact Formulier door Thijs</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="nl" />
    </head>
    <body>
    <div id="container">
    <?php
    if(isset($errors)) {
    echo $errors;
    }
    ?>
    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <p>
    <label <?php if(isset($fout['text']['naam'])) { echo 'class="fout"'; } ?>>Naam:</label>
    <input type="text" id="naam" name="naam" maxlength="30" <?php if(isset($fout['input']['naam'])) { echo 'class="fout"'; } ?> value="<?php if (!empty($naam)) { echo stripslashes($naam); } ?>" /><br />
    <label <?php if(isset($fout['text']['email'])) { echo 'class="fout"'; } ?>>Email:</label>
    <input type="text" id="email" name="email" maxlength="255" <?php if(isset($fout['input']['email'])) { echo 'class="fout"'; } ?> value="<?php if (!empty($email)) { echo stripslashes($email); } ?>" /><br />
    <label <?php if(isset($fout['text']['onderwerp'])) { echo 'class="fout"'; } ?>>Onderwerp:</label>
    <input type="text" id="onderwerp" name="onderwerp" maxlength="40" <?php if(isset($fout['input']['onderwerp'])) { echo 'class="fout'; } ?> value="<?php if (!empty($onderwerp)) { echo stripslashes($onderwerp); } ?>" /><br />
    <label <?php if(isset($fout['text']['bericht'])) { echo 'class="fout"'; } ?>>Bericht:</label>
    <textarea id="bericht" name="bericht" <?php if(isset($fout['input']['bericht'])) { echo 'class="fout"'; } ?> cols="30" rows="6"><?php if (!empty($bericht)) { echo stripslashes($bericht); } ?></textarea><br />
    <?php
    if($cfg['CAPTCHA'])
    {
    ?>
    <label></label>
    <img src="captcha.php" alt="" /><br />
    <label <?php if(isset($fout['text']['code'])) { echo 'class="fout"'; } ?>>Code:</label>
    <input type="text" id="code" name="code" maxlength="4" size="4" <?php if(isset($fout['input']['code'])) { echo 'class="captcha fout"'; } ?> /><br />
    <?php
    }
    ?>
    <label for="stuurkopie">Stuur mij een kopie</label><input type="checkbox" id="stuurkopie" name="stuurkopie" value="1" /><br />
    <label></label>
    <input type="submit" id="verzenden" name="verzenden" value="verzenden" />
    <input type="submit" id="wis" name="wis" value="Wis velden" />
    </p>
    </form>
    </div>
    </div>
    </div>
    </body>
    </html>

  11. #19
    Join Date
    Jul 2008
    Location
    Johannesburg, South Africa
    Posts
    31
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default

    Yes you can just alter the input tag based on the value of $error:

    PHP Code:
    <?php
    if ($error)
    {
    ?>
    <input class="fout" type="text".........
    <?php
    }
    else
    {
    ?>
    <input type="text" ........
    <?php
    }
    ?>

  12. #20
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

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
  •