0

I have to show redirect the page with increment by one using javascript function. But When I try this I got NaN Error . Can anyone please help me to fix the Problem.I Have attached My Source Below. like this quiz.php?qusId=NaN.

    <script type="text/javascript">
        function handler(var1,quizId) {
            alert(var1);
            var id = parseInt(quizId);
            window.location = "quiz.php?qusId="+parseInt(quid(id));
        }
        function quid(quzId){
            if(quzId == 1){
                return 1;
            }else{
                return quzId++;
            }
        }
    </script>
</head>
<body>
    <?php 
        $qusId=$_GET['qusId'];
        ?>
        <form action="test.php" method="POST">
            <?php
                $result = select("SELECT * FROM questions WHERE question_id='$qusId'");
                //$row = mysql_fetch_array($result);
                $i=$_GET['qusId'];
                while($row = mysql_fetch_array($result))
                {
                    ?>
                        <table width="581" height="299" border="1">
                            <tr>
                                <td>Union Assurance Questionnaire</td>
                            </tr>
                            <tr>
                                <td>
                                    <?php 
                                        echo $i.'.' .$row['questions']; 
                                        $i++;
                                    ?>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <?php 
                                        $qId=$row['question_id'];
                                        $result1=select("SELECT * FROM answers WHERE questionId='$qId' ORDER BY RAND()");
                                        while($row1=mysql_fetch_array($result1)){
                                            ?>
                                                <input type="radio" name="answers" value="<?php echo $row1['answers'];?>"  onclick="handler('<?php echo $row1["feedback"]; ?>,<?php echo $qusId;?>')" /><?php echo $row1['answers']; ?><br/>
                                            <?php 
                                        } 
                                    ?>
                                    &nbsp;
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                    <?php
                }
            ?>
        </form>
2
  • Dont use parseInt.. Just give as a string. Thats enough for PHP as its being passed in the URL, so the type is not considered. window.location = "quiz.php?qusId="+quid(id); Commented Sep 24, 2012 at 11:21
  • Don't trust user input! You're assuming that $_GET['qusId'] is a number but that's not necessarily true and that gives you security holes. Use $qusId=(int)$_GET['qusId'] and read about SQL injection vulnerabilities (eg. veracode.com/security/sql-injection) Commented Sep 24, 2012 at 11:25

1 Answer 1

2

In your click handler, you've got

handler('<?php echo $row1["feedback"]; ?>,<?php echo $qusId;?>')

You've put the single quotes in the wrong place - it's being passed into your 'handler' function as a single parameter. Try instead:

handler('<?php echo $row1["feedback"]; ?>',<?php echo $qusId;?>)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.