繁体   English   中英

Ajax&Php发布帖子

[英]Ajax & Php post issue

您好我想写一个脚本通过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"

您应该将url: "http:// localhost /raffle.php" localhost url: "http:// localhost /raffle.php" admin.tftrg.comadmin.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM