PHP Form Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DreamOn
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2009 8:33 am

PHP Form Script

Post by DreamOn »

Hey Guys,

im new here and im kinda stuck on a simple problem.

i need an email form which where a user can select which person they want to email (from drop down box).

Below is the code which i have atm..

Code: Select all

<?php
class maxContact {
    var $subjectText = "Message From Website Contact Form!";
    var $targetEmail = '';
    var $mailFormat  = "text/plain";
    var $fieldList;
    var $errorList;
    var $email = '';
    var $name  = '';
    var $message = '';
    var $additionalText = '';
 
    function maxContact(){
        //Extend the list as you want here
        $this->fieldList[0]['caption'] = "Mesto";
        $this->fieldList[0]['fieldName'] = "mesto";
        $this->fieldList[0]['value'] = "";
    }   
    
    function showForm(){
      $message = '';  
      echo '   <p><form action="'.$_SERVER['PHP_SELF'].'" method="post">';
      echo '   <select name="$targetEmail">'
          .'   <option value="john@example.com" name="john">John</option>'
          .'   <option value="mark@example.com" name="mark">Mark</option>';
      echo '   </select><br />';
      echo '   Full Name:<br />'
          .'   <input type="text" name="name" value="'.$this->name.'" size="40"/><br />';
      echo '   Email:<br />'
          .'   <input type="text" name="email" value="'.$this->email.'" size="50"/><br />';           
      
      echo '    Message:<br />'
          .'    <textarea cols="65" rows="15" name="message">'.$this->message.'</textarea><br />'
          .'<input type="submit" name="submitBtn" class="sbtn" value="Send" />';
      echo '</form></p>';
    }
    
    function sendMail() {
        $subject = $this->subjectText;
        $from    = "From: $this->name <$this->email>\r\nReply-To: $this->email\r\n"; 
        $header  = "MIME-Version: 1.0\r\n"."Content-type: $this->mailFormat; charset=utf-8";
        $content = $this->message;
        if ($this->additionalText != ''){
            $content .= "\r\n\r\nAdditional information:\r\n\r\n"
                     .$this->additionalText;
        }
        
        $content = wordwrap($content,70);
        @mail($this->targetEmail,$subject,$content,$from.$header);
 
    }
    
    function isValidEmail($email){
        $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
     
        if (eregi($pattern, $email)){
            return true;
        }
        else {
            return false;
        }   
    }
    
    function processForm(){
        if (isset($_POST['submitBtn'])){
            $email   = isset($_POST['email']) ? trim($_POST['email']) : '';
            $name    = isset($_POST['name']) ? trim($_POST['name']) : '';
            $message = isset($_POST['message']) ? trim($_POST['message']) : '';
            
            if (!$this->isValidEmail($email)) $this->errorList[] = "Invalid email address!";
            if (strlen($name)<2) $this->errorList[] = "Invalid name! It must be at least 2 characters long.";
            if (strlen($message)<5) $this->errorList[] = "Invalid message! It must be at least 10 characters long.";
 
            $this->email = $email;
            $this->name  = $name;
            $this->message = htmlspecialchars($message);
            foreach ($this->fieldList as $key=>$value) {
                if (isset($_POST[$value['fieldName']])) {
                    $this->fieldList[$key]['value'] = $_POST[$value['fieldName']];
                    $this->additionalText .= $value['caption'] . " : " . $_POST[$value['fieldName']]."\r\n";
                }
            }
 
            
            if (sizeof($this->errorList) > 0){
                $this->showErrors();
                $this->showForm();
            } else {
                $this->sendMail();
                $this->showInfo();
            }
            
        } else {
            $this->showForm();
        }
    }
    
    function showErrors(){
        foreach ($this->errorList as $value) {
            echo "<p>$value</p>";
        }            
    }
    
    function showInfo(){
        echo "<p>Thanks for your message!</p>";
    }
    
    
}
?>
any help will be great, and sorry if this is in the wrong section

-DreamOn
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: PHP Form Script

Post by Skoalbasher »

Just pull the email and name from the DB and then use a loop to create a dropdown box.
DreamOn
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2009 8:33 am

Re: PHP Form Script

Post by DreamOn »

no mysql here, just php

I did have it working in HTML, but I needed it to validate the form(check validate email, name was entered and message was entered), so I changed the form to php and now I cant get it to work.

I don't want JavaScript form validation incase the user has JavaScript disabled so im left with php.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: PHP Form Script

Post by Skoalbasher »

DreamOn wrote:no mysql here, just php

I did have it working in HTML, but I needed it to validate the form(check validate email, name was entered and message was entered), so I changed the form to php and now I cant get it to work.

I don't want JavaScript form validation incase the user has JavaScript disabled so im left with php.
Oh ok, gotcha. To be honest with you, if you were using MySQL and no classes I could help yea. I mean, I know how to do what you're asking, but I don't use classes yet. I'm still trying to perfect what I already know before trying to learn completely new methods of doing something. You know, Like i need to get really good at riding my bike before attempting a wheelie.

If you don't use a class you could just slap the php you have for creating the dropdown box wherever you need it.
DreamOn
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2009 8:33 am

Re: PHP Form Script

Post by DreamOn »

well im not really good with php, just kinda new at it for this project, i got alot of help from a friend of mine but what im asking to do he doesnt know how to do it..

guess i could wait for another reply by someone who has an answer, im still messing around with it myself to get it to work, but thats getting know where :x

thanks for the help

- DreamOn
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: PHP Form Script

Post by Skoalbasher »

Code: Select all

 
echo "<select name=\"targetemail\">";
echo "<option value=\"john@example.com\" name=\"john\">John</option>";
echo "<option value=\"mark@example.com\" name=\"mark\">Mark</option>";
echo "</select><br />";
 
This not work?
DreamOn
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2009 8:33 am

Re: PHP Form Script

Post by DreamOn »

hello again, sorry didnt reply went away for a week.

i tried your solution but again, it still did not work.

any other ideas?

-DreamOn
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: PHP Form Script

Post by s.dot »

Your option fields shouldn't have names.

Example:

Code: Select all

<select name="targetemail">
<option value="a@b.tld">John</option>
</select>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
DreamOn
Forum Newbie
Posts: 5
Joined: Fri Feb 06, 2009 8:33 am

Re: PHP Form Script

Post by DreamOn »

ok i changed the part of the php so that it looks like the following..

Code: Select all

   function showForm(){
      $message = '';  
      echo '   <p><form action="'.$_SERVER['PHP_SELF'].'" method="post">';
 
      echo "<select name=\"targetemail\">";
      echo "<option value=\"xxx@googlemail.com\">Google Account</option>";
      echo "<option value=\"xxx@hotmail.com\">Hotmail Account</option>";
      echo "</select><br />";
 
      echo '   Full Name:<br />'
          .'   <input type="text" name="name" value="'.$this->name.'" size="40"/><br />';
      echo '   Email:<br />'
          .'   <input type="text" name="email" value="'.$this->email.'" size="50"/><br />';           
      
      echo '    Message:<br />'
          .'    <textarea cols="65" rows="15" name="message">'.$this->message.'</textarea><br />'
          .'<input type="submit" name="submitBtn" class="sbtn" value="Send" />';
      echo '</form></p>';
    }
still didnt receive email to my account
Post Reply