SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Don't Understand This

    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?

    Code:
    <?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>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k is often used to stand for key
    v for value

    you could also have k stand for html_form_field_name
    PHP Code:
     Name: 
      <input name="name" type="text" id="name"> <?php echo $errors['name']; ?>
      <br>

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks crmalibu!
    Is there a better way this can be coded, 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!

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by F.Danials View Post
    also how do I display the error message stored in $errors['fieldname'] variables next to the appropriate fields?
    Look at crmalibu's answer again.


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
  •