开发者

Best practice PHP Form Action

开发者 https://www.devze.com 2022-12-26 02:27 出处:网络
Hi there i\'ve built a new script (from scratch not a CMS) and i\'ve done alot of work on reducing memory usage and the time it takes for the page to be displayed (caching HTML etc)

Hi there i've built a new script (from scratch not a CMS) and i've done alot of work on reducing memory usage and the time it takes for the page to be displayed (caching HTML etc)

There's one thing that i'm not sure about though. Take a simple example of an article with a comments section.

If the comment form posts to another page that then redirects back to the article page I won't have the problem of people clicking refresh and resending the information.

However if I do it that way, I have to load up my script twice use twice as much memory and it takes twice as long whilst i'm still only displaying the page once.

Here's an example from my load log. The first load of the article is from the cache, the second rebuilds the page after the comment is posted.


Example 1

0 queries using 650856 bytes of memory in 0.018667 - domain.com/article/1/my_article.html

9 queries using 1325723 bytes of memory in 0.075825 - domain.com/article/1/my_article/newcomment.html

0 queries using 650856 bytes of memory in 0.029449 - domain.com/article/1/my_article.html

Example 2

0 queries using 650856 bytes of memory in 0.023526 - domain.com/article/1/my_article.html

9 queries using 1659096 bytes of memory in 0.060032 - domain.com/article/1/my_article.html


Obviously the time fluctuates so you can't really compare that. But as you can see with the first method I use more memory and it takes longer to load.

BUT the first method avoides the refresh problem.

Does anyone have any suggestions for the best approach or for alternative ways开发者_开发技巧 to avoid the extra load (admittadely minimal but i'd still like to avoid it) whilst also avoiding the refresh problem?


Optimization efforts are always commendable, but in this specific case I see zero benefit in saving a few queries, and some RAM, for a few seconds. Definitely go with example 1 (or whatever is best from a programming, maintenance and user experience perspective).

I don't know about your server setup and all that so judging run times and memory consumption are always subjective, but you seem to be doing pretty well. Look at Wordpress where the average script instance (at least in the backend, haven't measured the frontend) weighs between 6 and 12 MB. Don't worry too much about optimization.


I consider your problem more imagined than real. The wheels add some weight to the car overall weight. Would it be wise to remove the wheels? I don't think so. According to the standard you ought to make redirect. So, if you do it this way, you have no other choice.

The other possible solution is to use AJAX to send a comment, as SO does. This would save you that precious reload :)


Use AJAX! Check the love letters form in the footer here: http://www.flatmaterooms.co.uk

HTML

<form id="feedback" method="post" action="#">
    <fieldset>
        <h3>Have comments? Feedback? <span id="feedback_status"></span></h3>
    <textarea name="feedback_body" id="feedback_body" class="placeholder" rows="10" cols="50">Send us your comment...</textarea>

    <div id="feedback_submit">
        <button type="submit">Send &rarr;</button>

        <div id="feedback_optional">
            <label for="feedback_email">Your email address (optional)</label>
            <input type="text" name="feedback_email" id="feedback_email" />
        </div>

        <input type="hidden" name="feedback_url" id="feedback_url" value="<?=$_SERVER['REQUEST_URI']?>" />
    </div>
    </fieldset>
</form>

JQuery

$(document).ready(function(){   function feedback() {
    var a = $("#feedback_body").val();
    if(a) {
        $("#feedback_status").show();
        $("#feedback_status").html("Sending...");
        $("#feedback_submit button").attr("disabled", "disabled").addClass("disabled").removeClass("default").blur();
        $.ajax( {
            type : "POST", url : "/feedback.php", data : {
                feedback_body  : a,
                feedback_email : $("#feedback_email").val(),
                feedback_url   : $("#feedback_url").val(),
                feedback_save  : true
            }
            , error : function(b, d, c) {
               $("#feedback_status").html("Whoops!")}
            , success : function(b, c) {
               $("#feedback_status").html("Success! Thanks.")}
            }
    )}
}

$(function() {
    var b = $("#feedback_body");
    var a = b.val();
    b.click(function() {
        if($(this).val() == a) {
           $(this).removeClass("placeholder").val("");
           $("#feedback_submit").show()}
        }
    );
    $("form#feedback").submit(function() {
       feedback(); return false}
    )}
); 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号