Question how to use javascript variable for php.

Asked 2 years ago, Updated 2 years ago, 37 views

var opt = $('#optSelect option:selected').val(); var index = $("#optSelect option").index($("#optSelect option:selected")); var price = <?= = $optionPrice[0];?>;

If you look at the var price, I want to put the index in the bracket of $optionPrice What should I do?

php html jquery javascript

2022-09-22 11:51

1 Answers

That code stored on the server is interpreted/executed by the server's PHP program. From PHP's point of view <?Except for the ones in php?>, it's just a String. I don't interpret it and just pass by as if it's commented.So the order, it makes sense to use the PHP variable in the Javascript code, but it doesn't make sense to use the Javascript variable in the PHP code.

I don't know specifically, but I think you're trying to use var price in other invisible JS codes below, but if the flow is like that, set the price to 0, and use asynchronous communication like jQuery.get() or jQuery.post() below, 0 or 1 as the key value of $optionPrice (var some value = 14).After you send...) you can get the value you want, and if you have, you can assign a new value from the server to the price variable that you set to 0.

in a nutshell

JS Code Execution -> PHP Code Execution -> To run JS Code, I think my level of answer is to use asynchronous communication such as,,,jQuery.get(), or jQuery.post(). I hope it was helpful!


2022-09-22 11:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.