0

Is it possible to use the results of a JavaScript function in PHP?

Let me break down why. I have a table that has several columns, including a column that gives each row a checkbox. When a user presses an edit button, an onclick event is activated. The onclick event does a few things, but primarily it goes through the table and figures out which checkboxes are clicked, and returns an array of numbers which indicate the IDs of the entries of the selected rows.

What I want to know is how can I take these Ids and use them in my inline php code that should use the Ids to get corresponding data from a database and generate a new table.

Also, is there another way of doing this type of thing? This seems a little convoluted. I've tried XMLHTTPRequests to get the data directly with Javascript but I had trouble/couldn't get past using the GET method of an XMLHTTPRequest. My code would execute, but I wasn't sure how to get/use the data.

Any help is appreciated.

Hay Trần
  • 564
  • 4
  • 22
Ben
  • 117
  • 3
  • 10
  • 1
    You can't do this as the JavaScript is client side and PHP is server side. You will need to perform a network request in JavaScript to send the information to the server. – SJTriggs Nov 28 '19 at 04:47
  • have you tried using GET parameters with your click event? – Meraki Nov 28 '19 at 06:32
  • 1
    I realise it might be hard to come up with the right search terms, but there are many, many duplicates of this on SO, [here's a good example with a very clear answer](https://stackoverflow.com/questions/1917576/how-do-i-pass-javascript-variables-to-php). – Don't Panic Nov 28 '19 at 08:34
  • 1
    Does this answer your question? [How do I pass JavaScript variables to PHP?](https://stackoverflow.com/questions/1917576/how-do-i-pass-javascript-variables-to-php) – Don't Panic Nov 28 '19 at 08:34

1 Answers1

0

You can't use javascript values in your PHP statements because, imagine that the javascript is a client-side language, so it don't communicate with the server, and imagine that PHP is a server-side language, so it can't communicate with javascript. When you request a webpage, the request go to the server and the server will trait your request, and all output of this request (the webpage) is a code on client-side language and it is rendered with javascript, html and css. So, we can say that javascript have no idea of what is PHP, because, what PHP do is write a full html code and send to your browser render like a response. But you can use Ajax inside your client-side, there is a lot of tools developed in javascript that do the work of make requests to server-sides