0
let sup_id = $(this).val()
let sup ='<?php echo $conn->query("SELECT supplier_lead_time FROM `supplier_list` WHERE id=2")->fetch_array()[0]; ?>'

I want to concatenate sup_id inside sup variable

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • This makes zero sense. The PHP code would have already executed the query **before** your Javascript even exists, let alone gets executed itself. Remember, PHP runs on the server when loading your page, JS runs in the browser after your page has been loaded - see the duplicate in the blue box above. – ADyson Sep 29 '22 at 22:40
  • Remember, the web is a stateless, disconnected, client-server architecture. When the browser makes a HTTP request to the server, it starts the PHP code running. When the PHP code stops running, the finished output is delivered to the browser as the next page. To make the server do something more after that, you need to send another HTTP request to the server to start the process again. To trigger that, you can use AJAX (to avoid refreshing the whole page), or submit a form, or cause a navigation (via a hyperlink or Javascript) to a new URL which may include querystring parameters. – ADyson Sep 29 '22 at 22:40
  • So basically, if you want to run a PHP query using a value from your Javascript as a parameter, your javascript needs to initiate a new HTTP request to the server, and send that value as one of the parameters in the request. That request can go to a PHP script, which can read the input parameter, use it in the query, execute the query, and return the result back to the browser. – ADyson Sep 29 '22 at 22:41
  • thank you for your wonderful explanation – Raine Sabile Sep 29 '22 at 22:44
  • $('#supplier').change(function(){ let sup_id = $(this).val() let sup ='query("SELECT supplier_lead_time FROM `supplier_list` WHERE id=2")->fetch_array()[0]; ?>' $('#supplier_lead_time').val(sup) }) still no way to concatenate js variable to this query code. I just want to change value of texbox if select was change – Raine Sabile Sep 29 '22 at 22:49
  • You may have thanked me for the explanation, but I'm not sure you've fully understood it yet. Take a look at what your finished JS code looks like (e.g. using the View Source feature in your browser). `let sup ='query("SELECT supplier_lead_time FROM supplier_list WHERE id=2")->fetch_array()[0]; ?>'` will turn into something like `let sup = 123` (assuming that query returned the value `123`. That is then fixed into your JS code, because the PHP has **already executed** and the **output of that PHP** becomes part of the content delivered to the browser, including the JS code. – ADyson Sep 29 '22 at 22:54
  • Again...if you want to use the value of `sup_id` in a SQL query, you must **send that value to the server via a new HTTP request**. For example, you could use an AJAX request to send it to a PHP script. – ADyson Sep 29 '22 at 22:55

0 Answers0