Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

how can I pass ajax value to php variable ?

I NEED IT FOR PASSING VARIABLES FROM AJAX TO PHP

2 Answers

You store the data you want to send in the data attribute of your ajax call. In a previous project I had this in my ajax call type : {type_id : type } where type was a variable I got from jQuery using var type = $('#type').val()

Another attribute of the ajax call will be the URL. To wherever that URL is pointing that file needs to receive the data. And that's easy. In that PHP file, in my case it simply was $type_id = $_GET['type_id']

Now I have a PHP variable $type_id that held the value from the ajax call. In my case it was a select drop down and i was getting the id value of the select, which is why I was calling it type_id.

thank you so much for answer , it was useful