Pass PHP variable to JS function so that it returns to another PHP

Asked

Viewed 36 times

0

Hello, probably the title was confused,but I will explain myself at most:I have a project in which in my page is being listed data from a table that I have in my database,these Data indicate a Group(ex.:id of the group=14,name of the group=group,admin of the group=so),and I need to create a way to "access" this group that contains other information in it. my problem is: I made a select that listed the groups,that are in the admin account,and I need to select one,pick your id and pass it to another php,so I can select from the other table that relates to the Grup id. select page:

 <header>
<a href="group.php">grupos</a> //pagina atual
<a href="../php/unset.php">sair</a>
<a href="home.php">home</a>

</header>
<hr>
 <table class="table">
     <thead>
 <tr>
   <th scope="col">ID do Grupo</th>
   <th scope="col">Nome do Grupo</th> 
   <th scope="col">Nome do líder</th>
   <th scope="col"><a href="create_group.php"><button type="button" class="btn btn-success">Criar</button></a></th>
 </tr>
 </thead>
 <tbody>
<?php include '../php/groups.php'?> //aqui é o php que faz o select e lista
 </tbody>
</table>   

select php file(the connection to the bank is all done correctly,and the $id is set in a global variable $_SESSION['id'],I cut some parts here pos the code is very large,but this variable $id this set right):

include "conexao.php";
$sth=$pdo->prepare("SELECT team_id,team_name,user_name FROM teams JOIN users ON team_admin_id = user_id WHERE user_id = :user_id");
$sth->bindValue(":user_id",$id);
$sth->execute();

foreach ($sth as $res){
    extract($res);

    echo '<tr>';
    echo '<td><span id="id">'.$team_id.'</span></td>';
    echo '<td><a href="#">'.$team_name.'</a><button id="botao" onclick="getgroupINFO('.$team_id.',"'.$team_name.'")//essa é a função do js que estou querendo executar\\">Entrar</button></td>';
    echo '<td>'.$user_name.'</td>';
    echo '<td></td>'; //aqui viria alguns icones,retirei eles por enquanto
    echo '</tr>';



}

as " just put here to make notes, what I want is for php to pass the $team_id and $team_name to the JS function,taking the id that is written in the table row that I click,(ex.: click on the button whose id written in the table row is 12 and the name "group1", then the function getgroupINFO would receive (12,"group1"), then starts the language I know little:JS(Jquery included).

the function getgroupINFO() would need to take the data (citing the example:getgroupINFO(12,'group1')) and send it to another php (called group_select.php);

JS(it was called at the end of the page, right after JQUERY):

function getgroupINFO(id,groupname) {
 var dados = new XMLHttpRequest();
   dados.open("POST", "../php/group_select.php",true);
   dados.send(id)
   dados.open("POST",".../php/group_select.php",true);
   dados.send(groupname);
}

remembering that the code is "raw", has no CSS practically. tell me what I’m missing(a part of me says it’s JS, because it’s a language I’m still learning and I have very little knowledge) and if you need any more information you can ask

Sincerely yours truly, Matheus Henrique.

1 answer

0


I would indicate you to instantiate jquery in your project and make a php file by returning a json to ajax as follows

<script>
window.dados = "<?=$dados?>"; //Uma forma de passar uma variável do php pro JS
</script>

var dados = $('#id/.class').val(); // caso seja um form

$.ajax({
    url: 'arquivo.php',
    type: 'post',
    data: {dados: dados}
}).done(function(e) {
   console.log(e) // retorno da consulta
});

I hope I’ve helped a little if you have any questions!

Browser other questions tagged

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