Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laravel_is_right's avatar

Use javascript in php

Hi there. Here is part of my code :

...
 			       function minicart(courseId="")
					{
       		 			 $.ajax({
           					 type: "GET",
           					 datatype: "json",
            				 url: "/cart/basket",

           			 success:function(response)
           			 {
                			<?php
                			$course = Course::where("id",***courseId)->get();
               				 ?>
        ...

I need to use courseId value in where part, but i don't know the right syntax about that. How should i write CourseId in my php part? Thanks.

0 likes
2 replies
martinbean's avatar

@laravel_is_right You can’t use JavaScript in PHP. PHP is executed on the server. JavaScript is executed in your browser. The PHP process has already finished before a single line of JavaScript is executed.

gych's avatar

@laravel_is_right Handle tis on the back-end and return the Course together with the response of the ajax request.

Please or to participate in this conversation.