SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a jquery function is causing a problem in my php

    I have a piece of code below where it determines which assessment the user is in and the assessment number the user is currently on:

    Code:
    <h1>CREATING QUESTIONS AND ANSWERS: ASSESSMENT <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
    So on the browser this could read for example:

    CREATING QUESTIONS AND ANSWERS: ASSESSMENT 1 OF 4

    Now below I have a submit button:
    Code:
        <input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
    Now if the user clicks on the button, it will show a confirmation box and if the user clicks OK, for the confirmation, then it will submit the page and what is suppose to happen is that it adds a number to the assessment to indicate that the user is on the next assessment.

    SO FOR EXAMPLE:

    If it says this on the page:


    CREATING QUESTIONS AND ANSWERS: ASSESSMENT 1 OF 4

    If the user submits the page and confirms, then it should now say this:


    CREATING QUESTIONS AND ANSWERS: ASSESSMENT 2 OF 4

    This is because the user is on the next assessment now.

    Now this does work with the full code below:

    Code:
    			<?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;
            
            }
            
        else if (isset($_POST['submitDetails']) && $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
            $_SESSION['sessionCount']++;
        }
        
        
        $sessionMinus =  $_SESSION['sessionCount'];
        
        if ($sessionMinus == $_SESSION['initial_count']){ 
        
            $action = 'create_session2.php'; 
        
        }else if($sessionMinus != $_SESSION['initial_count']){ 
        
            $action = $_SERVER['PHP_SELF']; 
        
        }
        
        ?>
        
        <script type="text/javascript">
        
         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>
        
        <body>
        
        <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">
        <h1>CREATING QUESTIONS AND ANSWERS: ASSESSMENT <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
        
        <p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" /></p>
        </form>
        
        </body>
        
        
                 <script type="text/javascript">
            			
        function myClickHandler(){
             if(validation()){
                        showConfirm();
             }
        }
        
        </script>
    But the strange thing is though is that if I add my javascript 'validation()' function into the javascript code. If I click on the submit button to confirm and submit the page, then suddenly it doesn't add the Assessment number after page is submitted.

    SO FOR EXAMPLE:

    If it says this on the page:


    CREATING QUESTIONS AND ANSWERS: ASSESSMENT 1 OF 4

    If the user submits the page and confirms, then it still says:


    CREATING QUESTIONS AND ANSWERS: ASSESSMENT 1 OF 4

    This is obviously incorrect.

    So my question is that when I include the javascript function `validation()` into the code, why does it not add the number after the page is submitted?

    Below is the `validation()` function:

    Code:
    function validation() {
        
            var _qid = "";
            var _msg = "";
        
            var alertValidation = "";
            // Note, this is just so it's declared...
            $("tr.optionAndAnswer").each(function() {
        
        
                _qid = $("td.qid",this).text();
                _msg = "You have errors on Question Number: " + _qid + "\n";
        
                $(".textAreaQuestion",this).each(function() {
        
        
        
                    if (!this.value || this.value.length < 5) {
                        alertValidation += "\n\u2022 You have not entered a valid Question\n";
                    }
        
                    if (alertValidation != "") {
                        return false; //Stop the each loop 
                    }
                });
                
                       if(alertValidation != ""){
                    return false;
                }
                
                });
        
              
            if (alertValidation != "") {
                alert(_msg + alertValidation);
                return false;
            }
        
            return true;
        
        }

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    first, lets add a bit of color so my eyes dont go bad, and then i'll answer the question.

    PHP Code:
                <?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;
            
            }
            
        else if (isset(
    $_POST['submitDetails']) && $_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
            
    $_SESSION['sessionCount']++;
        }
        
        
        
    $sessionMinus =  $_SESSION['sessionCount'];
        
        if (
    $sessionMinus == $_SESSION['initial_count']){ 
        
            
    $action 'create_session2.php'
        
        }else if(
    $sessionMinus != $_SESSION['initial_count']){ 
        
            
    $action $_SERVER['PHP_SELF']; 
        
        }
        
        
    ?>
        
        <script type="text/javascript">
        
         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>
        
        <body>
        
        <form id="QandA" action="<?php echo htmlentities($action); ?>" method="post">
        <h1>CREATING QUESTIONS AND ANSWERS: ASSESSMENT <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'?></h1>
        
        <p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" /></p>
        </form>
        
        </body>
        
        
                 <script type="text/javascript">
                        
        function myClickHandler(){
             if(validation()){
                        showConfirm();
             }
        }
        
        </script>
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Ok: General comment #1: elseif is a valid php statement (so you shouldnt need else if).


    #1: I assume 'sessionNum' comes from an initial page where you ask them how many iterations to do.
    #2: $sessionMinus is a redundant value if you're going to use the $_SESSION variables directly elsewhere.
    #3: This code is rather susceptible to back-button failures. I'd suggest putting the current-iteration value into a hidden form field, use that to check if the form has been submitted and ensure that it's for the correct question. (I'm guessing this would also solve your problem; are you using IE to test this?)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Ok: General comment #1: elseif is a valid php statement (so you shouldnt need else if).


    #1: I assume 'sessionNum' comes from an initial page where you ask them how many iterations to do.
    #2: $sessionMinus is a redundant value if you're going to use the $_SESSION variables directly elsewhere.
    #3: This code is rather susceptible to back-button failures. I'd suggest putting the current-iteration value into a hidden form field, use that to check if the form has been submitted and ensure that it's for the correct question. (I'm guessing this would also solve your problem; are you using IE to test this?)
    To answer comment #1: Yes 'sessionNum' comes from an initial page where I ask users to enter in number
    To answer comment #2: I am not using $_SESSION['sessionCount'] else where on the page so hopefully $sessionMinus should not be redundant
    To answer comment #3: I am testing it on firefox and google chrome. If you do not mind can you write me a sample of code to match your comment #3

    Thank you


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •