0

I have a php contact form which i would like to add some code on it but not sure how. So basically i have this form that is for online appointment. on my drop down selection for "department" i have multiple selections. How can i make the php form to send the request to a receiver email address by user selection. For instance if someone will select admin dept. email should be send to admin@domain if someone will select accounts dept. email should go to account@domain. Here is my php form code:

enter code here
<?php
/*

Variable 
$dzName : Contact Person Name
$dzEmail : Contact Person Email
$dzMessage : Contact Person Message
$dzRes : response holder
$dzOtherField : Form other aditional fields


$dzMailSubject : Mail Subject.
$dzMailMessage : Mail Body
$dzMailHeader : Mail Header
$dzEmailReceiver : Contact receiver email address
$dzEmailFrom : Mail Form title
$dzEmailHeader : Mail headers
*/

$dzEmailTo = "[email protected];   // Receiver Email Address                         "[email protected]" 
$dzEmailFrom   = "Website form";

function pr($value)
{
echo "<pre>";
print_r($value);
echo "</pre>";
}


#### Appoinment Form Script ####
if(!empty($_POST) && $_POST['dzToDo'] == 'Appoinment')
{
$dzName = trim(strip_tags($_POST['dzName']));
$dzEmail = trim(strip_tags($_POST['dzEmail']));
$dzMessage = strip_tags($_POST['dzMessage']);   
$dzRes = "";
if(!filter_var($dzEmail, FILTER_VALIDATE_EMAIL)) 
{
    $dzRes['status'] = 0;
    $dzRes['msg'] = 'Adresa Email gresita.';
    echo json_encode($dzRes);
    exit;
}



$dzMailSubject = 'Formular programare online';
$dzMailMessage  =   "
                    Programare online: <br><br>
                    Name: $dzName<br/>
                    Email: $dzEmail<br/>
                    Message: $dzMessage<br/>
                    ";
$dzOtherField = "";
if(!empty($_POST['dzOther']))
{
    $dzOther = $_POST['dzOther'];
    $message = "";
    foreach($dzOther as $key => $value)
    {
        $fieldName = ucfirst(str_replace('_',' ',$key));
        $fieldValue = ucfirst(str_replace('_',' ',$value));
        $dzOtherField .= $fieldName." : ".$fieldValue."<br>";
    }
}
$dzMailMessage .= $dzOtherField; 

$dzEmailHeader      = "MIME-Version: 1.0\r\n";
$dzEmailHeader      .= "Content-type: text/html; charset=iso-8859-1\r\n";
$dzEmailHeader      .= "From:$dzEmailFrom <$dzEmail>";
$dzEmailHeader      .= "Reply-To: $dzEmail\r\n"."X-Mailer:                           PHP/".phpversion();
if(mail($dzEmailTo, $dzMailSubject, $dzMailMessage, $dzEmailHeader))
{
    $dzRes['status'] = 1;
    $dzRes['msg'] = 'Va multumim pentru mesaj. Va vom raspunde la cerere     in cel mai scurt timp posibil.';
}
else
{
    $dzRes['status'] = 0;
    $dzRes['msg'] = 'Eroare! Va rugam incercati din nou!.';
}
echo json_encode($dzRes);
exit;
}   
#### Appoinment Form Script End ####

then i have this on my form:

<div class="col-md-6 col-sm-12 text-center worker">
                        <div class="p-lr40 p-t30 p-b40 clearfix mack-an-appointment black">
                            <form method="post" class="dzForm" action="script/contact.php">
                            <input type="hidden" value="Appoinment" name="dzToDo" >
                            <div class="row">
                                <div class="text-white text-center">
                                    <h2 class="h2 m-t0">Programare<span class="text-primary">online!</span></h2>
                                    <div class="dez-separator-outer"><div class="dez-separator bg-primary style-liner"></div></div>
                                    <p class="title-small">Folositi acest formular pentru a solicita o programare.<br></p>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                            <input name="dzName" class="form-control" placeholder="Nume" type="text">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                            <input name="dzEmail" class="form-control" placeholder="Email" type="text">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                            <input name="dzOther[Telefon]" class="form-control" placeholder="Telefon" type="text">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                            <select class="bs-select-hidden" name="dzOther[Departament]" >
                                               <option value="admin">Secretariat</option>
                                               <option value="accounts">Casierie</option>
                                               <option value="management">Decanat</option>
                                               <option value="sdut">Cancelarie studenti</option>
                                            </select>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <div class="input-group">
                                            <textarea name="dzMessage" rows="4" class="form-control" required=""></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-12 text-center">
                                    <div class="dzFormMsg" ></div>
                                    <button name="Reset" value="Reset" type="reset" class="site-button skew-secondry"><span>Reseteaza</span></button>
                                    <button name="submit" type="submit" value="Submit" class="site-button skew-secondry"><span>Trimite</span></button>

1 Answer 1

1

First of all, create an array of email ids like this:

$emailList = array('admin' => 'admin@domain', 
                'accounts' => 'accounts@domain'
                'management' => 'management@domain',
                'sdut' => 'sdut@domain'
            );

The keys in the above array should exactly matches with the dropdown list's value attributes

Now when the user submits the form, use $_POST['dzOther']['Departament'] value to get user's response and use it as a key to fetch the appropriate email id from $emailList array, and send email accordingly.

$dzEmailTo = $emailList[$_POST['dzOther']['Departament']];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Paul, I think i`m not doing something right... i added the code my .php file and for some reason is not working. The form do nothing...
@AndreiFechete Have you checked the status of $dzEmailTo? Do var_dump($dzEmailTo); and see what you have in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.