K
K
Kn942342021-06-14 20:57:12
PHP
Kn94234, 2021-06-14 20:57:12

How to pass js variable to php?

php file. Using the function, I display a visual, including links, by clicking on which, the say (this, 1) function is executed. It passes the value of the value tag to the js text_value variable, and fires an alert on that variable.
However, I need to transfer this js variable to a php variable. (text_value - this value is needed to create a link to the file, from where I load json data and feed them to armcharts - creating charts).
At the end of the code there is this line

$p1= "<script>document.writeln(text_value);</script>";

if instead of text_value we substitute a number or a word, then $p1 is displayed. Accordingly, all that remains for me is to pass the value of the local variable from the say () function to the outside. But I tried in different ways - it did not work. Lack of experience and knowledge. Does anyone have any ideas? Or maybe even a completely different approach to solving this problem?

UPD. I also tried using the GET and POST parameters, but it didn't work. Through the form I do not want - I need all the code to be executed in one file.

<?
$head->addFile('/components/excel_loader/admin/tmp/ui.css');

$stat_data = $db->query('SELECT id, title, views, statistic, total_views FROM com_page where statistic =1');
$stat_data = $stat_data->fetchAll();

function a_com()
{
global $stat_data, $ourData;
  
 	echo '
    <div id="main">
      <h1>Статистика просмотра страниц</h1>		
    </div>';

  $change="{MAIN_PAGE_CITY}";
  foreach($stat_data as &$iter) {

  $title = str_replace($change,'',$iter['title']);
  
  echo '<br><span><a href="https://xn--63-vlch8au.xn--p1ai/admin/com/statistic/?id='.$iter['id'].'"  
        name=""  value="'.$iter['id'].'" onclick="say(this, 1)">' . $title . '. </a> </span>: '.$iter['total_views'].' просмотр';
  echo $ourData = file_get_contents('stat/'.$iter['id'].'.json');
   	
  }
}
?>

<script>
function say(element, id) {	
        var text_value = $(element).attr('value');
  alert  (text_value);
  return text_value;
}
</script>

<? $p1= "<script>document.writeln(text_value);</script>"; 
echo $p1;
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vlad, 2021-06-14
@thoozu

Through the form I do not want - I need all the code to be executed in one file.

If desired, this can be done in one file, as well as AJAX to help

O
Oleg Frolov, 2021-06-14
@Digiport

can you try it with cookies?

S
Sergey, 2021-06-15
@oldzas

Can work with sessions? and do it through them.
https://github.com/AlexChittock/JQuery-Session-Plugin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question