sanch
sanch

Reputation: 716

Select tag value not being included in my Ajax script

I have an ajax script that sends data from a contact form to a php script but the problem is that I am not able to get the value from the "select" tag. I am limited in my knowledge of javascript/ajax so please excuse any inappropriate descriptions or phrases as I really can only explain it how I know it.

HTML :

<form action="" method="POST" id="contact">
    <table>
        <tbody>
            <tr>
                <td><h2>First Name: </h2></td>
                <td><h2>Last Name: </td>
                <td><h2>Email Address: </td>
            </tr>
            <tr>
                <td><input type="text" name="first_name"></td>
                <td><input type="text" name="last_name"></td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td><h2>Street Address:</h2></td>
                <td><h2>What's Dirty?</h2></td>
            </tr>
            <tr>
                <td><input type="text" name="address"></td>
                <td>
                    <select name="job" form="contact">
                        <option value="house">House</option>
                        <option value="roof">Roof</option>
                        <option value="garage-shed">Garage/shed</option>
                        <option value="other">Other</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><h2>Message: </h2></td>
            </tr>
        </tbody>
    </table>
    <textarea name="message" cols="80" rows="5"></textarea>

    <input type="submit" id="submit" name="send" value="Send!" class="send-button">

</form>

Ajax

    <script type="text/javascript">
    $("#submit").click(function(e) {
        e.preventDefault();
var data_string = $("form#contact").serializeArray();
$.ajax({
    type: "POST",
    url: "database.php",
    data: data_string,
    success: function(){
    }
});
return false;
    });
    </script>

PHP

  print_r($_POST);
  $fname = $_POST['first_name'];
  $lname = $_POST['last_name'];
  $email = $_POST['email'];
  $address = $_POST['address'];
  $job = $_POST['job']; 
  $message = $_POST['message']; 

What I get from the script is this

[first_name] => John
[last_name] => Smith
[email] => [email protected]
[address] => 1234 Jamestown Rd.

Please keep in mind I am very new to this so a simple mistake really isn't all that obvious to me.

Upvotes: 0

Views: 490

Answers (1)

sanch
sanch

Reputation: 716

I have found the problem to be in a mysterious attribute inside my html.

HTML

<select name="job" form="contact">

was the monkey wrench inside of my code, I removed

form="contact">

and all was working just fine!

Thanks for the help!

Upvotes: 1

Related Questions