Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: control in mailform

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

    Default control in mailform

    Hey all. I have troubles with a script, where the control is not waterproof. All fields need to be filled in, before the message should be send. But 'your name', 'your email' and 'your message' may not be filled in (as it is already onclick in the fields).

    This is the php:
    Code:
    <?php
    if ($_POST["action"] == "send"){
    if ($_POST[name] != " your name" and $_POST[name] != "" and $_POST[email] != " your e-mail adress" and $_POST[email] != "" and $_POST[message] != "") { 
    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 'Thanks ! <br>&nbsp;<br>Your message has been sent,<br> and you will receive a confirmation mail. <br><br> Ceci will get back to you as soon as posible.<br>&nbsp;<br><br>';
    }
    else{
    echo 'Please fill in all data !<br><br>Your name, email adress and message <br>are mandatory fields.<br><br><a href="contact.html"><font color="#565656;">[again please]</font></a><br>&nbsp;<br>';
    }
    }
    ?>
    and the html:
    Code:
    <table style="width:350px;" valign="top">
    <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="contact.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>

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

    Default

    If I understand your problem correctly you want to check if someone has filled in the fields correctly and i not then give them an error message?

    This is easiest done by not seperating your PHP and HTML as such:

    (Please forgive the typos busy doing this from work and only snatch a few minutes here and there but I think you get the idea)

    PHP Code:
    <?php


    if ($_POST["action"] == "send"){

    $error false;

    if (
    $_POST[name] != " your name" and $_POST[name] != "" and $_POST[email] != " your e-mail adress" and $_POST[email] != "" and $_POST[message] != "") { 
    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 
    'Thanks ! <br>&nbsp;<br>Your message has been sent,<br> and you will receive a confirmation mail. <br><br> Ceci will get back to you as soon as posible.<br>&nbsp;<br><br>';
    }
    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="contact.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>

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

    chechu (07-21-2008)

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

    Default

    Your line is better, indeed, instead of going to another page when there is an error. But the real problem is that I can send a mail now, without having to fill in all fields. So the control is wrong.

    To see in action: http://www.cecicasariego.com/contacttest.php
    Try filling in only name and message, and it gets send.

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

    Default

    I have altered the if statement as such:

    PHP Code:
    if ($_POST[name] != " your name" and $_POST[name] != "" and $_POST[email] != " your e-mail adress" and $_POST[email] != "" and strlen($_POST['message']) > 0) { 
    Let me know if this helps the problem...

  6. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    if ($_POST[name] != " your name" and $_POST[name] != "" and $_POST[email] != " your e-mail adress" and $_POST[email] != "" and $_POST[message] != "") {
    If you look at the above line in your code it misses the quotes around the key value for the $_POST. I think it should be

    Code:
      if ($_POST['name'] != " your name" and $_POST['name'] != "" and $_POST['email'] != " your e-mail adress" and $_POST['email'] != "" and $_POST['message'] != "") {

  7. The Following User Says Thank You to codeexploiter For This Useful Post:

    GarethMc (07-21-2008)

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

    Default

    -slaps head-

    Yes that too hehe .. but adding the strlen takes it from checking what the value is whch can fail to checking that whatever the value is is a string bigger than 0 which means any value entered will return true...

    So those two changes, quotes around your $_POST array elements and the strlen should help resolve it

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

    Default

    It still gets send only filling in the name and message ...

  10. #8
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You seems to be checking whether the email is a non-empty value. The problem here is if a user enter just space characters then this validation will fail this problem is applicable to other fields also. It would be better if you use trim function around the name, email and message fields during the checkings.

    You can find a good email validation script here http://www.addedbytes.com/php/email-address-validation try to use that.

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

    Default

    Sorry, but I do not understand what you mean or what the solution could be.

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

    Default

    Ok lets change them all to use the strlen function....

    PHP Code:
    if ($_POST['name'] != " your name" && strlen(trim($_POST['name'])) > 0
    && $_POST['email'] != " your e-mail adress" && strlen(trim($_POST['email']))> 0
    && strlen(trim($_POST['message'])) > 0) { 
    I have also added the trim() function here so that a person can't just enter a space and it goes through, it has to be an actual alphanumeric value that is input....

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
  •