PHP和javascript提交表单更改表单名称?

时间:2013-06-01 21:08:33

标签: php javascript jquery

您好,我正在研究一个php java submithing评论系统,现在我有一个问题,我不能单独解决。 我只是想问我怎么做这个

$('#addCommentForm'+x).submit(function(e){

DO something here
}

addCommentForm之后有很多数字:addCommentForm1 addCommentForm2 addCommentForm3 addCommentForm4 and rising。那么我怎样才能让这个改变名称的名字在java中进行子制作?

var x = $_POST['comentonpost'];

$ _POST来自这里:

<script type='text/javascript'> var $_POST = <?php echo !empty($_POST)?json_encode($_POST):'null';?>; </script>

X - 是我从HTML表单提交信息的后期值,但如果我按一次评论提交,则javascript仅对此评论有效,直到页面刷新为止。
在这里你可以看到--- http://www.youtube.com/watch?v=okqQsPCZqVE

以下是整个java脚本:

$(document).ready(function(){

var x = $_POST['comentonpost'];
/* The following code is executed once the DOM is loaded */

/* This flag will prevent multiple comment submits: */
var working = false;

/* Listening for the submit event of the form: */
$('#addCommentForm'+x).submit(function(e){

    e.preventDefault();
    if(working) return false;

    working = true;
    $('#submit').val('Working..');
    $('span.error').remove();

    /* Sending the form fileds to submit.php: */
    $.post('comment.submit.php',$(this).serialize(),function(msg){

        working = false;
        $('#submit').val('Submit');


            /* 
            /   If the insert was successful, add the comment
            /   below the last one on the page with a slideDown effect
            /*/

            $(msg.html).hide().insertBefore('#addCommentContainer'+x).slideDown();


    },'json');

});

});

所以请告诉我如何解决这个问题并使这个脚本运行良好而不刷新页面?

1 个答案:

答案 0 :(得分:0)

尝试用以下代码替换var x = $_POST['comentonpost']

var x = <?= !empty($_POST['comentonpost']) ? json_encode($_POST['comentonpost']) : 'null';?>;

必须首先使用var variableName = new Array();定义Javascript数组,否则它们将像标准变量一样运行。因此,如果不需要,最好避免设置和处理它们的麻烦。