Meagan Frost
Meagan Frost

Reputation: 9

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

            <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

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

Upvotes: 0

Views: 135

Answers (3)

shtrih
shtrih

Reputation: 172

You should replace localhost in url: "http://localhost/raffle.php" to admin.tftrg.com or just remove it (so you get 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);

Upvotes: 1

Anthed
Anthed

Reputation: 149

If you use debug tools such as Firebug on Firefox, you will see that your call is correctly sent. 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

Upvotes: 0

Ali
Ali

Reputation: 5121

Try this:

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

Also change dataType: "json" to dataType: "post"

Upvotes: 1

Related Questions