在下面的代码中,当我在我的ajax调用之前alert
时,它按预期执行并回显出正确的信息;但是,当我尝试通过php传递信息时,数据似乎是空的。我在php中做错了什么?
的Javascript :
$('#submit_fourth').click(function(){
//send information to server
var email = $('input#email').val();
var hash = $('input#username').val();
var type = $('input#type').val();
var finish = "email=" + email + "hash=" + hash + "type=" + type;
alert(finish);
$.ajax({
cache: false,
type: 'POST',
url: 'process.php',
data: finish,
success: function(response) {
alert('it worked!');
}
});
});
PHP :
<?php
$to = 'blanet910@yahoo.com';
$subject = 'Hash testing requested';
$message = $_POST['finish'];
$headers = 'From: webmaster@hashtester.com' . "\r\n" .
'Reply-To: webmaster@hashtester.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
答案 0 :(得分:3)
这是错误的:
var finish = "email=" + email + "hash=" + hash + "type=" + type;
至少应该是:
var finish = "email=" + email + "&hash=" + hash + "&type=" + type;
但是为了避免数据转义问题(你没有做的......),最好使用:
var finish = $("form").serialize();
答案 1 :(得分:1)
您应该在普通对象中传递数据:
var email = $('input#email').val();
var hash = $('input#username').val();
var type = $('input#type').val();
$.ajax({
cache: false,
type: 'POST',
url: 'process.php',
data: {email: email, hash: hash, type: type},
success: function(response) { alert('it worked!'); }
});