如何使用JQuery和PHP提交attr值和表单值?

时间:2022-04-13 09:37:08

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.

因此,我有一个带有输入框的表单和一个类型为3的div,单击时突出显示。我试图让它提交属性和“#email”值,并通过PHP将其发送给我。

Here's my HTML:

这是我的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:

这是我的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:

<?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 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.

编辑:不同于类似的问题,因为它是一个简单的语法错误。

2 个解决方案

#1


5  

Change your data option on your $.ajax

更改$.ajax上的数据选项

 data: email, storage

to

 data: {email:email, storage:storage}

#2


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?

您可能想要使用FormData并向它添加自定义数据,正如manta在这里指出的:通过JQuery AJAX将FormData和String数据一起发送?

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

这将节省您手动添加这些输入值的时间。

#1


5  

Change your data option on your $.ajax

更改$.ajax上的数据选项

 data: email, storage

to

 data: {email:email, storage:storage}

#2


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?

您可能想要使用FormData并向它添加自定义数据,正如manta在这里指出的:通过JQuery AJAX将FormData和String数据一起发送?

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

这将节省您手动添加这些输入值的时间。