javascript - How to submit attr value and form values with POST using JQuery and PHP? - Stack Overflow

So I have a form with an input box and an 3 of a type of div that highlights when clicked. I'm try

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.

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.

Share Improve this question edited Feb 15, 2016 at 0:59 Kyle Horkley asked Feb 15, 2016 at 0:43 Kyle HorkleyKyle Horkley 1,0211 gold badge11 silver badges24 bronze badges 1
  • Possible duplicate of Send FormData and String Data Together Through JQuery AJAX? – Kevin Kopf Commented Feb 15, 2016 at 0:48
Add a ment  | 

2 Answers 2

Reset to default 6

Change your data option on your $.ajax

 data: email, storage

to

 data: {email:email, storage:storage}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744340714a4569384.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信