简体   繁体   English

Ajax&Php发布帖子

[英]Ajax & Php post issue

Hello i was looking to write a script for sending information to PHP via ajax but i think i had some errors hence i would like some help. 您好我想写一个脚本通过ajax向PHP发送信息,但我想我有一些错误因此我想要一些帮助。

AJAX 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>

^ This is the ajax part of the script in the above script the information would be sent via POST and it would be in json format ^这是上述脚本中脚本的ajax部分,信息将通过POST发送,它将采用json格式

raffle.php raffle.php

 <?php
$data =$_POST['rafBut'];
echo $data;
?>

Hence i just want the information to be shown in JSON format so i can easily encorprate with php. 因此我只想要以JSON格式显示信息,以便我可以轻松地使用php进行填充。 and is there any other way to do this (Just using php without ajax) here is an live example of this - http://admin.tftrg.com/Prototype/raffle.html but it doesnt seem to work Thanks in advance 有没有其他方法来做到这一点(只使用PHP没有ajax)这是一个现实的例子 - http://admin.tftrg.com/Prototype/raffle.html但它似乎没有工作提前谢谢

Try this: 尝试这个:

<?php
$json = array();
$json["data"] = array("status" => $_POST['rafBut']);
echo json_encode($json);
?>

Also change dataType: "json" to dataType: "post" 同时将dataType: "json"更改为dataType: "post"

You should replace localhost in url: "http:// localhost /raffle.php" to admin.tftrg.com or just remove it (so you get url: "/raffle.php" ). 您应该将url: "http:// localhost /raffle.php" localhost url: "http:// localhost /raffle.php" admin.tftrg.comadmin.tftrg.com或者将其删除(以便获取url: "/raffle.php" )。

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);

If you use debug tools such as Firebug on Firefox, you will see that your call is correctly sent. 如果您在Firefox上使用Firebug等调试工具,您将看到您的呼叫已正确发送。 The only thing to change seems to be the beginning of your URLs, replacing localhost by your real context such as http://admin.tftrg.com 唯一要改变的事情似乎是您的URL的开头,用您的真实上下文替换localhost,例如http://admin.tftrg.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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