admin管理员组文章数量:1401914
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
2 Answers
Reset to default 6Change 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
本文标签: javascriptHow to submit attr value and form values with POST using JQuery and PHPStack Overflow
版权声明:本文标题:javascript - How to submit attr value and form values with POST using JQuery and PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744340713a2601458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论