-2

I am trying to add a day to a php date variable every time a button is clicked using javascript onclick(). Below is the code I have so far the only problem is that the first time I click the button, a day gets added to the date, however, any subsequent clicks of the button do nothing.

<?php $depDate = $dep ?>;

        function dayBack() {
            "<?php echo $depDate = date('Y-m-d', strtotime($depDate. ' + 1 days'))?>";
            document.getElementById("hell").innerHTML = "<?php echo $depDate ?>";
        }

The variable $dep is already declared earlier in the code and is equal to a date that the user entered in a form.

JamieNesh
  • 21
  • 6
  • 1
    Does this answer your question? [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) – El_Vanja Feb 12 '21 at 15:34
  • @El_Vanja is correct. The main issue is that PHP is executed at the server, while only the JavaScript is executed in the Browser. This means that in order to have the variable being actually available to PHP, a click on the button you have needs to send some form to the server, where the new $depDate gets calculated. – Ancoron Feb 13 '21 at 00:56

2 Answers2

1

That is because you are mixing two different languages and interpretations of those.

If you take a look at the developer tools in your brwoser, you will see that you are actually trying to execute JS code only, which looks somewhat like this:

    function dayBack() {
        "some date...";
        document.getElementById("hell").innerHTML = "result of $depDate";
    }

On the client-side, no PHP code is executed, it is replaced or removed so that JS code or HTML only remains.

You need a completely different approach to this question. Either move the logic from the backend to the client. Or if it is some sensitive logic, then keep it on the server and retrieve it with some ajax requests.

Also researching and understanding the difference between client and backend side code might help here.

henk
  • 2,677
  • 2
  • 18
  • 49
  • How would I go about writing php within the javascript that gets sent to the server on button click? – JamieNesh Feb 13 '21 at 16:54
  • You don't write PHP inside JS to send it to the server. Instead, you write just JS, which will send a request to the server. The server-side PHP code has then to process the request and send back a result, which then can be processed on the client by the JS code. Take a look at AJAX or POST/GET requests with php and js. – henk Feb 13 '21 at 18:57
0

Hello JamieNesh i think there is an easier and a complete "PHP way" to do this you can firstly add button within a form tag. Then Onwards update it on every click:

<form action="" method="post">
<button type='submit'>Add One Day</button>
</form>

The PHP part:

<?php

if(isset($_POST['add'])){
    $date = "02-12-2021";
    $date1 = str_replace('-', '/', $date);
    $tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));
}

?>