-1

I want to get php array element using javascript variable as an index of a php array. I have tried this below method but it doesn't work.

// I want to get value of $arr[0] and here array index is a javascript variable

<script type="text/javascript">
function select_seller()
{
    d = document.getElementById('seller_id').value;
    document.write("<?php echo  $arr['<script>document.write(d)</script>'];?>");
}
</script>

Please Help me

B. Kanani
  • 606
  • 5
  • 11

2 Answers2

0

You can't do this. PHP code is executed first so once the user loads the page, there's no way for PHP to detect a JavaScript variable.

If you posted some more code we could probably tell you how to do what you are trying to do in JavaScript.

0

You don't seem to know the difference between server-side and client-side, your php code is interpreted on the server to produce html data and send it to the browser to render it to the user, hence you cannot use php function on the client-side .. php role is preparing and sending data to the client-side. so whatever you are trying to achieve here you have the option of using ajax calls to get data from the server and process it with js upon receiving it.

Sherif Salah
  • 2,085
  • 2
  • 9
  • 21