Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit attr value and form values with POST using JQuery and PHP?

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.

like image 668
Kyle Horkley Avatar asked Feb 15 '16 00:02

Kyle Horkley


1 Answers

Change your data option on your $.ajax

 data: email, storage

to

 data: {email:email, storage:storage}
like image 180
roullie Avatar answered Nov 01 '22 15:11

roullie