-1

actually i am creating a online exam webapplication in php. and i m retrieving data from database .now the prob. is how to loop data by indexing one by one.. so user view one question at a time and after clicking on submit button it display the next question by from array. here i used array of all the rows and taking question one by one by javascript . but there is some prob. in javascript . i only render 1st question .. increment is not implementing correctly.

     <input type="hidden" value="<?php echo $i=0; ?>"  id="cc">
        <div id="counter">
        <p id="idIndex" name="id" value="<?php echo $findData[$i]['Exam']['id']; ?>"><?php echo $findData[$i]['Exam']['id']; ?>  </p>
        <p ><?php echo $findData[$i]['Exam']['que'];?></p>
        <input type="radio" value="<?php echo $findData[$i]['Exam']['op1']; ?>" name="op1" id="jaid" ><?php echo $findData[$i]['Exam']['op1']; ?>
        <input type="radio" value="<?php echo $findData[$i]['Exam']['op2']; ?>" name="op1" id="jai" ><?php echo $findData[$i]['Exam']['op2']; ?>
        <input type="radio" value="<?php echo $findData[$i]['Exam']['op3']; ?>" name="op1" id="jai"><?php echo $findData[$i]['Exam']['op3']; ?>
        <input type="radio" value="<?php echo $findData[$i]['Exam']['op4']; ?>" name="op1" id="jai" ><?php echo $findData[$i]['Exam']['op4']; ?>
        </div>
         <input type="submit" id="sub" onclick="myexam()" >
        <?php echo $this->Html->script('jquery-2.2.3.min'); ?>
        <script>

        function myexam(value)
        {
            var v=$("#cc").val();


        }


        </script>
  • You can't run PHP code inside Javascript. The PHP code will only run once. Either generate your JavaScript code using PHP or don't use JavaScript. You definitely can't generate PHP code with Javascript. – Gilles Lesire Sep 14 '17 at 11:56
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Qirel Sep 14 '17 at 11:56
  • Just to note; It's `$i++;` or `$i += 1;` not `$i =+ 1;` if you are in a loop outside of that code, you're defining the javascript function `myexam` multiple times and your function won't work how you expect it to – IsThisJavascript Sep 14 '17 at 11:58
  • There's quite a few points needing attention here. For a start: `` is reseting `$i` to 0 all the time. I don't think you intended that. – Ben Hillier Sep 14 '17 at 12:00
  • Secondly: you're not actually in a loop at all! Or if you are, you've left it out of the example – Ben Hillier Sep 14 '17 at 12:01
  • And thirdly: If you were in a loop, you would be reprinting the same Javascript function over and over. You need to print it once and let that do the work for you! – Ben Hillier Sep 14 '17 at 12:02
  • ok then tell me how to increment i value via javascript and then load next question Ben Hiller – Rahul Meemrot Sep 14 '17 at 12:10

1 Answers1

0

PHP is server side. Once your HTML is rendered, PHP has nothing more top do with it!

$("#cc").val("<?php echo $i=+1;?>");

The php echo is a string, that wont work.

var v=$("#cc").val();
v = v + 1;
$("#cc").val(v);

That should work. Fix the other line of JS similarly.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • It's nearer to being correct from what you had! Play with it and update your question when you get a little closer. – delboy1978uk Sep 14 '17 at 12:54