Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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??

share|improve this question
    
possible duplicate of How to pass JavaScript variables to PHP? –  King-of-IT Mar 10 at 7:12
    
you cannot ...but you can do the vice versa –  CodingAnt Mar 10 at 7:12
    
what you have tried so far –  King-of-IT Mar 10 at 7:12

2 Answers 2

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?

share|improve this answer

Use jQuery.

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

Then in PHP $_GET['var'];

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.