“请求页面太大”,重构的最佳方式是什么?

时间:2013-11-20 00:32:35

标签: javascript php ajax refactoring

以下代码应检查字符串中是否找到#或@符号。正则表达式应该找到每个@或#(类似于Twitter的那种)并且应该将它找到的每个实例放在messages表中(如果它是@符号),或者如果它是#符号它应该将实例插入hashtags表中,或者如果主题标签已经在表中,则更新现有记录 目前,脚本本身工作正常,但是当通过AJAX使用javascript代码时,控制台会通过说请求的实体(此脚本)太大(或某种口径)来响应。我认为它陷入了无限循环,但到目前为止,我还没有找到一种(工作)更好的方法来做到这一点。那么,更好的编码方式是什么?

if (preg_match_all("/[@]+[A-Za-z0-9-_]+/i", $post, $matches)) {
        for ($i = 0;$i <= $matches;$i++) {
            $match = str_replace("@", "", $matches[$i]);
            foreach($match as $key=>$mVal) {
                $uMSQL = "INSERT INTO `messages` (`to`, `from`, `message`, `sent`) VALUES (:to, :from, '<p>tagged you in a post</p>', NOW())";
                $uMQ = $con->prepare($uMSQL);
                $uMQ->bindParam(':from', $author, PDO::PARAM_STR);
                $uMQ->bindParam(':to', $mVal, PDO::PARAM_STR);
                $uMQ->execute();
            }
        }
    }
    if (preg_match_all("/[#]+[A-Za-z0-9-_]+/i", $post, $hashtags)) {
        for ($h = 0; $h <= $hashtags; $h++) {
            $htMatched = $hashtags[$h];
            foreach($htMatched as $key=>$htVal) {
                $htCheck = "SELECT COUNT(hashtag) FROM `hashtags` WHERE `hashtag` = '$htVal'";
                $htQ = $con->query($htCheck);
                $htExistence = $htQ->fetchColumn();
                if ($htExistence >= 1) {
                    $addTU = "UPDATE `hashtags` SET `used` = used+1 WHERE `hashtag` = '$htVal'";
                    $updateHT = $con->exec($addTU);
                } else {
                    $htMSQL = "INSERT INTO `hashtags` (`hashtag`) VALUES (:hashtag)";
                    $htMQ = $con->prepare($htMSQL);
                    $htMQ->bindParam(':hashtag', $htVal, PDO::PARAM_STR);
                    $htMQ->execute();
                }
            }
        }
    }

AJAX

 function sendData() {
    var hr = new XMLHttpRequest();
    var url = "http://localhost/NextIt/ajax/sendPost.php";
    var txtField = window.frames['richTextField'].document.body.innerHTML;
    var access = document.getElementById('postTo').selectedIndex;
    var acc = document.getElementById('postTo').options;
    var accss = acc[access].text;
    var vars = "post="+txtField+"&access="+accss;
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
    if (hr.readyState === 4 && hr.status === 200) {
        var return_data = hr.responseText;
        $('status').innerHTML = return_data;
        window.frames['richTextField'].document.body.innerHTML = '';
    }
}
    hr.send(vars);
    $('status').innerHTML = "Posting...";
}

post_max_size是8M。

0 个答案:

没有答案