SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting value based on selection of an array

    I am having some trouble setting a value to a variable based on the value of the array. Basically I want to assign a subject based on the selection of the recipient using an array. I'll post my code and that way you can have a full picture.

    Here is the code broken down that I'm trying to figure out:

    PHP Code:
    //This is who will get the email based on selection from the $to in the form.
    $receivers = array(
         
    => 'email1@example.com',
         
    => 'email2@example.com'
    );

    //Set $receivers based on selection of $to from the form.
    if(array_key_exists($formData['to'], $receivers))
    {
        
    $to $receivers[$formData['to']];
    }
    else
    {
        
    $to 'example@example.com';

    Now here is where I'm having my trouble. I want to have the $subject variable set based on the value of the $receivers array. I also want to keep the option of using a value from a text box on a form, so that I can use this script anywhere with little to no modification.

    PHP Code:
        if(strlen($formData['subject']) > 0)
    {
        
    $subject sanitizeString($formData['subject']);
    }
    elseif
    {
        
    $receivers "1";

        else( 
    $receivers == "2" $subject "Subject 2";
        else                  
    $subject "Subject 1";


    From what I can tell, this is kinda working. When the form sends out the email it assigns Subject 1 as the subject regardless of the $receivers array value.

    If you can help that would be greatly appreciated.

  2. #2
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't quite understand what you're trying do with the code in the second example. Firstly you are missing the condition on the elseif.

    Aside from that, perhaps you can alter your $receivers array so that you have a multi-dimensional array (i.e. each element itself is also an array) allowing you to story email addresses as well as subjects?

    Something like this:

    Code PHP:
    $receivers = array(
    1 => array('email1', 'subject1'),
    2 => array('email2', 'subject2')
    );

    Then you can call $receivers[$formData['to']][0] for email, $receivers[$formData['to']][1] for subjects.

  3. #3
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks N9ne for the reply!

    What I'm trying to do is create a multi-use contact form script that I can use for my site and other projects. The idea for the email address selection setting the subject is to give me the option to exclude a subject input field on the form. I still want the option to have the field if I need it. That is what I'm trying to do with my second block of code. In hopes that it helps here is the complete working script. I just don't know how to set the array like your suggesting. Is there a way to include the option for the input field?

    PHP Code:
    //Sainitize function
    function sanitizeString($value)
    {
        
    $value strip_tags($value);
        
    $value trim($value);
        
    $value escapeshellcmd($value);
        
    $value htmlentities($value);

        return 
    $value;
    }

    $errorMessage = array();
    $receivers = array(
        
    => 'email1@email.com',
        
    => 'email2@email.com'
    );

    if(isset(
    $_POST['form']))
    {
        
    $formData $_POST['form'];

        if (
    filter_var($formData['from'], FILTER_VALIDATE_EMAIL)) {
            
    $from sanitizeString($formData['from']);
        }
        else
        {
            
    $errorMessage[] = "Please use a valid email format: name@domain.com";
        }

        if(
    array_key_exists($formData['to'], $receivers))
        {
            
    $to $receivers[$formData['to']];
        }
        else
        {
            
    $to 'example@example.com';
        }

        if(
    strlen($formData['name']) > 0)
        {
            
    $name sanitizeString($formData['name']);
        }
        else
        {
            
    $errorMessage[] = "Please enter your name.";
        }

        if(
    strlen($formData['title']) > 0)
        {
            
    $title sanitizeString($formData['title']);
        }
        else
        {
            
    $title '';
        }

        if(
    strlen($formData['company']) > 0)
        {
            
    $company sanitizeString($formData['company']);
        }
        else
        {
            
    $company '';
        }

        if(
    strlen($formData['phone']) > 0)
        {
            
    $phone sanitizeString($formData['phone']);
        }
        else
        {
            
    $errorMessage[] = "Please enter a phone number.";
        }

        if(
    strlen($formData['subject']) > 0)
        {
            
    $subject sanitizeString($formData['subject']);
        }
        else
        {
            
    $subject '';
        }

        if(
    strlen($formData['message']) > 0)
        {
            
    $message sanitizeString($formData['message']);
        }
        else
        {
            
    $errorMessage[] = 'Cannot leave message box blank.';
        }

        if (empty(
    $errorMessage) && $formData['spam'] == 9)
        {
            
    $email_headers "From:" $from "\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";
            
    $message_send "<h3>" $name "<br>" $title "<br>" $company "<br>" $phone "<br>" $from "</h3><hr><h4>" $subject "</h4>" $message;

            if (
    mail($to$subject$message_send$email_headers))
            {
                
    $errorMessage[] = 'Thank you, your email is on the way!';
            }
            else
            {
                
    $errorMessage[] = 'There seems to be a problem!';
            }
        }

    Having the subject use a input field and using an if statement when there is not a subject input field on the form is really not important at this time. Hence why I was trying to make

    PHP Code:
    if(strlen($formData['subject']) > 0

        
    $subject sanitizeString($formData['subject']); 

    elseif 

        
    $receivers "1"

        else( 
    $receivers == "2" $subject "Subject 2"
        else                  
    $subject "Subject 1"


    work for me. Any help would be greatly appreciated!

  4. #4
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I have a fix...using the switch function.

    PHP Code:
    //select the correct to address
    switch ($to)
    {
    case 
    "1":
        
    $to "example@example.com";
        
    $subject "Subject 1";
        break;
    case 
    "2":
        
    $to "example2@example.com";
        
    $subject "Subject 2";
        break;
    default:
        
    $to "email@example.com";
            
    $subject "Default";
        break;

    If you have a better solution, please let me know. Thank you all for your help!

  5. #5
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tag.legacy View Post
    Well I have a fix...using the switch function.
    Scratch that, I'm messing something up. I can't even send out an email now with the form.

    I think it has to do with my

    PHP Code:
    if(strlen($formData['to']) > 0)
    {
        
    $to sanitizeString($formData['to']);
    }
    else
    {
        
    $to 'example@example.com';

    for $to...would I need to add anything for $subject or would the switch handle that on it's own?

    Either way, I'm at a lost, any help please...

    Here is my complete code, with the switch function instead of the array...if you see something, I clearly don't, your help will be greatly appreciated.

    PHP Code:
    <?php
    //Sainitize function
    function sanitizeString($value)
    {
    $value strip_tags($value);
    $value trim($value);
    $value escapeshellcmd($value);
    $value htmlentities($value);

    return 
    $value;
    }

    $errorMessage = array();

    switch (
    $to
    {
    case 
    "1":
        
    $to "example1@example.com";
        
    $subject "Subject 1";
        break;
    case 
    "2":
        
    $to "example2@example.com";
        
    $subject "Subject 2";
        break;
    default:
        
    $to "example@example.com";
        break;
    }

    if(isset(
    $_POST['form']))
    {
    $formData $_POST['form'];

    if (
    filter_var($formData['from'], FILTER_VALIDATE_EMAIL)) {
        
    $from sanitizeString($formData['from']);
    }
    else
    {
        
    $errorMessage[] = "Please use a valid email format: name@domain.com";
    }

    if(
    strlen($formData['to']) > 0)
    {
        
    $to sanitizeString($formData['to']);
    }
    else
    {
        
    $to 'example@example.com';
    }

    if(
    strlen($formData['name']) > 0)
    {
        
    $name sanitizeString($formData['name']);
    }
    else
    {
        
    $errorMessage[] = "Please enter your name.";
    }

    if(
    strlen($formData['title']) > 0)
    {
        
    $title sanitizeString($formData['title']);
    }
    else
    {
        
    $title '';
    }

    if(
    strlen($formData['company']) > 0)
    {
        
    $company sanitizeString($formData['company']);
    }
    else
    {
        
    $company '';
    }

        if(
    strlen($formData['phone']) > 0)
    {
        
    $phone sanitizeString($formData['phone']);
    }
        else
    {
        
    $errorMessage[] = "Please enter a phone number.";
    }

        if(
    strlen($formData['message']) > 0)
    {
        
    $message sanitizeString($formData['message']);
    }
        else
    {
        
    $errorMessage[] = 'Cannot leave message box blank.';
    }

        if (empty(
    $errorMessage) && $formData['message'])
    {
        
    $email_headers "From:" $from "\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";
        
    $message_send "<h3>" $name "<br>" $title "<br>" $company "<br>" $phone "<br>" $from "</h3><hr><h4>" $subject "</h4>" $message;

        if (
    mail($to$subject$message_send$email_headers))
    {
        
    $errorMessage[] = 'Thank you, your email is on the way!';
    }
        else
    {
        
    $errorMessage[] = 'There seems to be a problem!';
    }
    }
    }

  6. #6
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any help would be greatly appreciated...I think it has something to do with my validation that keeps the $to from being set by the switch. It will send to the default every time. Just don't know where I'm making my mistake. If you can help, please let me know. Thanks!

  7. #7
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by N9ne View Post
    Then you can call $receivers[$formData['to']][0] for email, $receivers[$formData['to']][1] for subjects.
    How do I do that? I've tried it and I either get a 500 error or a blank page...can you show me an example of setting the email and subject based on the array?

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    738
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    N9ne's suggestion was good. I did a slight variation in that I used the email as the array key with the value as subject. Added a sloppy form at end for testing which isn't needed, but wanted to show that the same $receivers array builds form selection fields.
    PHP Code:
    <?php
    //Sainitize function
    function sanitizeString($value){
        
    $value strip_tags($value);
        
    $value trim($value);
        
    $value escapeshellcmd($value);
        
    $value htmlentities($value);
        
        return 
    $value;
    }

    //Receivers
    $receivers = array();
    //IF built with query put email as key, subject as value in loop or specify like below
    $receivers['default@example.com']  = 'Default Subject';
    $receivers['example1@example.com'] = 'Subject 1';
    $receivers['example2@example.com'] = 'Subject 2';



    $errorMessage = array();

    if(isset(
    $_POST['form'])){
        
    $formData $_POST['form'];
        
        if (
    filter_var($formData['from'], FILTER_VALIDATE_EMAIL)) {
            
    $from sanitizeString($formData['from']);
        }else{
            
    $errorMessage[] = "Please use a valid email format: name@domain.com";
        }
        
        if(
    strlen($formData['to']) > 0){
            
    $to sanitizeString($formData['to']);
        }else{
            
    $to 'example@example.com';
        }
        
        if(
    strlen($formData['name']) > 0){
            
    $name sanitizeString($formData['name']);
        }else{
            
    $errorMessage[] = "Please enter your name.";
        }
        
        if(
    strlen($formData['title']) > 0){
            
    $title sanitizeString($formData['title']);
        }else{
            
    $title '';
        }
        
        if(
    strlen($formData['company']) > 0){
            
    $company sanitizeString($formData['company']);
        }else{
            
    $company '';
        }
        
        if(
    strlen($formData['phone']) > 0){
            
    $phone sanitizeString($formData['phone']);
        }else{
            
    $errorMessage[] = "Please enter a phone number.";
        }
        
        if(
    strlen($formData['message']) > 0){
            
    $message sanitizeString($formData['message']);
        }else{
            
    $errorMessage[] = 'Cannot leave message box blank.';
        }
        
        if (empty(
    $errorMessage) && $formData['message']){        
        
            if(
    strlen($formData['subject']) > 0){
                
    $subject sanitizeString($formData['subject']);
            }else{
                
    $subject $receivers[$to];
            }
            
            
    $email_headers "From:" $from "\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";
            
    $message_send "<h3>" $name "<br>" $title "<br>" $company "<br>" $phone "<br>" $from "</h3><hr><h4>" $subject "</h4>" $message;
            
            if (
    mail($to$subject$message_send$email_headers)){
                
    $errorMessage[] = 'Thank you, your email is on the way!';
            }else{
                
    $errorMessage[] = 'There seems to be a problem!';
            }
        }
    }

    ////Sample form
    ?>
    <html>
        <body>
        <?php
            
    if(!empty($errorMessage)):
                foreach(
    $errorMessage as $message):
                    echo 
    '<p>' $message '</p>';
                endforeach;
            endif;
        
    ?>        
            <form action="" method="post">
                <input type="text" name="form[from]" value="" />from<br />
                <input type="text" name="form[name]" value="" />name<br />
                <input type="text" name="form[title]" value="" />title<br />
                <input type="text" name="form[company]" value="" />company<br />
                <input type="text" name="form[phone]" value="" />phone<br />
                <input type="text" name="form[message]" value="" />message<br />
                <input type="text" name="form[subject]" value="" />subject<br />
                <?php
                
    foreach($receivers as $email => $sub):                
                    echo 
    '<input type="radio" name="form[to]" value="' $email '" />' $email '<br />';        
                endforeach;
                
    ?>
                <br />
                <input type="submit" name="Submit" value="Submit" />
            </form>
        </body>
    </html>

  9. #9
    SitePoint Member
    Join Date
    Jan 2014
    Location
    Houston, Texas
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much! That works amazingly!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •