我不知道为什么这不起作用。我试图通过ajax传递数据。我经常使用这个,但由于某种原因,它不起作用。它没有返回。
这是js
$('#contactformbtn').click(function(){
var fullname = $('#fullname').val();
var youremail = $('#youremail').val();
var subject = $('#subject').val();
var yourmessage = $('#yourmessage').val();
var datastring = 'fullname=' + fullname + '&youremail=' + youremail + '&subject=' + subject + '&yourmessage=' + yourmessage;
$.ajax({
type: "POST",
url: "ajax-contact.php",
data: datastring,
success: function(status){
alert(status);
}
});
//alert(datastring);
return false;
});
这是php
<?php
require 'core/init.php';
if(isset($_POST)){
$fullname = $_POST['fullname'];
$youremail = $_POST['youremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
echo $fullname;
}
?>
在chrome中检查控制台并且我得到2个错误
Uncaught TypeError: Cannot read property '1' of null
Uncaught TypeError: Cannot read property '3' of null
我的完整js就是这个,(只是错误不是联系功能)
$(document).ready(function(){
$("#download-btn").bind("click", downloadfile);
$(".download-link").hide();
function downloadfile()
{
var dl = $("#dl").val();
var counter = 10;
var interval = setInterval(function(e) {
counter--;
if(counter > 0){
$("#download-btn").html("Your Download will begin in " + counter + " Seconds");
$("#download-btn").attr("disabled", "disabled");
} else {
window.location.href="http://www.forwardfiles.com/get_file.php?i="+dl;
$("#download-btn").hide();
$(".download-status").html("<h3 class='center'>Download is complete</h3>");
clearInterval(interval);
}
}, 1000);
}
$('#contactformbtn').click(function(){
var fullname = $('#fullname').val();
var youremail = $('#youremail').val();
var subject = $('#subject').val();
var yourmessage = $('#yourmessage').val();
var datastring = 'fullname=' + fullname + '&youremail=' + youremail + '&subject=' + subject + '&yourmessage=' + yourmessage;
$.ajax({
type: "POST",
url: "ajax-contact.php",
data: datastring,
success: function(status){
alert(status);
}
});
//alert(datastring);
return false;
});
});
答案 0 :(得分:0)
您是否尝试过.serialize?
$('#contactformbtn').click(function(){
$.ajax({
type: "POST",
url: "ajax-contact.php",
data: $("form").serialize(),
success: function(status){
alert(status);
}
});
//alert(datastring);
return false;
});
答案 1 :(得分:0)
检查您的HTML代码是否为这些值
var fullname = $('#fullname').val();
var youremail = $('#youremail').val();
var subject = $('#subject').val();
var yourmessage = $('#yourmessage').val();
已经定义了他们的ID而不仅仅是“名称”标签
答案 2 :(得分:0)
我得到了它,我的代码很好。问题是我没有运行PHP版本5.3,所以我更新它和它的工作:D。无论如何,谢谢你的帮助