Don't Understand This

Discussion in 'PHP' started by FishSword, Feb 10, 2009.

  1. #1
    Hiya,

    I made this script years ago, and now don't understand what is happening.
    I wondered if someone would be so kind as to recode it using proper variable names etc instead of just using names like $k and $p (I know these names are bad, and I no longer do this. But like I said it was a long time ago since I coded this ;)).

    Also, how do you output the appropriate error messages next to the correct field within the form?

    Is there a better way of doing this?

    <?php
    $post = array("name" => "", "email" => "", "phone" => "");
    $errors = $post;
    
    if (isset($_POST["submit"])) {
        foreach ($post as $k => $v)
            $post[$k] = stripslashes($_POST[$k]);
    
        if ($post["name"] == "")
            $errors["name"] = "error";
        if (!preg_match('/[^@]+@[^.]+(\.[^.]+)+/', $post["email"]))
            $errors["email"] = "error";
        if ($post["phone"] == "")
            $errors["phone"] = "error";
    
        $error = false;
        foreach($errors as $k => $v) {
            if ($errors[$k] != "")
                $error = true;
        }
    
        if (!$error) {
            $message = "";
            foreach($post as $k => $v)
                if ($v != "")
                    $message .= "$k: $v\n";
            $sent = mail("info@company.com", "Subject", $message, "From: info@company.com");
        }
    }
    ?>
    
    <form name="form" method="post" action="">
      SECTION #1<br>
    <br>
      Name: 
      <input name="name" type="text" id="name">
      <br>
    <br>
      E-Mail:
      <input name="email" type="text" id="email">
      <br>
    <br>
      Phone:
      <input name="phone" type="text" id="phone">
      <br>
      <br>
      <input name="send" type="submit" id="send" value="Send">
      <input name="reset" type="reset" id="reset" value="Reset">
    </form>
    Code (markup):
     
    FishSword, Feb 10, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    why don;t you switch to javascript verification, it will be so much faster
     
    javaongsan, Feb 10, 2009 IP
  3. boatsource

    boatsource Active Member

    Messages:
    663
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #3
    All you need to do is search and replace, $k to $key and $v to $value.

    The $key ($k) would be name, email, phone
    The $value ($v) would be whatever was submitted in each.

    As for the javascript suggestion, its great for front line processing but you still need the validation in PHP or you have no security.
     
    boatsource, Feb 10, 2009 IP
  4. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    Thanks Boatsource! - I agree. JavaScript alone is a bad idea due to JavaScript being able to be disabled!
    Is there a better way this can be coded in PHP, also how do I display the error message stored in $errors['fieldname'] variables next to the appropriate fields?

    Also, what is actually happening in the script? I can't remember. Without being too cheeky, would it be possible for you to comment it?

    I just wish I could remember! ;)
     
    FishSword, Feb 11, 2009 IP