dqch34769 2015-05-09 12:28
浏览 57

让PHP通过AJAX更改文本文件而不刷新页面

I'm developing a simple site for Year 12 IT, and what it needs to do is sell coffees (or at least give the appearance, we aren't actually selling anything). When orders are submitted, they get converted to PHP variables and then laid down as a brief order summary; a summary of that order gets posted to a text file. All good so far.

The problems come in when I try to clear the text file/"confirm the orders" (post the file contents to a log and then clear the file itself). Using pure PHP (writing both as submit buttons, checking for isset(), and then using file_get_contents()) works perfectly, but forces a page request that also resubmits the most recent order. Using mixed AJAX/PHP lets me create nice little notifications for cleared/confirmed orders and is supposed to refresh specific page areas and avoid resubmits, but for some reason full refreshes and resubmits still happen. Why?

PHP:

<?php

$txtManip = $_REQUEST['txtFunk'];

if($txtManip == "clear"){
    file_put_contents('orders.txt', '');
}

if($txtManip == "confirm"){
    $n = 1;
    $custNum = $n++;

    $confirmedOrders = "Customer " . $custNum . " " . "ordered " . file_get_contents('orders.txt') . "
";
    file_put_contents('log.txt', $confirmedOrders, FILE_APPEND | LOCK_EX);
    file_put_contents('orders.txt', '');
}
?>

Javascript:

clearButton.addEventListener('click', notifyClear);
confirmButton.addEventListener('click', notifyConfirm);

function orderCleared(){
    orders.style.display = "block";
    orderScrollText.innerHTML = "Order another coffee";
    orderShifter.removeEventListener('click', orderDisp);
    orderShifter.addEventListener('click', orderHide);
}

function notifyClear(){     
    ajaxTime.onreadystatechange = function(){
        if (ajaxTime.readyState == 4 && ajaxTime.status == 200){
            notification.style.display = "flex";

            setTimeout(function(){
                    notifyBox.style.display = "none";
            }, 4000);

            notifyText.innerHTML = "Cleared!";
        }
    }
    ajaxTime.open("POST", "notepadControl.php", true);
    ajaxTime.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    ajaxTime.send("txtFunk=clear");

    /*$.ajax({
        url: "notepadControl.php",
        data: clear,
        type: "post",
        success: function(output){
            alert (output);
        }
    })*/

    setTimeout(function(){
        orderCleared();
        window.location = "http://heptane.smgx.co#subbedOrders";
        location.reload();
    }, 5000);
}

function notifyConfirm(){   
    ajaxTime.onreadystatechange = function(){
        if (ajaxTime.readyState == 4 && ajaxTime.status == 200){
            notification.style.display = "flex";

            setTimeout(function(){
                    notifyBox.style.display = "none";
            }, 4000);

            notifyText.innerHTML = "Confirmed Orders";
        }
    }
    ajaxTime.open("POST", "notepadControl.php", true);
    ajaxTime.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    ajaxTime.send("txtFunk=confirm");

    setTimeout(function(){
        orderDisp();
        window.location = "http://heptane.smgx.co#subbedOrders";
    }, 5000);
}

HTML:

<div id="showOrders">
    <p class="whiteText" id="orderScroller">See Orders</p>
</div>

<div id="subbedOrders">
    <iframe id="orderList" src="orders.txt" ></iframe>
    <form method="post">
        <input type="button" id="clearOrders" method="post" name="clear" value="Clear Orders" />
        <input type="button" id="orderConfirm" method="post" name="confirm" value="Confirm Orders" />
    </form>

    <div id="notifyBox">
        <p id="notifyText" class="whiteText"></p>
    </div>
</div>

<script src="milk.js"></script>

In other words, I want something that will empty my text file without resubmitting any data or refreshing the whole page :). JQuery and your pet libray are all fine, so long as the final solution works. Obviously I'm using AJAX, since it's supposed to be the only way to do this. More secure things are preferred, so anything that keeps most of the work server-side will make me happy. Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 @microsoft/fetch-event-source 流式响应问题
    • ¥15 ogg dd trandata 报错
    • ¥15 高缺失率数据如何选择填充方式
    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False