所以我有一个带有输入框的表单和一个<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>
类型的表单,在单击时突出显示。我试图让它提交属性和#34; #mail&#34;价值并通过PHP发送给我。
这是我的HTML:
$(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) {
...
});
});
});
这是我的JQuery:
<?php
$emailto = "purchases@prismpc.net";
$subject = "Custom PC";
$email = $_POST['email'];
$storage = $_POST['storage'];
$entire = "Email: ".$email."\n"."Storage: ".$storage;
mail($emailto, $subject, $entire);
?>
最后我的PHP:
Email:
Storage:
然而,当我看到电子邮件时,这就是我得到的:
dataType
我做错了什么?我需要设置<ion-view hide-nav-bar="true">
吗?非常感谢你的回答!
编辑:与类似问题不同,因为它是一个简单的语法错误。
答案 0 :(得分:5)
更改data
$.ajax
选项
data: email, storage
到
data: {email:email, storage:storage}
答案 1 :(得分:1)
您可能希望使用FormData
并向其添加自定义数据,正如manta在此处指出的那样: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) {
...
});
这将节省您手动添加这些输入值的时间