How to send attr values ​​and form values ​​using POST using jQuery and PHP?

So, I have a form with an input field and 3 types divthat are highlighted when clicked. I am trying to get it to send the attribute and the value "#email" and send it to me in PHP.

Here is 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 is 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 = "purchases@prismpc.net";
    $subject = "Custom PC";
    $email = $_POST['email'];
    $storage = $_POST['storage'];
    $entire = "Email: ".$email."\n"."Storage: ".$storage;
    mail($emailto, $subject, $entire);
?>

However, when I see an email, that’s all I get:

Email:
Storage:

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

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

+4
source share
2 answers

data $.ajax

 data: email, storage

 data: {email:email, storage:storage}
+5

, FormData , manta : FormData String Data Together 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) {
    ...
});

,

+1

Source: https://habr.com/ru/post/1628759/


All Articles