Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    127
    Thanks
    9
    Thanked 0 Times in 0 Posts

    php contact form array error

    I'm having problem in my send contact php file. I wanted the email to send based on the subject the visitor choose, for example: Pedicure Booking subject the email will send to enquiry@gmail.com while Booking Time subject will send to info@gmail.com

    below is my php code.

    PHP Code:
    <?php
    function emailswitch$key ) {
        
    $to = array(
            
    'Pedicure Price Inquiry' => 'enquiry@gmail.com',
            
    'Manicure Price Inquiry' => 'enquiry@gmail.com',
            
    'Booking Time' => 'info@gmail.com'
        
    );
        
    $default 'info@gmail.com';
        return isset( 
    $_POST['field_?'] ) && !empty($to$_POST['field_?'] ])  ? $to$_POST['field_?'] ]: $default;  $from $_POST['email'];
    }
    $from_name $_POST['name'];
    $email $_POST['email'];
    $subject $_POST['category']; 

    // collect data
    $body "";
    foreach(
    $_POST as $key => $val)
    {
        if(
    $key != 'captcha')
            
    $body .= ucfirst($key).": ".$val."\r\n";
    }

    // construct MIME PLAIN Email headers
    $header "MIME-Version: 1.0\n";
    $header .= "Content-type: text/plain; charset=utf-8\n";
    $header .= "From: $from_name <$email> \r\nReply-To: $from_name <$email> \r\nReturn-Path: <$email> \r\n";

    // send email
    $to emailswitch$subject );
    $mail_sent mail($to$subject$body$header);
    ?>
    using this code
    PHP Code:
    return isset( $_POST['field_?'] ) && !empty($to$_POST['field_?'] ])  ? $to$_POST['field_?'] ]: $default;  $from $_POST['email']; 
    all the email send to default email

    if i change to this code
    PHP Code:
    return (!empty($to[$key]))?$to[$key]:$default
    the email cant send out

    below is my html code for the form

    Code:
    <div id="contact_form">
                        <form action="sendContact.php" method="post" onsubmit="return sendContact();">
                            <p><label id="lname" for="name">Full name:</label>
                            <input id="name" class="text" name="name" onblur="input_blur('name');" onfocus="input_focus('name');" type="text" /></p>
                            <p><label id="lemail" for="email">Email address:</label>
                            <input id="email" class="text" name="email" onblur="input_blur('email');" onfocus="input_focus('email');" type="text" /></p>
                            <div class="x"></div>
                            <p id="email-error" class="error">You must enter your email address!</p>
                            <p><label id="lcategory" for="category">Category:</label>
                            <select id="category" name="category" onblur="input_blur('category');" onfocus="input_focus('category');">
                            <option value="Pedicure Price Inquiry">Pedicure Price Inquiry</option>
                            <option value="Manicure Price Inquiry">Manicure Price Inquiry</option>
                            <option value="Booking Time">Booking Time</option></select></p>
                            <p><label id="lmessage" for="message">Message:</label>
                            <textarea id="message" name="message" onblur="input_blur('message');" onfocus="input_focus('message');" rows="" cols=""></textarea></p>
                            <div class="x"></div>
                            <p id="message-error" class="error">You must enter your message!</p>
                            <p><label id="lcaptcha" for="captcha"></label>
                            <input id="captcha" class="text" name="captcha" onblur="input_blur('captcha');" onfocus="input_focus('captcha');" type="text" /></p>
                            <div class="x"></div>
                            <p id="captcha-error" class="error">Are you sure about your calculations?</p>
                            <script type="text/javascript">
                                generate_captcha('lcaptcha');
                            </script>
                            <div class="x"></div>
                            <input class="submit" name="send_contact" type="submit" value="Send" /><input class="submit" type="reset" value="Reset" />
                        </form>
                    </div>
                    <div id="message_sent" style="display: none;">
                        <h1>Your message has been sent</h1>
                        <p>We&#39;ll contact you as soon as possible.</p>
                        <p>You can now <a href="./">go back</a> to home page.</p>
                    </div>
    im still novice in php can someone enlighten me where did i go wrong?
    thanks a million!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What is "field_?"? It's not listed anywhere in your submission fields.
    What you have available in $_POST will be 'name', 'email', 'category', 'message', 'captcha', and possibly 'send_contact'. Given the argument, I'd say that should be using $key (representing 'category').

    This return call isn't correct. After the initial instruction it then attempts to set a variable, but it will never reach that point in the code. So that can be removed. Simply use this for a return:
    PHP Code:
    return (isset($_POST[$key]) && isset($to[$_POST[$key]]) ? $to[$_POST[$key]] : $default); 
    Edit:
    Wait a sec, since you're already providing the $key a value that is $_POST['category'] and not just 'category', than you can use this (which is pretty much what you had):
    PHP Code:
    return (isset($to[$key]) ? $to[$key] : $default); 
    You'll need to make sure you use an isset on the POST prior to calling the function though.
    If that still doesn't work, make sure you check the results of the call to the emailswitch function.
    Last edited by Fou-Lu; 03-22-2013 at 08:07 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    lazyserv (03-25-2013)

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    127
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    What is "field_?"? It's not listed anywhere in your submission fields.
    What you have available in $_POST will be 'name', 'email', 'category', 'message', 'captcha', and possibly 'send_contact'. Given the argument, I'd say that should be using $key (representing 'category').

    This return call isn't correct. After the initial instruction it then attempts to set a variable, but it will never reach that point in the code. So that can be removed. Simply use this for a return:
    PHP Code:
    return (isset($_POST[$key]) && isset($to[$_POST[$key]]) ? $to[$_POST[$key]] : $default); 
    Edit:
    Wait a sec, since you're already providing the $key a value that is $_POST['category'] and not just 'category', than you can use this (which is pretty much what you had):
    PHP Code:
    return (isset($to[$key]) ? $to[$key] : $default); 
    You'll need to make sure you use an isset on the POST prior to calling the function though.
    If that still doesn't work, make sure you check the results of the call to the emailswitch function.
    tested both code, not working and there was no error log... ouch really headache now... i wondering where went wrong...

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What's the result when you print this prior to returning: print (isset($to[$key]) ? $to[$key] : $default); ?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Jan 2010
    Posts
    127
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    What's the result when you print this prior to returning: print (isset($to[$key]) ? $to[$key] : $default); ?
    no error came out... by the way below is the code..

    PHP Code:
    <?php
        
    function emailswitch$key ) {
            
    $to = array(
                
    'Pedicure Price Inquiry' => 'info@gmail.com',
                
    'Manicure Price Inquiry' => 'info@gmail.com',
                
    'Booking Time' => 'enquiry@gmail.com'
            
    );
            
    $default 'enquiry@gmail.com';
            return print (isset(
    $to[$key]) ? $to[$key] : $default);  
        }
      
    $from_name $_POST['name'];
      
    $email $_POST['email'];
      
    $subject $_POST['subject']; 
        
        
    // collect data
        
    $body "";
        foreach(
    $_POST as $key => $val)
        {
            if(
    $key != 'captcha')
                
    $body .= ucfirst($key).": ".$val."\r\n";
        }
        
        
    // construct MIME PLAIN Email headers
        
    $header "MIME-Version: 1.0\n";
        
    $header .= "Content-type: text/plain; charset=utf-8\n";
        
    $header .= "From: $from_name <$email> \r\nReply-To: $from_name <$email> \r\nReturn-Path: <$email> \r\n";
                    
        
    // send email
        
    $to emailswitch$subject );
        
    $mail_sent mail($to$subject$body$header);
    ?>

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That's not what I meant to do. This is what I meant:
    PHP Code:
    print (isset($to[$key]) ? $to[$key] : $default); 
    return (isset(
    $to[$key]) ? $to[$key] : $default); 
    And post the results of the print.

    Post the HTML form in use as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Jan 2010
    Posts
    127
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's not what I meant to do. This is what I meant:
    PHP Code:
    print (isset($to[$key]) ? $to[$key] : $default); 
    return (isset(
    $to[$key]) ? $to[$key] : $default); 
    And post the results of the print.

    Post the HTML form in use as well.
    my mistake i wrote the wrong code... now everything works like charm! thanks sifu!

    learnt a new thing again


  •  

    Tags for this Thread

    Posting Permissions

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