您好,我想编写一个脚本通过 ajax 向 PHP 发送信息,但我认为我遇到了一些错误,因此我需要一些帮助。
AJAX
<script type="text/javascript">
$(document).ready(function(){
$(".infitem").offset({ top: 0, left: 0 });
$(".item").hover(function(){
var op = $(this);
var pos = $(op).offset();
var height = $(op).height();
var width = $(op).width();
$(".infname").html(op.attr("iname"));
$(".infdesc").html("Level: "+op.attr("ilevel")+((op.attr("iu1") != "") ? "<br />#"+op.attr("iu1") : ""));
if(op.attr("ilevel") != "")
$(".infitem").show();
$(".infitem").css({ "left": pos.left + (width / 2) - ($(".infdesc").width() / 2) + "px", "top": pos.top + height + "px" });
},function(){
$(".infitem").hide();
});
$(".item").click(function(){
if($(this).attr("id") == "notselected"){
$(this).appendTo("#selitems");
$(this).attr("id", "selected");
}else if($(this).attr("id") == "selected"){
$(this).appendTo("#allitems");
$(this).attr("id", "notselected");
}else if($(this).attr("id") == "gamenotselected"){
$(this).appendTo("#selitems");
$(this).attr("id", "gameselected");
}else if($(this).attr("id") == "gameselected"){
$(this).appendTo("#allgames");
$(this).attr("id", "gamenotselected");
}
});
$("#rafBut").click(function(){
$(this).attr("disabled", "disabled");
$(this).attr("value", "Please wait...");
var itms = new Array();
var gmes = new Array();
var ia = 0;
var ga = 0;
$(".item").each(function(i){
if($(this).attr("id") == "selected"){
itms[ia] = $(this).attr("iid")+":"+$(this).attr("iqual")+":"+$(this).attr("ilevel")+":"+$(this).attr("iu1");
ia++;
}
if($(this).attr("id") == "gameselected"){
gmes[ga] = $(this).attr("iid");
ga++;
}
});
$.ajax({
type: "post",
url: "http://localhost/raffle.php",
dataType: "json",
data: {
"postraffle": "true",
"title": $("#rtitle").val(),
"message": $("#mess").val(),
"maxentry": $("#maxentry").val(),
"duration": $("#durr").val(),
"filter": $("#reffil").val(),
"split": $("input[name=split]:checked").val(),
"pub": $("input[name=rafflepub]:checked").val(),
"stype": $("input[name=stype]:checked").val(),
"invo": $("input[name=invo]:checked").val(),
"items[]": itms,
"games[]": gmes,
},
success: function(data){
if(data.status == "fail")
{
alert(data.message);
$("#rafBut").removeAttr("disabled");
$("#rafBut").attr("value", "Raffle it!");
}
else if(data.status == "ok")
{
window.location.href = "http://localhost/raffle.php";
}
}
});
});
});
</script>
^ 这是脚本的 ajax 部分 在上面的脚本中,信息将通过 POST 发送,并且采用 json 格式
raffle.php
<?php
$data =$_POST['rafBut'];
echo $data;
?>
因此,我只想以 JSON 格式显示信息,以便我可以轻松地与 php 合并。 有没有其他方法可以做到这一点(只需使用 php 而不使用 ajax) 这是一个活生生的例子 - http://admin.tftrg.com/Prototype/raffle.html 但似乎不起作用 提前致谢
最佳答案
试试这个:
<?php
$json = array();
$json["data"] = array("status" => $_POST['rafBut']);
echo json_encode($json);
?>
同时将 dataType: "json"
更改为 dataType: "post"
关于javascript - Ajax 和 Php 帖子问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21085944/