1

I am trying to append the selected html item on the form to be sent along with the email. I would like to know how to correctly fetch the value from the select list and pass it to PHP. I have not been able to correctly figure it out. I would appreciate any assistance. Below is my code...

    function _(id){ return document.getElementById(id); }
function submitForm(){
    _("contact-submit").disabled = true;
    _("status").innerHTML = 'please wait ...';
    var formdata = new FormData();
    formdata.append( "n", _("n").value );
    formdata.append( "e", _("e").value );
    formdata.append( "t", _("t").value );
    formdata.append( "w", _("w").value );
    formdata.append( "m", _("m").value );

    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "contact.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("contact-form").innerHTML = '<h2 class="tnx">Thanks '+_("n").value+', we will be in touch soon!</h2>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("contact-submit").disabled = false;
            }
        }
    }
    ajax.send( formdata );
}

HTML

    <div class="cat-select">
                    <label class="cat-selects" for="subject"><span>Area of Interest</span>
                            <select name="subject" class="select-field" id="s">
                            <option value="General Inquiry">General Inquiry</option>
                            <option value="Elderly Care">Elderly Care</option>
                            <option value="Child Care">Child Care</option>
                            </select>
                        </label>
                </div>

PHP

     <?php
    if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['t']) && isset($_POST['w']) && isset($_POST['m']) ){
        $n = $_POST['n']; // HINT: use preg_replace() to filter the data
        $e = $_POST['e'];
        $t = $_POST['t'];
        $w = $_POST['w'];

        $m = nl2br($_POST['m']);
        $to = "hello@gmail.com";    
        $from = $e;
        $subject = 'Contact Form Message';
        $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$s.' '.$m.'</p>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        if( mail($to, $subject, $message, $headers) ){
            echo "success";
        } else {
            echo "The server failed to send the message. Please try again later.";
        }
    }
    ?>
Ibo Chief
  • 39
  • 5
  • Not really sure what your issue is or what you're trying to do. I don't see your code referencing the select box anywhere? – M. Eriksson Feb 20 '17 at 18:50
  • I would like to know how to correctly add the select box in the php and Javascript file @MagnusEriksson – Ibo Chief Feb 20 '17 at 18:52
  • Still not clear what you mean by "add the select box". Tried copy/paste? Or are you talking about getting the selected value and pass it to your PHP code? You _really_ need to rewrite your question and clarify what you're trying to do, what the actual issue is and what you have tried so far. – M. Eriksson Feb 20 '17 at 18:54
  • Yea I probably didn't write my question well. I would like to know how to correctly fetch the value from the select list and pass it to PHP. @MagnusEriksson – Ibo Chief Feb 20 '17 at 18:57
  • You're already passing other form elements. Have you tried doing it the same way? – M. Eriksson Feb 20 '17 at 18:58
  • Yes, I was successful with the other elements. But the the select list item doesn't seem to work. @MagnusEriksson – Ibo Chief Feb 20 '17 at 19:02
  • Possible duplicate: [Get selected value in dropdown list using JavaScript?](http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – M. Eriksson Feb 20 '17 at 19:10
  • Thanks! I got it working now @MagnusEriksson – Ibo Chief Feb 20 '17 at 23:48

1 Answers1

0

The id of your <select> element is "s" but your Javascript isn't referencing it anywhere. Assuming your _() function refers to the id, you will need to add the following line with the similar lines.

formdata.append( "s", _("s").value );

This should make $_POST['s'] available for you to use on the PHP side.

Remember to do something to escape all the fields sent through this form, not just the text inputs and textareas, to avoid any attempts to exploit the site or the recipient of the email.

Gwellin
  • 484
  • 1
  • 4
  • 8