tags:

views:

148

answers:

2

i have bought a template that have built in contact form problem is that it submits every thing except "company" name i messed around with it but cant get to work it. if you can point me to some solution i would be greatful

thanks in advance

this is php script

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','message');
    $required = array('name','email','message');

    $your_email = "[email protected]";
    $email_subject = "New Message: ".$_POST['subject'];
    $email_content = "new message:\n";

    foreach($values as $key => $value){
      if(in_array($value,$required)){
        if ($key != 'subject' && $key != 'company') {
          if( empty($_POST[$value]) ) { echo 'Please fill in all required fields, marked with *'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
      }
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Thanks for your message, will contact you soon.'; 
    } else {
        echo 'ERROR!';
    }
}
+3  A: 
$values = array ('name','email','message');

Add 'company' to this list in the PHP script.

Also, I would modify the foreach loop to look like this, to get the intended functionality:

foreach($values as $key => $value)
{
    if(in_array($value,$required))
    {
        if(empty($_POST[$value]))
        {
                echo 'Please fill in all required fields, marked with *';
                exit;
        }
    }
    $email_content .= $value.': '.$_POST[$value]."\n";
}

This way, fields that aren't in the $required array are still added to the email if they exist - they just don't have to pass the empty check.

Amber
i tried but form will not send an email nowalso adding to this array will make company a required field in form?$required = array('name','email','message');
steve
Then add it to $values only. That's the one that tells the script which fields need to be put into the email message (if the foreach loop).
Marc B
@Marc B sorry this doesnt help, form will not submit filed
steve
Then look at the javascript you had posted earlier. If the field/value makes it to the server, PHP can deal with it. It won't just selectively ignore one field because it doesn't like your cologne or whatever.
Marc B
From the code posted, since the call to append the field to the email content array is inside an `if(in_array($value, $required))` block, it actually only includes the required fields in the email as coded. I thought it was odd.
Amber
@dav can i correct this?
steve
@Marc B i have repoested javascript, it looks like it does get the fields through to php
steve
@steve I've updated my answer to include a proper version of the foreach loop that should make it behave how one would normally expect.
Amber
+1  A: 

The problem is this line:

$email_content .= $value.': '.$_POST[$value]."\n";

Is never reached unless this line:

if (in_array($value,$required)) {

... is satisfied. This means, only fields listed in $required are going to be appended to the email. If that is OK, then simply change these lines:

$required = array('name','email','message');
$values = array ('name','email','message');

To read:

$required = array ('name','email','message','company');
$values = array ('name','email','message','company');

If that is NOT ok, change this:

foreach ($values as $key => $value) {
    if (in_array($value,$required)) {
        if ($key != 'subject' && $key != 'company') {
            if (empty($_POST[$value])) {
                echo 'Please fill in all required fields, marked with *';
                exit;
            }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
    }
}

To look like this:

foreach ($values as $key => $value) {
    if (in_array($value,$required)) {
        if ($key != 'subject' && $key != 'company') {
            if (empty($_POST[$value])) {
                echo 'Please fill in all required fields, marked with *';
                exit;
            }
        }
    }
    $email_content .= $value.': '.$_POST[$value]."\n";
}

Then you can take company out of $required, but leave it in $values. I suspect the template worked fine when you started because all fields were required.

If it still does not work, please change this (up top):

if(!$_POST) exit;

To look like this:

if(!$_POST) exit;
print_r($_POST);

... and paste the additional output in your question.

Also, consider buying templates from another vendor :)

Tim Post