There is an array:

$arr = [ ['id'=>101, 'note'=>'Содержание_1'], ['id'=>108, 'note'=>'Содержание_2'], ['id'=>258, 'note'=>'Содержание_3'] ]; 

On the php page, radio buttons are generated whose value is equal to the 'id' element from the $ arr array.

Here is an example code:

 <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script> $(function(){ $('.id').click(function(){ $('.note').html($(this).val()); }); }); </script> </head> <body> <?php $arr = array( array('id'=>101, 'note'=>'Содержание_1'), array('id'=>108, 'note'=>'Содержание_2'), array('id'=>258, 'note'=>'Содержание_3') ); ?> <input class="id" name="id" type="radio" value="<?=$arr[0]['id']?>" />punkt 1 <input class="id" name="id" type="radio" value="<?=$arr[1]['id']?>" />punkt 2 <input class="id" name="id" type="radio" value="<?=$arr[2]['id']?>" />punkt 3 <div class="note"></div> </body> </html> 

Now when I select a radio button, the value of 'id' falls into the div class = "note".

Is it possible using javascript to extract the value of 'note' from the array $ arr, whose 'id' is equal to the value of the selected radio button and output in the div class = "note"

  • In my opinion, php cannot be extracted from the php array using javascript, or am I mistaken? - Xfirab
  • People can help somebody, they really need to decide - Xfirab

3 answers 3

It can be done this way, but all the data will be visible in the DOM

 <input class="id" name="id" type="radio" value="<?=$arr[0]['id']?>" data-array="<?=$arr[0]['note']?>"/>punkt 1 <script> $(function(){ $('.id').click(function(){ $('.note').html($(this).attr('data-array')); }); }); </script> 
  • The fact that it will be visible in the DOM as something is not good, i.e. This is bad - Xfirab
  • @Xfirab And why is it bad, if not a secret? - rjhdby
  • @rjhdby not I wanted to ask, i.e. “Is it good or not?” And so the decision of lyhoshva was quite suitable - Xfirab
  • @Xfirab data attributes are intended to be used via JS. In general, it is normal just all the envy of the data there. - lyhoshva
  • @lyhoshva is still such a moment, now the menu has a value of 'a "88" Content_1' in the 'note' element, and nothing comes out after the letter 'a'. Here in my opinion double quotes interfere. How can I screen them? - Xfirab

You can not extract data from a php-array for the simple reason that this array exists only at the time of execution of the php-script. As soon as the script has completed and delivered the rendered page to the web server for transmission to the client browser, the array disappears. (we will not now consider caching and sessions, since in fact they are the same eggs, only in profile)

There are exactly two solutions:

  1. Give this data to the customer. Either as DOM attributes, or as an array of JS.
  2. When selecting radio buttons, make a request to the backend.
  • @PavelMayorov this is what you, sorry? - rjhdby
  • @PavelMayorov I also did not understand - Xfirab
  • @rjhdby this is me to your recent vote for closing spam - Pavel Mayorov

And what's the problem when getting an id make a call to the php script via ajax , pass an id in the request and get the corresponding note value?

In this case, I see only such a solution, provided that you do not want to display the value of note on the page.

Another solution proposed by @lyhoshva is to use data-class , it is also possible to output the value of note in the input type="hidden"