1

嗨,我有这段代码可以通过 AJAX 和文件 php 提交评论,而无需刷新页面。以前可以工作,但是自从我更换计算机来开发此应用程序后,我遇到了问题,并且代码从未更改过。现在它要么使 Chrome 崩溃,要么在未到达文件 comment.php 的情况下提交

这是JS代码

    <script src="/js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript"> 
    $("#formcomment").submit(function(){        
        ctask = this.task.value;
        cname = this.usrnm.value;
        cmessage = this.comment.value;
        submitter = this.submitter;
        cty = this.ty.value;
        celem = this.elem.value;
        cusrcd = this.usrcd.value;
        cusrcdauth = this.usrcdauth;
        val_recaptcha_challenge_field = this.recaptcha_challenge_field.value;
        val_recaptcha_response_field = this.recaptcha_response_field.value;

        if(cname=="" || cmessage=="") { $("#errAll").html('<p>Please don\'t leave the requierd fields.</p>');  }

        $.post("/comment.php", {task: ctask, usrnm: cname, comment: cmessage, ty: cty, elem: celem, recaptcha_challenge_field:val_recaptcha_challenge_field, recaptcha_response_field:val_recaptcha_response_field, usrcd: cusrcd, usrcdauth: cusrcdauth}, function(data){
            if(data=='0') { $("#errAll").html('<p>Please don\'t leave the requierd fields.</p>'); }
            else if (data=='1') {$("#errAll").html('<p>The reCAPTCHA wasn\'t entered correctly. Try it again.</p>'); }
            else if (data=='2') {$("#errAll").html('<p>Username must contain 6 to 20 characters.</p>'); }
            else { 
                submitter.value="Comment posted"; 
                submitter.disabled=true; 
                $('#listcom').prepend(data); 
                $("#errAll").html('');
                window.location.hash = "#comments";
            }
        });

        return false;
    });
   </script>

以及页面中的表单

   <form class="well" method="post" id="formcomment"> 
    <input type="hidden" id="ty" name="ty" value="stg" /> 
    <input type="hidden" id="elem" name="elem" value="13" /> 
    <input type="hidden" name="task" id="task" value="addComments" /> 
    <input type="hidden" name="usrcdauth" id="usrcdauth" value="yudtjnisifwndyubleedbtisngvxlj" /> 

    <label>Username</label> 
    <input value="" type="text" id="usrnm" name="usrnm" maxlength="20" class="span3" /> 
    <input type="hidden" id="usrcd" name="usrcd" value="" /> 

    <label>Message</label> 
    <textarea id="comment" name="comment" rows="5" cols="150" class="span7"></textarea> 


    <div style="margin: 15px 0;"><input type="submit" id="submitter" name="submitter" value="Add Comment" class="btn" /></div> 
    <div id="errAll"></div> 
   </form> 
4

1 回答 1

0

听起来可能很傻,但是您是否在新计算机上安装了像 xammp 这样的 php 服务器?如果是这样,请确保文件位于同一目录中,并尝试删除comment.php 之前的“/”。我没有详细介绍您的代码,但我认为因为它以前有效;可能是这种情况。

于 2012-06-23T07:52:35.320 回答