[英]Ajax & Php post issue
您好我想寫一個腳本通過ajax向PHP發送信息,但我想我有一些錯誤因此我想要一些幫助。
<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格式
<?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"
您應該將url: "http:// localhost /raffle.php"
localhost
url: "http:// localhost /raffle.php"
admin.tftrg.com
為admin.tftrg.com
或者將其刪除(以便獲取url: "/raffle.php"
)。
raffle.php
<?php
// $_POST['rafBut'] - what is it? It is not passed via AJAX
// response fields
$data = array(
'status' => 'fail',
'message' => 'Failed to save data',
);
/* some operations with $_POST data */
echo json_encode($data);
如果您在Firefox上使用Firebug等調試工具,您將看到您的呼叫已正確發送。 唯一要改變的事情似乎是您的URL的開頭,用您的真實上下文替換localhost,例如http://admin.tftrg.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.