Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
// Set email variables
$email_to = 'xxx.com';
$email_subject = 'Tap';
  
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
    'fullname' => 'Please enter a Name to proceed.',
    'email' => 'Please enter a valid Email Address.',
    'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
    // Sanitise POST array
    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
    
    // Loop into required fields and make sure they match our needs
    foreach($required_fields as $field) {       
        // the field has been submitted?
        if(!array_key_exists($field, $_POST)) array_push($validation, $field);
        
        // check there is information in the field?
        if($_POST[$field] == '') array_push($validation, $field);
        
        // validate the email address supplied
        if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
    }
    
    // basic validation result
    if(count($validation) == 0) {
        // Prepare our content string
        $email_content = 'Tap : ' . "\n\n";
        
        // simple email content
        foreach($_POST as $key => $value) {
            if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
        }
        
        // if validation passed ok then send the email
        mail($email_to, $email_subject, $email_content);
        
        // Update form switch
        $form_complete = TRUE;
    }
}
function validate_email_address($email = FALSE) {
    return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
   return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
 
//  usable constants  
define('DB_NAME', 'yyy_ContactForm');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'zzz');
define('DB_HOST', 'localhost');
$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_select_db(DB_NAME,$link);
if(!$db_selected) {
    die('Can\'t use' . DB_NAME . ':' .mysql_error());
}
//echo 'Connected successfully';
/* $fullname = $_POST['fullname'];
$email = $_POST['email'];
$comment = $_POST['comment']; */
$fullname = mysql_real_escape_string ($_POST['fullname']);
$email = mysql_real_escape_string ($_POST['email']);
$comment = mysql_real_escape_string ($_POST['comment']);
/*~~~~  $error = array(); 
if($_POST[$fullname, $email, $comment] == '') { 
    $error[] = ''; 
}  ~~~~*/
$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', 
'$email', '$comment')";
if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}
mysql_close();
 
window.addEvent('domready', function() {
    // Get the form
    var form = $('comments_form');
    
    //  if the form is found...
    if (form) {
        // obtain error fields
        var name = $('fullname');
        var email = $('email');
        var comment = $('comment');
        // Set the default status
        var isValid = true;
        // input error function for the error messages
        var addError = function (field, msg) {
            field.addClass('error'); // Add error class to field
            var error = field.getParent().getElement('span') || new Element('span', {'class': 'error'}); // add error message if not already placed
            error.set('text', msg); // error text msg
            error.inject(field, 'after'); // Insert error message after field
        };
        // detach error function used to delete any error messages and remove the error class
        var removeError = function (field) {
            field.removeClass('error'); // Remove error class from form fields
            var error = field.getParent().getElement('span'); // find any existing error messages
            // destroy if error message
            if (error) {
                error.destroy();
            }
        };
        //  insert submit form event
        form.addEvent('submit', function (e) {
            // Test name length
            if (name.get('value').length === 0) {
                isValid = false;
                addError(name, nameError);
            } else {
                isValid = true;
                removeError(name);
            }
            // check email length
            if (email.get('value').length === 0) {
                isValid = false;
                addError(email, emailError);
            // check email validity
            } else if (!email.get('value').test(/^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/)) {
                isValid = false;
                addError(email, emailError);
            } 
          
          //else {
            //  isValid = true;
            //  removeError(email);
            //}
            // check comment length
            if (comment.get('value').length === 0) {
                isValid = false;
                addError(comment, commentError);
            } 
          //else {
            //  isValid = true;                     
            //  removeError(comment);
            //}
            // If form invalid then stop event happening
            if (!isValid) {
                e.stop();
            }
        });
    }   
});
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
amkaospro
0viewers