Select an ID of a radiobutton query and store it in a session

Asked

Viewed 216 times

0

I’m having trouble picking up an ID from a query using array and storing it in a session to use it on other screens. Any help I thank.Dry the code below

Tela do Pedido

Order Screen

<!DOCTYPE html>

<html>
<head>
    <title>Projeto Web-ConsultarPedido</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../_css/layoutPrincipal.css">
    <link rel="stylesheet" href="../_css/formulario.css">
    <style>
  ul{
     list-style: none;
  }
    #lista{
        display:table-cell;
        padding-left:10px;
    }

    p{
     font-family:"arial black";
     font-size:15px;
     margin: 10px 20px 20px 20px;
      }
    </style>    
</head>
<body>

<header id="cabecalho">
    <img src="../_imagens/Logo.jpg">
</header>
<br class="fixFloat">
<nav id="menu">
    <ul>
        <li><a href="../index.html" target="_self">Home</a></li>
        <li><a href="../cadastra-se.html" target="_self">Cadastra-se</a></li>
        <li><a href="logout.php" target="_self">Logout</a></li>
    </ul>
</nav>


<section id="form">
    <?php

    session_start();
    $ID_Cliente = $_SESSION['ID_Cliente'];

    $conexao = mysql_connect("localhost:3306","root","root") or die("Erro durante a conexão do banco de dados");
    mysql_select_db("prestadora",$conexao);
    mysql_query("SET NAMES 'utf8'", $conexao);
    mysql_query('SET character_set_connection=utf8', $conexao);
    mysql_query('SET character_set_client=utf8', $conexao);
    mysql_query('SET character_set_results=utf8', $conexao);
    $consulta= "select * from pedido where ClienteID_Cliente='$ID_Cliente'" ;
    $resultado=mysql_query($consulta,$conexao) or die ("Não foi possível Consultar os seus dados.");
    mysql_close($conexao);



      while($consulta=mysql_fetch_array($resultado)){
        $_SESSION["ID_Pedido"]=$consulta["ID_Pedido"];  
        $ID_Pedido = $_SESSION["ID_Pedido"];        
        $Tipo_Servico=$consulta["Tipo_Servico"];
        $Status_Servico=$consulta["Status_Servico"];


          echo "<fieldset id='form_field'><legend id='form_legend'>Pedidos</legend><input type='radio' name='pedido' value='$ID_Pedido'>
 <p>Tipo de Serviço:$Tipo_Servico<br/>Status_Servico:$Status_Servico<br/></p></input></fieldset>";

      }



     ?>
     <fieldset id="form_field">
        <legend id="form_legend">Opções</legend>
        <ul>
       <li id="lista" > <a href="novopedido.php" id="botao">Novo Pedido</a> </li>
       <li id="lista"> <a href="alterarpedidoform.php" id="botao">Alterar Pedido</a> </li>
       <li id="lista" > <a href="../menucliente.html" id="botao">Excluir Pedido</a> </li>
         <li id="lista" > <a href="../menucliente.html" id="botao">Voltar</a> </li>
        </ul>
     </fieldset>;




</section>


</body>
</html>

Screen to Change Order

<!DOCTYPE html>

<html>
<head>
    <title>Projeto Web-AlterarPedido</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../_css/layoutPrincipal.css">
    <link rel="stylesheet" href="../_css/formulario.css">

</head>
<body>

<header id="cabecalho">
    <img src="../_imagens/logo.jpg">
</header>
<br class="fixFloat">
<nav id="menu">
    <ul>
        <li><a href="../index.html" target="_self">Home</a></li>
        <li><a href="../cadastra-se.html" target="_self">Cadastra-se</a></li>
        <li><a href="../login.html" target="_self">Login</a></li>
    </ul>
</nav>
<section id="form">

<?php
   session_start();
    $ID_Pedido = $_SESSION['ID_Pedido'];


    $conexao = mysql_connect("localhost:3306","root","root") or die("Erro durante a conexão do banco de dados");
    mysql_select_db("prestadora",$conexao);
    mysql_query("SET NAMES 'utf8'", $conexao);
    mysql_query('SET character_set_connection=utf8', $conexao);
    mysql_query('SET character_set_client=utf8', $conexao);
    mysql_query('SET character_set_results=utf8', $conexao);
    $consulta= "select * from pedido where ID_Pedido='$ID_Pedido' " ;
    $resultado=mysql_query($consulta,$conexao) or die ("Não foi possível Consultar os seus dados.");


      while($consulta=mysql_fetch_array($resultado)){

        $Tipo_Servico=$consulta["Tipo_Servico"];

      }

?>

    <fieldset id="form_field">
        <legend id="form_legend">Alterar Pedido</legend>
                 <form method="post" action="alterarpedido.php">
                 Tipo de Serviço: <select name="Tipo_Servico"  value="<?php echo $Tipo_Servico;?>">
                 <option name="Reforma Predial">Reforma Predial</option>
                 <option name="Jardinagem">Jardinagem</option>
                 <option name="Eletricista">Eletricista</option>
                   </select> 
                 <fieldset id="form_field">
                      <legend id="form_legend">Opções</legend>
                      <input type="submit" id="botao" value="Alterar">
                      <input type="reset" id="botao" value="Resetar">
                </fieldset>
            </form>

</section>


</body>
</html>

Bank Screen

<!DOCTYPE html>

<html>
<head>
    <title>Projeto Web-AlterarPedido</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../_css/layoutPrincipal.css">
    <link rel="stylesheet" href="../_css/formulario.css">
 </head>
<body>

<header id="cabecalho">
    <img src="../_imagens/Logo.jpg">
</header>
<br class="fixFloat">
<nav id="menu">
    <ul>
        <li><a href="../index.html" target="_self">Home</a></li>
        <li><a href="../cadastra-se.html" target="_self">Cadastra-se</a></li>
        <li><a href="../login.html" target="_self">Login</a></li>
    </ul>
</nav>


<section id="form">
    <?php

    $ID_Pedido=$_POST["ID_Pedido"];
    $Tipo_Servico=$_POST["Tipo_Servico"];




    $conexao = mysql_connect("localhost:3306","root","root") or die("Erro durante a conexão do banco de dados");
    mysql_select_db("prestadora",$conexao);
    mysql_query("SET NAMES 'utf8'", $conexao);
    mysql_query('SET character_set_connection=utf8', $conexao);
    mysql_query('SET character_set_client=utf8', $conexao);
    mysql_query('SET character_set_results=utf8', $conexao);
    $atualiza= "update pedido set Tipo_Servico='$Tipo_Servico' WHERE ID_Pedido='$ID_Pedido'" ;
    mysql_query($atualiza,$conexao) or die ("Não foi possível executar a atualização.");
    mysql_close($conexao);



    echo"<fieldset id='form_field'><legend id='form_legend'>Dados do Pedido</legend>
  <p>Dados do Pedido Alterado com Sucesso !!!</p></fieldset>";



    ?>

<fieldset id="form_field">
     <legend id="form_legend">Opções</legend>
    <a href="../menucliente.html" id="botao">Voltar</a>
</fieldset>

</section>


</body>
</html>
  • Explain to me better what is happening and what you want to happen... I don’t quite understand

  • I made an Order query and I want to take his ID and store one in a session.But I don’t know how to catch it selecting with a radiobutton

  • I left an image to help you better visualize the doubt

1 answer

0


One possibility is to use an ajax request to send the id of the request that was clicked to the server. To avoid having to change many things in your code you can use the fetch api (available in newer browsers), instead of having to use the jquery plugin.

Basically you should add the following javascript block before closing the tag body of the file Order screen:

<script type="text/javascript">
    //retorna um vetor com todos os radio buttons com name igual a pedido
    var radios = document.getElementsByName('pedido');

    //percorre o array de radio button adicionando um evento para quando houver um click 
    //em um deles
    for(var i = 0; i < radios.length; i++){

        radios[i].onclick = function(){
            //quando um radio button for clicado, envia o id dele par ao servidor
            enviarPedidoId(this.value);
        };
    }
    function enviarPedidoId(id_pedido){
        var parametros = { 
        method: 'POST',
        body: 'id_pedido=' + id_pedido,
        headers: new Headers({
           'Content-Type': 'application/x-www-form-urlencoded'
        }) 
        };

        //pagina em php para receber o id do pedido e armazenar na sessao
        fetch('recebePedido.php', parametros)
        .then(function(response) {

        }); 
    }
</script>

And create a new php file (not to mess with what is done). In the above example, a file called receive.php was created, with the following content::

<?php
session_start();

$_SESSION['id_pedido'] = $_POST['id_pedido'];
?>
  • Thank you very much served well

Browser other questions tagged

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