Jump to content


Photo

Problems with mail


  • Please log in to reply
26 replies to this topic

#21 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 06 June 2006 - 08:08 PM

Ok I think I get the point a little, so I can still use the same thing I am doing, I could take my current setup and just attribute them into error, I like this part

if (isset($_POST['send'])){
    if (empty($_POST['name'])){
    print('The name field has been left blank<br />');
    }if (empty($_POST['emailaddress'])){
    print('The Email Address field was left blank<br />');
    }if (empty($_POST['verifyemail'])){
    print('The Verify Email Field was left blank<br />');
    }if ($_POST['emailaddress'] != $_POST['verifyemail']){
    print('The Email Addresses do not match, please fix this<br />');
    }if (empty($_POST['description'])){
    print('The Description Field was left blank<br />');
I like the way that looks, the way it operates and the way it functions, doing it one at a time, I know the code is longer, but it's funner, it helps me learn it faster, and understand this, before I start using arrays to do it, which I will later after I can do this blindly with no help. Right now what I want to know is with the error handling, what is it, what's the purpose, are you speaking of trapping all of the errors into an array, and printing them ALL out at the end, or is there another purpose, thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#22 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 June 2006 - 08:31 PM

If you are validating user input it is best to trap all errors into an array, then at the end of the validaton checks you can check whether there are any errors in the array if there are use a simple foreach loop and then display the errors in an unordered list, or in what ever format you wish to show them.

So heres what your code will basically look like:
if (isset($_POST['send']))
{
    $error = ""; //setup blank error variable, this will store errors

    if (empty($_POST['name'])) {
        $error[] = 'The name field has been left blank';
    }

    if (empty($_POST['emailaddress'])) {
        $error[] = 'The Email Address field was left blank';
    }
    elseif (empty($_POST['verifyemail'])) {
        $error[] = 'The Verify Email Field was left blank';
    }
    elseif ($_POST['emailaddress'] != $_POST['verifyemail']) {
        $error[] = 'The Email Addresses do not match, please fix this';
    }

    if (empty($_POST['description'])) {
        $error[] = 'The Description Field was left blank';
    }

    //check that the error varibale is an array, if it loop through the error and displau them as an unordered list
    if(is_array($error)) {
        echo "Please correct the following errors before you continue:<br />\n<ul>";

        foreach($error as $k => $v) {
            echo '<li>' . $v . "</li>\n";
        }
        echo '</ul>';
    }
    else
    {
        //put email code here
    }
}
//form code here
Thats the best way of doing it rather than printing each error one at a time as that can pertential annoy the user. It is best to show all the errors all at once, therefore the user can work through each of the errors reported one by one.

#23 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 06 June 2006 - 08:40 PM

ok I guess I will go ahead and formulate away based on one of the ideas here, as far as today I quit on this project, I am going to my other project, tomorrow I will start trying to work it like I was shown on here, thanks for all the help I appreciate it.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#24 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 06 June 2006 - 09:03 PM

ok there is one more thing I need to ask before I even think about trying this, I thought about it, I might like doing it this way more, but what does.

foreach($error as $k => $v) {

I want to avoid something that uses single letters, I understand the functionality on this all but the section that I showed, any explanation would be greatly appreciated.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#25 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 June 2006 - 09:14 PM

I hope you don't mind one more example...

I took your code and modified it to fit into that way I do validations. You can ignore the CSS stuff -- I was trying to make your form easier on the eyes.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Contact Us</title>
<style>
form {
    display: block;
    width: 80%;
    margin-left: auto;
    margin-right: auto;
}

form label {
    display: block;
    width: 18%;
    font-weight: bold;
    float: left;
    text-align: left;
    margin-bottom: 0.1em;
}

form input, form textarea {
    display: block;
    width: 78%;
    float: left;
    margin-bottom: 0.1em;
}

.btn {
    display: block;
    width: 100%;
    text-align: center;
    padding-top: 0.1em;
    padding-bottom: 0.1em;
    border: 1px solid red;
}

.button {
    width: auto;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
}

.clearer {
    clear: both;
    line-height: 0.01em;
}
</style>
</head>
<body>
<h3>Contact Us</h3>
<p><a href="index.php" title="Return Home">Return Home</a></p>
<p>If you need to contact us for any reason you can do so below.  We will try and respond within 24 hours.</p>
<br />
<?php
$flds = array('name' => 'Name',
                  'emailaddress' => 'Email Address',
                  'verifyemail' => 'Verify Email',
                  'description' => 'Description');
if (isset($_POST['send'])){
    $errs = array();
    foreach($_POST as $k => $v)
        switch($k) {
            case 'name':
            case 'emailaddress':
            case 'verifyemail':
            case 'description':
                if (strlen(trim(stripslashes($v))) == 0)
                    $errs[] = 'The <span style="color:red;font-weight:bold">' . $flds[$k] . '</span> field was left blank';
                if ($k == 'emailaddress' && trim(stripslashes($v)) != trim(stripslashes($_POST['verifyemail'])))
                    $errs[] = 'The <span style="color:red;font-weight:bold">Email Addresses</span> do not match';
                break;
        }
    if (empty($errs)) {
        $to = "businessman332211@hotmail.com";
        $subject = "FunnyEmailForwards.com contact";
        $name = stripslashes($_POST['name']);
        $emailaddress = $_POST['emailaddress'];
        $verifyemail = $_POST['verifyemail'];
        $description = stripslashes($_POST['description']);
        $from = "FunnyEmailForwards.com Contact";
        $message = "
        Name: {$name}
        Email Address: {$emailaddress}
        Verify Email: {$verifyemail}
        What the person had to say:
        {$description}
        ";
       if (mail($to, $subject, $message, $From))
               echo 'Thank you for contacting us!<br />We will try and get back to you within 24 hours.<br />';
       else{
                echo 'There was an error sending the message, please try again!<br />';
                echo 'If it does not work the second time, please send an email directly to<br />';
                echo '<a href="mailto:businessman332211@hotmail.com">Here</a>';
                }
    }
}
if (!isset($_POST['send']) || !empty($errs)) {
    if (!empty($errs))
        echo '<hr />The following problems were found in your submitted data:<br />' . implode("<br />\n",$errs) . "<br /><hr />\n";
?>
<form name="contact" id="contact" method="post">
<label for="name">Name:</label>
<input name="name" id="name" type="text" maxlength="80" value="<?=isset($_POST['name']) ? $_POST['name'] : ''?>"/><br />
<label for="emailaddress">Email Address:</label>
<input name="emailaddress" id="emailaddress" type="text" maxlength="80" value="<?=isset($_POST['emailaddress']) ? $_POST['emailaddress'] : ''?>"/><br />
<label for="verifyemail">Verify Email Address:</label>
<input name="verifyemail" id="verifyemail" type="text" maxlength="80" value="<?=isset($_POST['verifyemail']) ? $_POST['verifyemail'] : ''?>"/><br />
<label for="description">Message:</label>
<textarea name="description" id="description" cols="20" rows="7" />
<?php
if (isset($_POST['description'])){
echo $_POST['description'];}; ?>
</textarea><br />
<div class="clearer">&amp;nbsp;</div>
<div class="btn">
<input class="button" name="send" id="send" type="submit" value="Contact Us!" />
<input class="button" name="reset" id="reset" type="reset" value="Clear Form" />
</div>
</form>
<?php } ?>
</body>
</html>

Ken

#26 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 07 June 2006 - 12:21 PM

thanks, but what about that k=>as something what does that mean, I understand the error message part, and I might start using that but the bottom, I don't understand what that for each statement is doing with the letters.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#27 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 07 June 2006 - 12:27 PM

The foreach it taking your array and splitting it into key/value pairs. It loops through the array and each time you go through the guts of the foreach, you'll have the key => value pair in the $k and $v variables. I normally use $key and $val instead of $k and $v.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users