Php Ajax表单提交表单问题。我能';无法获得正确的值
在下面的代码中,当我在调用ajax之前发出警报时,它会按预期执行,echo会输出正确的信息;然而,当我试图通过php传递信息时,数据似乎是空的。我在php中做错了什么 Javascript:Php Ajax表单提交表单问题。我能';无法获得正确的值,php,jquery,ajax,Php,Jquery,Ajax,在下面的代码中,当我在调用ajax之前发出警报时,它会按预期执行,echo会输出正确的信息;然而,当我试图通过php传递信息时,数据似乎是空的。我在php中做错了什么 Javascript: $('#submit_fourth').click(function(){ //send information to server var email = $('input#email').val(); var hash = $('input#username').val()
$('#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
$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);
?>
PHP:
$('#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
$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);
?>
这是错误的:
var finish = "email=" + email + "hash=" + hash + "type=" + type;
应至少:
var finish = "email=" + email + "&hash=" + hash + "&type=" + type;
但为了避免转义数据(您没有这样做…)时出现问题,最好使用:
var finish = $("form").serialize();
这是错误的:
var finish = "email=" + email + "hash=" + hash + "type=" + type;
应至少:
var finish = "email=" + email + "&hash=" + hash + "&type=" + type;
但为了避免转义数据(您没有这样做…)时出现问题,最好使用:
var finish = $("form").serialize();
应在普通对象中传递数据:
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!'); }
});
应在普通对象中传递数据:
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!'); }
});
您是否可以将finish作为查询字符串传递?
var\u dump($message)设置并粘贴结果后,是否可以将finish作为查询字符串传递?var\u dump($message)代码>设置并粘贴结果后。它是一个对象,不是数组。它是一个对象,不是数组