How to save click id on a PHP session?

Asked

Viewed 519 times

-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 the alert is stored in a PHP session.

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

1 answer

2

You’re mixing PHP with Javascript.

First you need to understand two things:

  • PHP is a server-side language - it runs on the server side.
  • Javascript is a client-side language - it 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 upon this HTML result.

Therefore, it is not possible to affect the variable of a PHP code through the intervention of Javascript, because 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, we go to the next situation:

There is no way to write the javascript variable directly in the PHP function. What you can do is send a Javascript request to your PHP script to process that information.

You can use the $.ajax 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 the salvar_informacao.php, you must put:

session_start();

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

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

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

  • 1

    The phrase "cannot mix Javascript code with PHP" is ambiguous. Actually it is possible let appName = "<?php echo "My App" ?>", what is not possible is to change the values of the variable PHP after processing.

  • 1

    @Valdeirpsr is true, in the context of it, really is not possible, but has to explain better. I will edit

Browser other questions tagged

You are not signed in. Login or sign up in order to post.