Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-16-2012, 06:09 PM   PM User | #1
carlbrooks
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
carlbrooks is an unknown quantity at this point
a jquery function is stopping numbers from adding up in 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:

PHP Code:
 <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'?>
So on the browser this could read for example:

1 OF 4

Now below I have a submit button:

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

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:


1 OF 4

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


2 OF 4

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

But the problem is that it is not adding the number at all when the user submits the page. It just stays at '1' and doesn't add up. So instead of doing the above it is doing the below:

If it says this on the page:


1 OF 4

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


1 OF 4

This is obviously incorrect.

I have tested my code and what I have found is that if there is that the jquery `validation()` is causing the number to not add up and stay at '1'. But my question is that how come this jquery function is causing this to happen?

Below is the jquery `validation()` function:

PHP Code:
    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;
    
    } 
Below is relevant code so you know what the code is:



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;
            
            }
            
        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>
carlbrooks is offline   Reply With Quote
Old 10-02-2012, 05:20 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
Code:
    
    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;
    
    }
the jQuery code doesn't apepar to be your issue. yoru validation fucntion, doesnt actually do anything. The code in red, does nothing. the rest of th efunction, the way it is written will always return true as you are never changing the value of alertValidation
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:09 PM.


Advertisement
Log in to turn off these ads.