-4

How can we have JavaScript variable value in PHP variable within the same page??

I have a JavaScript variable which have the JSON String and I want to store in PHP variable so that I can store it into database. Is it possible??

Grant
  • 2,413
  • 2
  • 30
  • 41

2 Answers2

0

You should write a php page accepting the JSON using post and save it to database.

In javascript when you are ready to save the JSON, send the JSON to the PHP page using AJAX. This is the way to save some client side data without using forms.

As explained in this answer

Send JSON data from Javascript to PHP?

or if its a form you can refer this answer to submit the serialised data with HTML form

How to pass JavaScript variables to PHP?

Community
  • 1
  • 1
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

Use jQuery.

$.get('file.php?var='+variable);

Then in PHP $_GET['var'];

ThePixelPony
  • 721
  • 2
  • 8
  • 30