Jump to content

Recommended Posts

Hi Guys

 

I've just finished a new website and for some reason i'm having issues on the php code for the contact form.

 

I want a Name field ( Manditory) Phone field, Email field and message field. The only field manditory is name.

 

Now the form i have inherrited is already set up as name, email and message all with validation. Now i want to take this validation off and add a phone field. I have dont this on my contact form in page. But comign to the php config i cant get it to work. Pasted below is the code in a working state, i want to strip out all the validation as i'm not fussed if people dont put the email in and i want to add the phone data so it takes it from my form.

 

Any ideas, help, advice would really help as i'm stuck.

 

<?php
/*
    Spark – Simple and Effective
    Rev. 6
*/

// Load WordPress
require( '../../../../../wp-load.php' );

// Grab all Spark theme options
global $spark_options;
$spark_options = get_spark_options();

// Get user's email (defined in the OptionTree backend)
$to_email = $spark_options['spark_user_email'];


$aErrors = array();
$aResults = array();

/* Functions */

function stripslashes_if_required($sContent) {

    if(get_magic_quotes_gpc()) {
        return stripslashes($sContent);
    } else {
        return $sContent;
    }
}

function get_current_url_path() {

    $sPageUrl = "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    $count = strlen(basename($sPageUrl));
    $sPagePath = substr($sPageUrl,0, -$count);
    return $sPagePath;
}

function output($aErrors = array(), $aResults = array()){ // Output JSON

    $bFormSent = empty($aErrors) ? true : false;
    $aCombinedData = array(
        'bFormSent' => $bFormSent,
        'aErrors' => $aErrors,
        'aResults' => $aResults
        );
        
    header('Content-type: application/json');
    echo json_encode($aCombinedData);
    exit;
}

// Check supported version of PHP
if (version_compare(PHP_VERSION, '5.2.0', '<')) { // PHP 5.2 is required for the safety filters used in this script

    $aErrors[] = 'Unsupported PHP version. <br /><em>Minimum requirement is 5.2.<br />Your version is '. PHP_VERSION .'.</em>';
    output($aErrors);
}


if (!empty($_POST)) { // Form posted?

    // Get a safe-sanitized version of the posted data
    $sFromEmail = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $sFromName = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
    $sMessage  = stripslashes_if_required($_POST['message']);
    $sMessage .= "\r\n--\r\nEmail sent from ". get_current_url_path();
    
    $sHeaders  = "From: '$sFromName' <$sFromEmail>"."\r\n";
    $sHeaders .= "Reply-To: '$sFromName' <$sFromEmail>";
    
    if (filter_var($sFromEmail, FILTER_VALIDATE_EMAIL)) { // Valid email format?
    
        $bMailSent = mail($to_email, "New inquiry from $sFromName", $sMessage, $sHeaders);
        if ($bMailSent) {
            $aResults[] = "Message sent, thank you!";
        } else {
            $aErrors[] = "Message not sent, please try again later.";
        }

    } else {
        $aErrors[] = 'Invalid email address.';
    }
} else { // Nothing posted
    $aErrors[] = 'Empty data submited.';
}

 
output($aErrors, $aResults);

Link to comment
https://forums.phpfreaks.com/topic/275809-contact-form-on-my-website/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.