How to save button click id in a PHP session?

-3

I have the following table:

<tr id="teste">
      <?php echo '<form id="teste" action="banco.php" method="post"><td><input  id="bike'.$fetch['referenciageral'].'" name="bike'.$fetch['referenciageral'].'" onchange="this.form.submit()"  type="checkbox" value="on" '.$fetch['checkbox'].' class="confirmacoes"><a href="banco.php"> </a></td></form>';?>
       <?php  echo '<td >'.utf8_encode($fetch['referenciageral']).'</td>';?>
        <?php  echo '<td>'.utf8_encode($fetch['nivel']).'</td>';  ?>
        <?php echo '<td>'.utf8_encode($fetch['numero_item']).'</td>';?>
<?php   echo '<td>'.utf8_encode($fetch['material']).'</td>';?>
 <?php echo '<td>'.utf8_encode($fetch['descricao']).'</td>';?>


 <?php echo '<td>'.utf8_encode($fetch['qtdeng']).'</td>';?>
<?php  echo '<td>'.utf8_encode($fetch['qtdfalt']).'</td>';?>
<?php  echo '<td>'.utf8_encode($fetch['OBSERVACAO']).'</td>';?>
<?php  echo '<td><a id="logo-container" href="'.$fetch['PDF_PT'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_PDF_PT_IMAGE'].'"  /></a></td>';?>
 <?php echo '<td><a id="logo-container" href="'.$fetch['PDF_IT'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_PDF_IT_IMAGE'].'"  /></a></td>';?>
 <?php echo '<td><a id="logo-container" href="'.$fetch['LINK_JT'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_JT_IMAGE'].'"  /></a></td>';?>
 <?php echo '<td><a id="logo-container" href="'.$fetch['LINK_DXF'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_DXF_IMAGE'].'"  /></a></td>';?>
  <?php echo '<td><a id="logo-container" href="'.$fetch['LINK_RAR'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_RAR_IMAGE'].'"  /></a></td>';?>
 <?php echo '<td><a id="logo-container" href="'.$fetch['LINK_ZIP'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_ZIP_IMAGE'].'"  /></a></td>';?>
  <?php echo '<td><a id="logo-container" href="'.$fetch['LINK_EPJ'].'" class="brand-logo"><img src="'.$fetch['LINK_TCD#LINK_EPJ_IMAGE'].'"  /></a></td>';?>


 <?php echo '<form id="teste2" action="preencheformulario.php" method="post"><td><input type="submit" id="dev'.$fetch['referenciageral'].'" name="dev'.$fetch['referenciageral'].'"
onclick="reply_click(this.id)" ></td></form>';?>

And I have the script:

<script type="text/javascript">
function reply_click(clicked_id)
{
    alert(clicked_id);
<?php session_start(); $_SESSION['teste'] = this.id?>
}
</script>

I want the id that appears in alert to be stored in a PHP session.

I did as follows but it does not work, the session is empty.

    
asked by anonymous 24.10.2018 / 14:42

1 answer

2

You are mixing PHP with Javascript.

First you need to understand two things:

  • PHP is a server-side language that runs on the server side.
  • Javascript is a client-side language - which runs on the client side.

In other words, PHP runs on the server (usually Apache) to generate the HTML content that will be displayed on your site.

Javascript, in turn, is a language that will act on top of this HTML result.

Therefore, it is not possible to affect the variable of a PHP code through the intervention of Javascript, since the two act at different times.

The fact that you can write Javascript, HTML and PHP together, does not mean that you can mix the functionality of one with another.

Explained this, let's go to the next situation:

You can not write the javascript variable directly in the PHP function. What you can do is to submit a Javascript request to your PHP script for it to process that information.

You can use $.ajax of jQuery, for example:

function reply_click(clicked_id)
{
    $.ajax({
        url: 'salvar_informacao.php', 
        data: {clicked_id: clicked_id},
        success: function () {
            // Quando a requisição for concluída
        }
    });
}

In salvar_informacao.php , you should put:

session_start();

$_SESSION['clicked_id'] = $_POST['clicked_id'];

However, this will depend a lot on what you want to do.

Depending on the operation, you can simply use localStorage or even cookie to solve the problem. The latter can have the value accessed by PHP through the variable $_COOKIE .

    
24.10.2018 / 17:30