0

I need to get data from the database, I use PHP code to do that, but when I need to set an array value in this part new_article_query->execute( array(article, manufacturer) );
how can I use javascript value if article and manufacturer are defined in javascript

<script>
//Javascript function code part 1...
//Then I insert PHP code in the javascript function

<?php
$new_article_query = $db_link->prepare('SELECT * FROM `shop_prices_data` WHERE `article` = ? AND `manufacturer` = ?;');
    $new_article_query->execute( array(article, manufacturer) );
    $new_article = $new_article_query->fetch();
    $reg_invoice_value = $new_article["$new_article"];
?>  

//Then javascript function continue
    
    return start_wrap_div + '<td class="td_manufacturer">'+ manufacturer + show_hide_button +'</td><td class="td_article">'+ article_show +'</td><td class="td_name">'+ name +'</td><td class="td_exist">'+ exist +'</td><td class="td_time_to_exe"><span onclick="openInfoWindow(null, null, 1, \''+ supply_info_json +'\');">'+ time_to_exe +'</span></td><td class="td_info">'+ info +'</td><td class="td_price">'+ price +'</td><td class="td_add_to_cart">'+ cart_html +'</td>'+ '<td class="td_color" style="background:'+color+'"></td>' + end_wrap_div;
}
</script>
lolspy
  • 1
  • 5
  • This isn't how it works. PHP runs server side, only when a request is happening. Javascript runs client-side, once the page has loaded. They don't execute at the same time, so you can't intermesh them like that. If you have some data in Javascript, then you need to send it to the server via a HTTP request, in order to trigger the PHP script to run again. You can a) use AJAX, b) cause the browser to redirect to a URL with those values as query parameters, or c) cause the browser to submit a form with those values inside form fields. – ADyson Sep 01 '21 at 22:01
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Tangentially Perpendicular Sep 01 '21 at 22:54

1 Answers1

-1

You can use ajax for this. like below

<script>
function function_name(){
let article = 'article';
let manufacturer = 'manufacturer';

  $.ajax({
      type: "post",
      url: 'php_file_name/getData',
      data: {'article':article, 'manufacturer':manufacturer },
      succuess: function(msg){
         // your javascript 
      }
})
}

</script>

and in your php_file_name.php file

function getData(){
$article= $_POST['article'];
$manufacturer= $_POST['manufacturer'];

$new_article_query = $db_link->prepare('SELECT * FROM shop_prices_data WHERE article = ? AND manufacturer = ?;');
    $new_article_query->execute( array(article, manufacturer) );
    $new_article = $new_article_query->fetch();
    $reg_invoice_value = $new_article["$new_article"];

//return data

}
Rishni Meemeduma
  • 324
  • 3
  • 14
  • It would be sensible to mention that this requires jQuery, since the OP has not mentioned or tagged jQuery and may not be aware of this rquirement. Also the URL `php_file_name/getData` won't work without a routing engine being present in the code, which also isn't mentioned. Don't overcomplicate it, but if you are adding such extra things, you should explain them in the answer rather than just giving a raw code dump. – ADyson Sep 02 '21 at 10:51