5

So I have a form with an input box and an 3 of a type of div that highlights when clicked. I'm trying to get it to submit the attribute and "#email" value and email it to me via PHP.

Here's my HTML:

<form id="form" name="form" class="custompurchase" action="custom.php" method="post">
    <input type="email" id="email" name="email" maxlength="40" class="textbox2" placeholder="Email address" required/>
    <a class="customoption storage" onclick="activate(this, 'storage');" data-name="500GB HDD">
        ...
    </a>
    <a class="customoption storage" onclick="activate(this, 'storage');" data-name="1TB HDD">
        ...
    </a>
    <a class="customoption storage" onclick="activate(this, 'storage');" data-name="2TB HDD">
        ...
    </a>
</form>

Here's my JQuery:

$(function() {
    var form = $('#form');
    $(form).submit(function(event) {
        event.preventDefault();
        var email = $("#email").val();
        var storage = $(".customoptionactive.storage").attr("data-name");
        $.ajax({
            type: "POST",
            url: $(form).attr("action"),
            data: email, storage
        })
        .done(function(response) {
            ...
        });
    });
});

And finally my PHP:

<?php
    $emailto = "[email protected]";
    $subject = "Custom PC";
    $email = $_POST['email'];
    $storage = $_POST['storage'];
    $entire = "Email: ".$email."\n"."Storage: ".$storage;
    mail($emailto, $subject, $entire);
?>

However, when I see the email, this is all I get:

Email:
Storage:

What am I doing wrong? Do I need to set the dataType? Thank you so much for your answers!

Edit: Different from similar question because it was a simple syntax error.

1

2 Answers 2

6

Change your data option on your $.ajax

 data: email, storage

to

 data: {email:email, storage:storage}
Sign up to request clarification or add additional context in comments.

6 Comments

You, sir, are a life saver. I've spent HOURS on this and could not figure it out. I'll accept as best answer as soon as I can :D
@KyleHorkley Glad i helped. :)
if you don't mind, could you explain how this fixed it? I understand with the curly brackets, but what is the ":" doing to help?
@KyleHorkley since on your php your accessing it through $_POST['email'].. you should also name the email value with email. converting it to an input on a normal post submition it should look like <input type="text" name="email" value="" />
normally you could also do data : $('Your_form').serialize()
|
1

You may want to use FormData and append custom data to it, as manta pointed it out here: Send FormData and String Data Together Through JQuery AJAX?

var formData = new FormData();
formData.append('storage', $(".customoptionactive.storage").attr("data-name"));

$.ajax({
    type: "POST",
    url: $(form).attr("action"),
    data: formData
})
.done(function(response) {
    ...
});

this will save you time adding those input values manually

Comments

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.