阻止Ajax多次发送

时间:2016-04-05 16:51:44

标签: javascript php jquery ajax

我正在使用这个脚本: https://jsfiddle.net/b7wjj39p/9/ 它随机运行12个轮子,然后将结果通过AJAX发送给php,后者打印出来。

问题是AJAX被发送12次而不是只发送一次。

这是发送数据的相关代码:

function sendwinnertophp(){
    var winner = $("#winner").html();
    var winner_two = $("#winner222").html();
    var winner_three = $("#winner333").html();
    var winner_four = $("#winner444").html();
    var winner_five = $("#winner555").html();
    var winner_six = $("#winner666").html();
    var winner_seven = $("#winner777").html();
    var winner_ehight = $("#winner888").html();
    var winner_nine = $("#winner999").html();
    var winner_ten = $("#winner10").html();
    var winner_eleven = $("#winner11").html();
    var winner_twelve = $("#winner12").html();
    $.ajax({
        type: "POST",
        url: "getwinner.php",
        data: {
            "winner": winner,
            "winner_two" : winner_two,
            "winner_three" : winner_three,
            "winner_four" : winner_four,
            "winner_five" : winner_five,
            "winner_six" : winner_six,
            "winner_seven" : winner_seven,
            "winner_ehight" : winner_ehight,
            "winner_nine" : winner_nine,
            "winner_ten" : winner_ten,
            "winner_eleven" : winner_eleven,
            "winner_twelve" : winner_twelve,
        },
        cache: false,
        success: function(data){ 
        //  alert(data);
        },
        error: function(err){
            alert(err);
        }
    });
}

这是将结果打印到html文件的PHP代码:

$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
$winner_two = $_POST['winner_two'];
$winner_three = $_POST['winner_three'];
$winner_four = $_POST['winner_four'];
$winner_five = $_POST['winner_five'];
$winner_six = $_POST['winner_six'];
$winner_seven = $_POST['winner_seven'];
$winner_ehight = $_POST['winner_ehight'];
$winner_nine = $_POST['winner_nine'];
$winner_ten = $_POST['winner_ten'];
$winner_eleven = $_POST['winner_eleven'];
$winner_twelve = $_POST['winner_twelve'];

//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>first:</b> $winner<br/><b>second:</b> $winner_two<br/><b>third:</b> $winner_three<br/><b>forth:</b> $winner_four<br/><b>fifth:</b> $winner_five<br/>
<b>sixth:</b> $winner_six<br/><b>seventh:</b> $winner_seven<br/><b>ehihth:</b> $winner_ehight<br/><b>ninth:</b> $winner_nine<br/><b>tenth:</b> $winner_ten<br/><b>eleventh:</b> $winner_eleven<br/><b>twelvth:</b> $winner_twelve<br/><hr/>" );
fclose( $file )

0 个答案:

没有答案