I have a question system, and I've implemented a form where people choose which area to respond to:
<form class="form-horizontal" method="POST" action="perguntas.php">
<fieldset>
<legend>SELECIONE O QUESTIONÁRIO</legend>
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Selects</label>
<div class="col-lg-10">
<select class="form-control" name="sub_grupo" id="sub_grupo">
<option value="100">SETOR 1</option>
<option value="200">SETOR 2</option>
<option value="300">SETOR 3</option>
<option value="400">SETOR 4</option>
<option value="500">SETOR 5</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">RESPONDER</button>
</div>
</div>
</fieldset>
</form>
In the perguntas perguntas.php
page I get the post with the sector value and write to a variable:
$sub_grupo = $_POST['sub_grupo'];
Then the question appears with my select's and works perfectly, the doubt is:
When the user answers the question and goes to the next page refreshes and thereby clears the post variable causing the system errors:
Notice: Undefined index: $ sub_group in /var/www/html/checklistlojas/setores/teste/pergunta.php on line 9
What could I do to stop this error?
Thank you all at once.
Here is an example of a select that I use with this $ _POST:
$query_pergs = mysql_query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id ='$area'
AND sb.sub_area_cod='$sub_grupo'");
while ($row = mysql_fetch_array($query_pergs)) {
$totalpergunta = $row["TOTAL_PERG"];
}
I tried to use SESSION in this way, but it did not work:
@session_start();
$_SESSION["sub_grupo"] = $_POST['sub_grupo'];
$sub_grupo = $_SESSION["sub_grupo"];
Personal, To try to help follow my complete codes:
INDEX.PHP:
<?php
include("conexao.php");
//PERGUNTAS
$teste_count_perg = mysql_query("SELECT
COUNT(id_pergunta) AS TOTAL
FROM relacaoperg
WHERE id_area = 100
AND id_subgrupo = 100");
while ($row = mysql_fetch_array($teste_count_perg)) {
$total_perg = $row["TOTAL"];
}
//RESPOSTAS
$query_count_resp = mysql_query("SELECT
COUNT(resp_resposta) AS TOTAL
FROM respostas
WHERE resp_loja = 2
AND resp_questionario = 5
AND resp_area = 100
AND resp_subgrupo = 100
AND resp_usuario NOT IN ( '37', '38', '39','40','1')");
while ($row = mysql_fetch_array($query_count_resp)) {
$total_resp = $row["TOTAL"];
}
?>
<?php
// A sessão precisa ser iniciada em cada página diferente
if (!isset($_SESSION)) session_start();
// Verifica se não há a variável da sessão que identifica o usuário
if (!isset($_SESSION['usu_login'])) {
// Destrói a sessão por segurança
session_destroy();
// Redireciona o visitante de volta pro login
header("Location: ../../logout.php"); exit;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>#</title>
<link rel="stylesheet" href="includes/css/style.css">
<link rel="stylesheet" href="includes/css/bootstrap.min.css">
<link rel="stylesheet" href="includes/css/datatables.css">
<script src="includes/js/jquery.min.js"></script>
<script src="includes/js/bootstrap.min.js"></script>
<script src="includes/js/jquery.dataTables.min.js"></script>
<script src="includes/js/datatables.js"></script>
<script>function funcao1(){ alert("Atenção ! todas as perguntas foram respondidas !"); } </script>
</head>
<body>
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" ></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
<?php
if ($total_perg == $total_resp) {
echo"<ul class='nav navbar-nav'>";
echo"<li><a href='#' onclick='funcao1()'>COMPORTAMENTO E HIGIENE PESSOAL <span class='glyphicon glyphicon-ok' aria-hidden='true'></span></a></li>";
echo"</ul>";
echo"</li>";
echo"</ul>";
} else {
echo"<ul class='nav navbar-nav'>";
echo"<li><a href='comportamento.php'>COMPORTAMENTO E HIGIENE PESSOAL</a></li>";
echo"</ul>";
echo"</li>";
echo"</ul>";
}
?>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
</div>
</nav>
<form class="form-horizontal" method="POST" action="perguntas.php">
<fieldset>
<legend>SELECIONE O QUESTIONÁRIO</legend>
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Selects</label>
<div class="col-lg-10">
<select class="form-control" name="sub_grupo" id="sub_grupo">
<option value="100"<?php echo ($_POST['sub_grupo'] == "100") ? " selected=\"selected\"" : ""; ?>>SETOR 1</option>
<option value="200">SETOR 2</option>
<option value="300">SETOR 3</option>
<option value="400">SETOR 4</option>
<option value="500">SETOR 5</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">RESPONDER</button>
</div>
</div>
</fieldset>
</form>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>SUBAREA</th>
<th>PERGUNTAS</th>
<th>RESPOSTAS</th>
</tr>
</thead>
<tbody>
<tr>
<td>COMPORTAMENTO E HIGIENE PESSOAL</td>
<td><?php echo"$total_perg"; ?></td>
<td><?php echo"$total_resp"; ?></td>
</tr>
<tr>
<td>ATENDIMENTO AO CLIENTE</td>
<td>5</td>
<td>7</td>
</tr>
<tr>
<td>ACONDICIONAMENTO</td>
<td>5</td>
<td>7</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
QUESTIONS.PHP:
<?php
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
include("conexao.php");
@session_start();
$id_usuario = $_SESSION['usu_id'];
$loja = $_SESSION['usu_loja_id'];
$area = $_SESSION['usu_setor'];
$quest = $_SESSION['quest'];
$_SESSION['sub_grupo'] = $_POST["sub_grupo"];
$sub_grupo = $_SESSION['sub_grupo'];
if(isset($_GET['submit'])){
$loja = $_GET ["loja"];
$id_usuario = $_GET ["id_usuario"];
$questionario = $_GET ["questionario"];
$area = $_GET ["area"];
$idpergunta = $_GET ["pergunta"];
$resp = $_GET ["resp"];
$datacad = $_GET ["data_cad"];
$nro_pergunta = $_GET ["nro_pergunta"];
$sql_select_insert = mysql_query("SELECT *
FROM respostas
WHERE
resp_loja='$loja'
AND resp_usuario='$id_usuario'
AND resp_questionario='$questionario'
AND resp_area ='$area'
AND resp_subgrupo='$sub_grupo'
AND resp_pergunta_id='$idpergunta'");
if(mysql_num_rows($sql_select_insert)==0) {
$sql_insert = mysql_query("INSERT INTO respostas
(resp_id,
resp_loja,
resp_usuario,
resp_questionario,
resp_area,
resp_subgrupo,
resp_pergunta_id,
resp_resposta,
resp_data,
resp_status)
VALUES
('',
'$loja',
'$id_usuario',
'$questionario',
'$area',
'$sub_grupo',
'$idpergunta',
'$resp',
'$datacad',
'1')")
or die (mysql_error());
mysql_query($sql_insert,$conexao);
}
} else {
}
$query_pergs = mysql_query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id ='$area'
AND sb.sub_area_cod='$sub_grupo'");
while ($row = mysql_fetch_array($query_pergs)) {
$totalpergunta = $row["TOTAL_PERG"];
}
$query_count_resp = mysql_query("SELECT
COUNT(resp_resposta) AS TOTAL
FROM respostas
WHERE resp_loja = '$loja '
AND resp_questionario = '$quest'
AND resp_area = '$area'
AND resp_subgrupo = '$sub_grupo'
AND resp_usuario NOT IN ( '37', '38', '39','40','1')");
while ($row = mysql_fetch_array($query_count_resp)) {
$total_resp = $row["TOTAL"];
}
if (isset($_GET['nro_pergunta'])) $nro_pergunta = $_GET['nro_pergunta'] + 1 ;
else $nro_pergunta = 1;
if ($total_resp == $totalpergunta) {
// echo '<script>alert(\'Todas as perguntas foram respondidas!\');parent.location =\'index.php\';</script>';
} else {
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>#</title>
<link rel="stylesheet" href="includes/css/style.css">
<link rel="stylesheet" href="includes/css/bootstrap.min.css">
<link rel="stylesheet" href="includes/css/datatables.css">
<script src="includes/js/jquery.min.js"></script>
<script src="includes/js/bootstrap.min.js"></script>
<script src="includes/js/jquery.dataTables.min.js"></script>
<script src="includes/js/datatables.js"></script>
<script language=javascript>
document.onkeydown = function () {
switch (event.keyCode) {
case 116 :
event.returnValue = false;
event.keyCode = 0;
return false;
case 82 :
if (event.ctrlKey) {
event.returnValue = false;
event.keyCode = 0;
return false;
}
}
}
</script>
</head>
<body>
<div class="container">
<?php
echo"$sub_grupo";
$query_count_totresp = mysql_query("SELECT
count(r.resp_id) AS TOTRESP
FROM
respostas AS r
WHERE
r.resp_status = 1 and
r.resp_loja = 2 and
r.resp_questionario = 1 and
r.resp_area = 100 and
r.resp_subgrupo = '$sub_grupo'") ;
while ($row = mysql_fetch_array($query_count_totresp)) {
$respondidas = $row["TOTRESP"];
};
$query_count = mysql_query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id='$area' AND sb.sub_area_cod='$sub_grupo'");
while ($row = mysql_fetch_array($query_count)) {
$totalpergunta = $row["TOTAL_PERG"];
}
$query_questionario = mysql_query("SELECT * FROM questionario WHERE quest_nome='$quest'");
while ($row = mysql_fetch_array($query_questionario)) {
$questionario = $row["quest_id"];
}
$query_pesquisa = mysql_query("SELECT
rp.id AS ORDEM,
a.area_id AS AREA,
sb.sub_area_cod AS SUBGRUPO,
sb.sub_area_nome AS DESC_SUBGRUPO,
p.perg_id AS ID_PEGUNTA,
p.perg_desc AS DESC_PERGUNTA,
rp.foto AS FOTO
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id='$area' AND sb.sub_area_cod='$sub_grupo' LIMIt $nro_pergunta ");
while ($row = mysql_fetch_array($query_pesquisa)) {
$ordem = $row["ORDEM"];
$area = $row["AREA"];
$subgrupo = $row["SUBGRUPO"];
$descsubgrupo = $row["DESC_SUBGRUPO"];
$idpergunta = $row["ID_PEGUNTA"];
$descpergunta = $row["DESC_PERGUNTA"];
$foto = $row["FOTO"];
}
?>
<div class="container-fluid">
<form class="form-horizontal" action="perguntas.php?nro_pergunta=<?php echo $nro_pergunta; ?>" method="GET">
<fieldset>
<div class="panel panel-danger"><div class="panel-heading"><h3 class="panel-title"></h3></div><div class="panel-body">
<?php
echo"<button id='button' type='next' name='next' class='btn btn-danger btn-sm'>PULAR</button> ";
$query_nav = mysql_query("SELECT
p.perg_id AS ID_PEGUNTA
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id='$area' AND sb.sub_area_cod='$sub_grupo' LIMIT $nro_pergunta ");
while ($row = mysql_fetch_array($query_nav)) {
$perg_id = $row["ID_PEGUNTA"];
$sub = 1;
$voltar=$perg_id - $sub;
echo"<a href='perguntas.php?nro_pergunta=$voltar' class='btn btn-danger btn-sm'>$perg_id</a> ";
}
?>
</div>
</div>
<legend><b>Pergunta <?php echo"$ordem";?> de <?php echo"$totalpergunta";?> | <?php echo"$descsubgrupo";?></b></legend>
<?php
$data_cad = date('Y-m-d H:i:s');
echo"<input type='hidden' name='loja' id='loja' value='$loja'>";
echo"<input type='hidden' name='id_usuario' id='id_usuario' value='$id_usuario'>";
echo"<input type='hidden' name='questionario' id='questionario' value='$questionario'>";
echo"<input type='hidden' name='area' id='area' value='$area'>";
echo"<input type='hidden' name='nro_pergunta' id='nro_pergunta' value='$nro_pergunta'>";
echo"<input type='hidden' name='data_cad' id='data_cad' value='$data_cad'>";
echo"<input type='hidden' name='pergunta' id='pergunta' value='$idpergunta'>";
?>
<?php
$sql_pergsn = mysql_query("SELECT * FROM respostas WHERE
resp_loja='$loja'
AND resp_usuario='$id_usuario'
AND resp_area ='$area'
AND resp_pergunta_id ='$idpergunta'
AND resp_subgrupo='$sub_grupo'
AND resp_questionario='$questionario'");
if(mysql_num_rows ($sql_pergsn) == 0 )
{
echo"<div class='form-group'>";
echo"<center><h4><label> $descpergunta</label></h4></center>";
echo"<div class='col-lg-10'>";
echo"<div class='radio'><label><input type='radio' name='resp' id='optionsRadios1' value='5'>OTIMO </label></div>";
echo"<div class='radio'><label><input type='radio' name='resp' id='optionsRadios2' value='3'>BOM </label></div>";
echo"<div class='radio'><label><input type='radio' name='resp' id='optionsRadios3' value='2'>REGULAR </label></div>";
echo"<div class='radio'><label><input type='radio' name='resp' id='optionsRadios3' value='1'>RUIM </label></div>";
echo"<div class='radio'><label><input checked type='radio' name='resp' id='optionsRadios4' value='0'>NAO APLICADO </label></div>";
echo"</div>";
echo"</div>";
echo"<center>";
echo"<div class='form-group'>";
echo"<div class='col-lg-10 col-lg-offset-2'>";
echo"<button id='button' type='submit' name='submit' class='btn btn-danger'><span class='glyphicon glyphicon-ok'></span> Responder</button> ";
echo"</div>";
echo"</div>";
} else {
echo"<div class='form-group'>";
echo"<div class='col-lg-10 col-lg-offset-2'>";
echo"<br>";
echo"<div class='alert alert-dismissible alert-danger'>";
echo"<strong>ATENÇÃO! </strong>Você já respondeu essa pergunta.";
echo"<button type='button' class='close' data-dismiss='alert'></button>";
echo"</div>";
echo"</div>";
echo"<button id='button' type='next' name='next' class='btn btn-danger'><span class='glyphicon glyphicon-circle-arrow-right'></span> Próxima</button><br />";
echo"</center>";
}
?>
</fieldset>
</form>
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"></span></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#" data-toggle="modal" data-target="#modalResp" ><span class='glyphicon glyphicon-floppy-saved'></span> Respondidas</a></li>
<li><a href="#" data-toggle="modal" data-target="#modalRegra"><span class='glyphicon glyphicon-question-sign'></span> Regra</a></li>
<li><a href="index.php"><span class='glyphicon glyphicon-remove-sign'></span> Sair</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
</div>
</nav>
<center><div class="panel panel-danger"><div class="panel-heading">#</div></div></center>
</div>
</div>
<div class="modal fade" id="modalResp">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Respostas</h4>
</div>
<div class="modal-body">
<table class="table table-bordered">
<?php
$query_modal = mysql_query("SELECT
p.perg_desc AS PERGUNTA,
r.resp_resposta AS RESPOSTA
FROM respostas AS r
INNER JOIN perguntas AS p ON r.resp_pergunta_id = p.perg_id
WHERE
resp_loja='$loja'
AND resp_usuario='$id_usuario'
AND resp_area ='$area'
AND resp_subgrupo='$sub_grupo'
AND resp_questionario='$questionario'")or die(mysql_error());
if (empty($query_modal)) {
echo "Nenhum registro encontrado.";
}
?>
<table class='datatable table table-hover table-bordered table-responsiv'>
<thead>
<tr>
<th>PERGUNTA</th>
<th>RESPOSTA</th>
</tr>
</thead>
<?php
echo"<tbody>";
while ($row = mysql_fetch_array($query_modal)) {
echo" <tr>";
echo"<td>".$row['PERGUNTA']."</td>";
echo"<td >".$row['RESPOSTA']."</td>";
echo" </tr>";
}
echo" </tbody>";
echo" </table>";
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="modalRegra">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Regra de pontuação</h4>
</div>
<div class="modal-body">
<table class="table table-bordered">
<thead>
<tr>
<th>Descrição</th>
<th>Nota</th>
<th>Ocorrencias</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ótimo</td>
<td>5</td>
<td>Sem ocorrências </td>
</tr>
</tbody>
<tbody>
<tr>
<td>Regular</td>
<td>3</td>
<td>02 ocorrências</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Ruim</td>
<td>1</td>
<td>03 ocorrências</td>
</tr>
</tbody>
<tbody>
<tr>
<td> N/A</td>
<td>Não se aplica</td>
<td>Não se aplica</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('.datatable').dataTable({
"sPaginationType": "bs_full"
});
$('.datatable').each(function(){
var datatable = $(this);
// SEARCH - Add the placeholder for Search and Turn this into in-line form control
var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
search_input.attr('placeholder', 'Search');
search_input.addClass('form-control input-sm');
// LENGTH - Inline-Form control
var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
length_sel.addClass('form-control input-sm');
});
});
</script>
</body>
</html>
Mais alguma dica?