-1

I'm currently looking for a way on how I can access a javascript variable and using a php code to store it in the database without using Ajax. The code below is a stripped-down version of what I'm currently working on:

<script>
 function secondPassed() {
   document.getElementById('time').value = parseInt(document.getElementById('time').value) + 1;
   int val = 0;
   num = "<?php $insert = mysql_query('INSERT INTO tbl_timer VALUES(\'\', \"."$val".\"  )',$connection); ?>";}
</script>

I'am trying to get the value of val and store it in the database but it will not be inserted. Please tell me where I went wrong with this code.

Sheila T
  • 1
  • 2
  • 1
    You either use ajax, reload the page, or use sockets, or anything else that can actually send the data to the server. – adeneo Feb 07 '16 at 14:22
  • but i just need to use javasrcipt only. – Sheila T Feb 07 '16 at 14:24
  • 2
    You can't, PHP runs on the server, one it has done it's job, it sends the HTML to the users browsers, and that's where javascript runs. Needless to say, by the time the page reaches the users browsers, it's too late to do anything with PHP, now you have to make requests and send stuff back to the server to use PHP again. – adeneo Feb 07 '16 at 14:28
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Johannes Jander Feb 07 '16 at 16:17

2 Answers2

0

Please tell me where I went wrong with this code.

Well, .php scripts are executed by the server, before result is sent to the user. Wrapping it in <script> </script> won't help. So, yours code flow looks like:

→ Serverside, php:

<?php
  ...
?>
<script>
 function secondPassed() {
   document.getElementById('time').value = parseInt(document.getElementById('time').value) + 1;
   int val = 0;
   num = "<?php 
       $insert = mysql_query('INSERT INTO tbl_timer VALUES(\'\', \"."$val".\"  )',$connection);
       // error notice (probably suppresed), there is no $val variable defined...
       // If somethig _will_ be inserted, it will contain just (0) or ("")
?>";}
</script>

→ Client side, html:

...
<script>
 function secondPassed() {
   document.getElementById('time').value = parseInt(document.getElementById('time').value) + 1;
   int val = 0;
   num = "0";}
</script>

And when browser executes that <script>, it will just store incremented value in #time, and nothing else.

If you want pass something from javascripts to server, you must make request to the server, always. That's just logical.

So, as @adeneo suggested, you either must use:

  1. ajax request (aynchronous/synchronous).
  2. Cookies (synchronous, page reload will be required to pass cookies to server)
  3. Sockets (asynchronous/synchronous, and there is plenty of socket-related solutions)
  4. iframes (asynchronous, can cause unwanted side effects).
  5. Don't use serverside database at all (if appropriate). Just use browser's localStorage or similar.
ankhzet
  • 2,517
  • 1
  • 24
  • 31
  • I use ajax as you you suggested but it still won't work. This is my code for it. – Sheila T Feb 08 '16 at 13:23
  • There no need to use '$' in variable names in javascript (`$val` and `val` are _different_ variables), and no `int` type specifier. Use something like this: ``. Remember, that you need jQuery included in yours html for this to work. Also, in almost any modern webbrowser there is debug console, where you can look for errors (syntax, execution) in code (like Developer Tools in Chrome). – ankhzet Feb 08 '16 at 14:24
0

I've actually had to do this many times on several occasions. Passing variables from javascript to php is no biggie. However, it's very difficult to answer you without the specifics of what you're trying to do. The question here is, are you trying to pass the seconds passed on a real time basis? Are you trying to input data already acquired by the browser(via windows.onload) or via user interaction? are the seconds passing while the interaction with the server is active? If so, you'll need to cache the data first via json. Otherwise php is just gonna render with different times. You could do it with XML, but if this is mission critical, speeds will vary and again you will get very different data just because of the way php communicates with the server. json will be a better choice. You could use Angularjs (not my personal recommendation) or if it's a small application on your website, ReactJS is very handy but you need to read up on it. Not enough room here to explain. Otherwise cache the data first using .data(), which is jquery and technically AJAX. Frankly, idk why you don't want to use AJAX, it's the perfect tool for this issue. .data() is what I always use for prototyping and production of this types of issues. It works like a charm.

BOTTOM LINE: There is no way of doing this without caching the data to json or xml first so that php can retrieve it later. PHP won't do it in realtime. It's just not built for it. You will need one class to retrieve the data whether via POST method and one class to convert from json to php objects and then you can use whatever methods to pass to MYSQL.