-2

How to get javascript variable value in PHP and php value stored in to database.

if my JS code like

var age = new Date().getFullYear()-getYearInteger();

This code o/p 2016-(yearofbirth). I am getting output correct in JS. How to store this age value in MySQL database. How to call ajax function and insert the values?

jeni
  • 1
  • 6

2 Answers2

1

Send this javascript value through form or ajax submit method and get this value saved in Database.

You cannot directly get js values in Php.

A simple example is 
var age  = new Date().getFullYear()-getYearInteger(); 
$.ajax({
      url : '/save.php',
      type : "POST",
      data : {
                age    : age,
                saveAge    : true,
             },
      success : function(data)
      {
              data = JSON.parse(data);
              if(data.status == 1)
              {
                   window.location = 'index.php/dashboard';
              }
              else
              {                                

              }
       }    
});
S.I.
  • 3,250
  • 12
  • 48
  • 77
Mukesh Swami
  • 415
  • 2
  • 11
0

In order to save JS value to a PHP variable you need to make a server call.

You can easily do the vice versa like this:

var jsVariable = '<?= $php_variable ?>';

Have an ajax call, send the data to server side and store it in a variable.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • Thank you for your response. i have no knowledge in ajax. can u explain in detail? – jeni Nov 25 '16 at 07:22
  • @jeni just read your comment, did you check ajax? you can see this example http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – Danyal Sandeelo Nov 25 '16 at 07:38
  • Its not not working Please anybody help me. i added the above code in javascript page . how to insert into database. how to call ajax – jeni Nov 25 '16 at 07:42