我有一个表单,它将表单字段值传递给 ajx 脚本,然后将这些值传递给 php,并将它们发送到电子邮件地址。
我遇到的问题是这些值没有显示,而是得到了“未定义”一词。
这是我的 ajax 代码。
$(document).ready(function(){
$('form.save').submit(function () {
var name = $(this).find('.name').attr('value');
var email = $(this).find('.email').attr('value');
var telephone = $(this).find('.telephone').attr('value');
// ...
$.ajax({
type: "POST",
url: "application.php",
data: "name="+ name +"& email="+ email +"& telephone="+ telephone,
success: function(){
$('form.save').hide(function(){$('div.success')});
}
});
return false;
});
});
这是我的表格
<form action="" method="post" class="save">
<input type="text" name="name" id="name" clsss="name" value="" placeholder="Name"/>
<input type="text" name="email" class="email" value="" placeholder="Email"/>
<input type="text" name="telephone" class="telephone" value=""placeholder="Telephone"/>
<input name="certified" type="checkbox" value="" checked>I am a Certified sophisticated Investor
<input type="hidden" name="type" value="Certified sophisticated Investor">
<input type="submit" name="submit" class="button" value="Submit"/>
</div>
</form>
然后是 php(php 代码没有任何卫生条件,我只是想获取值,然后使用更好的 php 代码)
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
// mail
$msg = "$name $email $telephone";
$msg = wordwrap($msg,70);
mail("*************","Application",$msg);
最佳答案
data: "name="+ name +"& email="+ email +"& telephone="+ telephone
删除空格
data: "name="+ name +"&email="+ email +"&telephone="+ telephone
关于javascript - 通过 Ajax 发送时 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113727/