jquery函数停止在php中添加数字

时间:2012-09-16 17:08:03

标签: php jquery

我在下面有一段代码,用于确定用户所处的评估以及用户当前所在的评估编号:

   <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?>

所以在浏览器上这可以读取例如:

1 OF 4

现在,我有一个提交按钮:

<input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />

现在,如果用户点击该按钮,它将显示一个确认框,如果用户点击确定,进行确认,那么它将提交页面,并且假设发生的是它为评估添加了一个数字表示用户正在进行下一次评估。

例如:

如果在页面上显示:

1 OF 4

如果用户提交页面并确认,那么现在应该这样说:

2 OF 4

这是因为用户现在正在进行下一次评估。

但问题是当用户提交页面时根本没有添加数字。它只是保持在'1'并且不加起来。因此,不是采取以上措施,而是采取以下措施:

如果在页面上显示:

1 OF 4

如果用户提交页面并确认,则仍然显示:

1 OF 4

这显然是不正确的。

我已经测试了我的代码,我发现的是,如果有jquery validation()导致数字不加起来并保持在'1'。但我的问题是,为什么这个jquery函数会导致这种情况发生?

以下是jquery validation()函数:

function validation() {

    var _qid = "";
    var _msg = "";

    var alertValidation = "";
    // Note, this is just so it's declared...
    $("tr.optionAndAnswer").each(function() {

        });


    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;

}

以下是相关代码,以便您知道代码是什么:

    <?php

    session_start();


    if(isset($_POST['sessionNum'])){
                //Declare my counter for the first time

                $_SESSION['initial_count'] = $_POST['sessionNum'];
                $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
                $_SESSION['sessionCount'] = 1;

        }

    elseif (isset($_POST['submitDetails']) && $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
        $_SESSION['sessionCount']++;
    }


    $sessionMinus =  $_SESSION['sessionCount'];

    if ($sessionMinus == $_SESSION['initial_count']){ 

        $action = 'create_session2.php'; 

    }elseif($sessionMinus != $_SESSION['initial_count']){ 

        $action = $_SERVER['PHP_SELF']; 

    }

    ?>

    <script type="text/javascript">


    function validation() {

        var _qid = "";
        var _msg = "";

        var alertValidation = "";
        // Note, this is just so it's declared...
        $("tr.optionAndAnswer").each(function() {

            });


        if (alertValidation != "") {
            alert(_msg + alertValidation);
            return false;
        }

        return true;

    }




                 function showConfirm(){

             var confirmMsg=confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Session." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" );

             if (confirmMsg==true)
             {
             submitform();   
         }
    }

             function submitform()
    {
        var fieldvalue = $("#QandA").val();
        $.post("insertQuestion.php", $("#QandA").serialize() ,function(data){
            var QandAO = document.getElementById("QandA");
            QandAO.submit();
        });  
        alert("Your Details for this Session has been submitted"); 
    }




    </script>

    </head>

    <body>

    <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">

    <h1><?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>

    <div id="detailsBlock">


<table id="questionBtn" align="center">
<tr>
<th>
<input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
</th>
</tr>
</table>

</div>
<hr/>

<div id="details">
<table id="qandatbl" align="center">
<thead>
<tr>
    <th class="question">Question</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

    <p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" /></p>

    </form>

             <script type="text/javascript">

    function myClickHandler(){
         if(validation()){
                    showConfirm();
         }
    }

    </script>

更新:

下面是insertQuestion()函数的代码:

function insertQuestion(form) {   


    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'>");
    var $image = $("<td class='image'></td>"); 


var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" + 
    "<p class='imagef1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='imagef1_upload_form' align='center'><br/><label>" + 
    "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" + 
    "<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 
    "<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");     

    $image.append($fileImage);


    $tr.append($image);  
    $tbody.append($tr); 



}

1 个答案:

答案 0 :(得分:0)

我想你想检查sessionCount是否未设置(!isset)。尝试像这样调整代码:

if(!isset($_POST['sessionCount'])){
            //Declare my counter for the first time

            $_SESSION['initial_count'] = $_POST['sessionNum'];
            $_SESSION['sessionNum'] = intval($_POST['sessionNum']);
            $_SESSION['sessionCount'] = 1;

    }

elseif (isset($_POST['submitDetails']) && $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
    $_SESSION['sessionCount']++;
}