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

PHP Posting Data with fetch()

Martin Park
Martin Park
12,792 Points

How to combine fetch() POST method with php?

Hi was wondering if anyone could help point me in the right direction. I know how to connect to a db and pass/retrive data with php.

I now know how to use fetch() to POST data using javascript.

Just not entirely sure how I connect the 2 different languages? Would I create a separate .php file with the required php function then use that url as the target in fetch()?

If there is a workshop/track for this, then I'd be keen to watch it :)

Thanks in advance

2 Answers

Martin Park
Martin Park
12,792 Points

Thanks for the reply James. I have just finished the AJAX course but it doesn't cover how to link to PHP. I'll keep looking.

Also trying to avoid Jquery as quite a few people advised that new Javascript updates mean Jquery is out of date. So trying to learn to do the above using vanilla Javascript.

jamesjones21
jamesjones21
9,260 Points

I tend not to use Jquery as much as possible, but its what they use in work -_-, but there is a JS class which you can intantiate which is HTTPRequest() then that should allow you to start using Ajax.

hope it goes well.

jamesjones21
jamesjones21
9,260 Points

There is a technique called Ajax where you can post the form data using javascript to php

You can use Jquery: https://www.w3schools.com/jquery/jquery_ref_ajax.asp

hope this helps :)