Problems with math equations not being able to loop

Hi, I am new to php and jquery so my code and this problem may seem so dumb, but it is not performing like I am expecting. The starting numbers are 3, 4, 5, 6, 7, 8, 9. I have when a div is clicked the numbers are supposed to add 7 to themselves calling these functions.

$('.rightbox').on("click", function(){

        <?php
            correctdatesright();
        ?>
        $('.date1').html(<?php echo($day1) ?>);
        $('.date2').html(<?php echo($day2) ?>);
        $('.date3').html(<?php echo($day3) ?>);
        $('.date4').html(<?php echo($day4) ?>);
        $('.date5').html(<?php echo($day5) ?>);
        $('.date6').html(<?php echo($day6) ?>);
        $('.date7').html(<?php echo($day7) ?>);
    });
    $('.leftbox').on("click", function(){
        <?php
         correctdatesleft();
        ?>
        $('.date1').html(<?php echo($day1) ?>);
        $('.date2').html(<?php echo($day2) ?>);
        $('.date3').html(<?php echo($day3) ?>);
        $('.date4').html(<?php echo($day4) ?>);
        $('.date5').html(<?php echo($day5) ?>);
        $('.date6').html(<?php echo($day6) ?>);
        $('.date7').html(<?php echo($day7) ?>);
    });

The functions are:

function correctdatesleft() {

        global $day1, $day2, $day3, $day4, $day5, $day6, $day7;

        $day1 -= 7;
        $day2 -= 7;
        $day3 -= 7;
        $day4 -= 7;
        $day5 -= 7;
        $day6 -= 7;
        $day7 -= 7;
    }
    function correctdatesright() {
        global $day1, $day2, $day3, $day4, $day5, $day6, $day7;

        $day1 += 7;
        $day2 += 7;
        $day3 += 7;
        $day4 += 7;
        $day5 += 7;
        $day6 += 7;
        $day7 += 7;
    }

For some reason it only allows me to click the div once for each and it making a change. The rightbox function only works to make the numbers 10, 11, 12, 13, 14, 15, 16 and the leftbox only to return it to the normal values 3, 4, 5, 6, 7, 8, 9. Anyone know how I can loop it to go on forever?

P.S Sorry if format of this post is bad, first time using this.

Okay. Here’s where it’s going wrong.

PHP and Javascript operate at different execution timeframes.

PHP operates on the server end, and operates before the browser sees the output.
Javascript operates in thr browser.

Your PHP code Preprocesses the page, and hands the resulting HTML to the browser. At that point, PHP is done. It stops operating.

Anything you want to have happen as a result of user interaction, without reloading the page, must be handled entirely by javascript.

Hint: View Source on your webpage. That’s all that javascript sees.

4 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.