dynamic checkbox value access in another file in PHP - php

i am facing problem i am developing school system in php.
i want use checkbox dynamic for attendance but checkbox value i cant use in another file please help me
first file attendacne.php
<form name="abc" method="post" action="attendance-con.php">
<p>Date:
<input name="dat" type="text" class="tcal"/>
<input name="Save" type="submit" style="float:right 20ox;" value="Save Attendace" />
<input name="class_id" type="hidden" value="<?php echo $_REQUEST['class_id']; ?> " />
<?php
$a=0;
$qry=("SELECT DISTINCT
s.rollnum as rollnum,
s.std_name as name
FROM student s WHERE s.std_class=".$_REQUEST['class_id']."");
$result=mysql_query($qry);
while($row=mysql_fetch_array($result))
{
?>
<table width="100%" border="1" align="center" cellpadding="4" cellspacing="4" style="text-align:left;" id="stname">
<?php
if($a==0)
{ ?>
<tr>
<th width="117" height="39" bgcolor="#FFE1CC">Student RollNo</th>
<th bgcolor="#FFE1CC">Student Name</th>
<th width="100" bgcolor="#ECFAFF">Present</th>
</tr>
<?php
}?>
<?php
if($a==1)
{ ?>
<tr>
<th width="117" height="-5" bgcolor="#FFE1CC"></th>
<th bgcolor="#FFE1CC"></th>
<th width="100" bgcolor="#ECFAFF"></th>
</tr>
<?php
}
?>
<?php $a=1; ?>
<input type="hidden" name="rollnum[]" value="<?php echo $row['rollnum'];?>"/><tr>
<td>
<?php echo $row['rollnum'];?>
</td>
<td>
<?php echo $row['name'];?>
</td>
<td>
<input type="checkbox" name="stid[]" value="1">
</td>
</tr>
<?php }?>
</table>
</form>
second file attendance-con.php
<?php
include_once("include/config.php");
$i=0;
$Dat=$_POST['dat'];
$cid=$_POST['class_id'];
$sid=$_POST['stid'];
foreach($_POST['rollnum'] as $x)
{
if($sid[$i]=='1')
{
$query=mysql_query("INSERT INTO attendance (sid,cid,Date,status)
values('$x','$cid','$Dat','1')");
}
if($sid[$i]!='1')
{
$query=mysql_query("INSERT INTO attendance (sid,cid,Date,status)
values('$x','$cid','$Dat','0')");
}
$i=$i+1;
}
?>

<input type="checkbox" name="stid" value="1">
not
<input type="checkbox" name="stid[]" value="1">

Related

HTML form WON'T POST data ($_POST[] is empty)

One of my html forms is not POSTing data, the submit button respect the action attribute and redirect the page as spected BUT $_POST[] is empty for some reason. could sonebody help me find the issue here?
I already tried using html method POST and even ajax to process the form and submit the data, and theres no case. $_POST[] will allways stay empty (this is the only page that does that, I hava an exact copy of the form but without the GET method and it works fine...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="nocep-rules.css" rel="stylesheet" type="text/css">
<?php
session_start();
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxxxx");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
<?php
if(!isset($_SESSION['logged'])){
header("location: /bubale/login.php");
}
?>
</head>
<body>
<div class="header">
<img class="logo" src="/bubale/img/logo1.png" alt="" />
<img class="foro-title" src="/bubale/img/loja_title.png" alt="" />
</div>
<div class="menu">
<nav class="site-nav">
<ul>
<li>HOME</li>
<li>LOJA</li>
<li>FORUM</li>
<li>CHECKOUT</li>
<li>SAIR</li>
</ul>
</nav>
</div>
<div class="info">
<table width="100%">
<tr>
<td width="20%">
<h2 class="text">USUÁRIO:</h2>
<?php echo $_SESSION['logged']; ?>
<h2 class="text2">PONTOS:</h2>
<?php
$sql5= "Select points from profile where user = '".$_SESSION['logged']."'";
$result5 = mysqli_query($link, $sql5);
if(mysqli_num_rows($result5)==1){
while($row5 = mysqli_fetch_assoc($result5)) {
echo $row5['points'];
}
}
?>
</td>
<td width="30%">
<?php
$sql2= "select * from bolsinhas where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result2 = mysqli_query($link, $sql2);
$count = mysqli_num_rows($result2);
?>
<h2 class="text">BOLSINHAS:</h2>
<?php echo $count; ?>
<h2 class="text2">PRODUTOS:</h2>
<?php
$sql3= "select * from fullsize where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result3 = mysqli_query($link, $sql3);
$count3 = mysqli_num_rows($result3);
echo $count3;
?>
</td>
<td>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
}else{$cart_count = "0";}
?>
<img id="icon1" src="img/basket.png" title="Compras" ><span id = "count_wrapper"><a id="count">
<?php echo $cart_count; ?></a></span>
<img class="icon" src="img/sale.png" title="Promoção" alt="" >
<img class="icon" src="img/points.png" title="Fidelidade Buba'le" alt="" >
</td>
</table>
</div>
<h1 class="frete">FRETE:</h1>
<div class="wrapper_frete">
<?php
$rua1 = $_GET['1'];
$numero1 = $_GET['2'];
$apt1 = $_GET['3'];
$cep1 = $_GET['4'];
?>
<table id="forma_frete" width="100%">
<tbody>
<th>DADOS PESSOAIS</th>
<th>OOPS!!!</th>
<tr>
<form id="modform" method="POST" action="nocep2.php">
<td width="40%" class="td2">
<table id="nested" width="100%">
<tr>
<td id="td-nested-left" width="30%">
<label class="label-form" for="user">USUÁRIO:</label><br>
<label class="label-form" for="rua">RUA:</label><br>
<label class="label-form" for="numero">NÚMERO:</label><br>
<label class="label-form" for="apt">APT:</label><br>
<label class="label-form" for="cep">CEP:</label><br>
</td>
<td id="td-nested-right">
<input name="user" form="buttons" type="text" maxlenght="20" disabled value="<?php echo $_SESSION['logged']; ?>" /><br>
<input name="rua" form="buttons" type="text" value="<?php echo $rua1; ?>" /><br>
<input name="numero" form="buttons" type="text" value="<?php echo $numero1;?>" /><br>
<input name="apt" form="buttons" type="text" value="<?php echo $apt1; ?>" /><br>
<input name="cep" form="buttons" type="text" value="<?php echo $cep1;?>" />
</td>
</tr>
</table>
<input type="submit" class="refresh" value="ATUALIZAR">
</td>
</form>
<td id="td_mensage">
<h3 id="mensage">-irrelevant text-. </h3>
</td>
</tr>
<tr>
<td id="td_ob" colspan="2">
<h3 id="comentario">OBSERVAÇAÕ:</h3>
<textarea form="buttons" name="observacion" id="text_ob" placeholder="Deixe aqui sua observaçaõ..."></textarea>
</td>
</tr>
</tbody>
</table>
</div>
<h1 class="frete">SUA COMPRA:</h1>
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<?php } ?>
<table class="table">
<tbody>
<tr>
<th></th>
<th>PRODUTO</th>
<th>QUANTIDADE</th>
<th>VALOR UNITARIO</th>
<th>VALOR TOTAL</th>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td id="imgtd"><img src='<?php echo $product["image"]; ?>' width="90" height="80" /></td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
</form></td>
<td><form method='post' action=''>
<input type='hidden' name='code2' value="<?php echo $product["code"]; ?>" />
<?php echo $product["quantity"];?>
</form></td>
<td><?php echo "R$".$product["price"]; ?></td>
<td><?php echo "R$".$product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td class="td_tot" colspan="5" align="right"><strong id="total">TOTAL: <?php echo "R$".$total_price; ?></strong></td>
</tr>
</tbody>
</table>
<form method="post" action="contact-venta.php" id="buttons">
<?php
?>
<input type="hidden" name="compras" value="<?php echo print_r($_SESSION["shopping_cart"], TRUE); ?>"/>
<input type="hidden" name="total" value="<?php echo $total_price; ?>">
<input type="submit" id="submit" value="ENVIAR">
</form>
<?php mysqli_close($link); ?>
</body>
</html>
the issue is with the first submit (the one with the value ACTUALIZAR), the form will go to nocep2.php BUT $_POST[] will remain empty .... I posted the entire page just in case there is an issue I'm not seeing....
Your HTML is rather confused.
Firstly, <form> is not a valid element in <tr>. The browser is rendering them outside the table, and separate from your <input>. Move your form tags inside the <td>, or move them outside the <table>
Eg
<table>
<tr>
<td>
<form method="POST">
<input type="text" name="inputName">
<input type="submit">
</form>
</td>
</tr>
</table>
Or, if you need to spread your form over several cells:
<form method="POST">
<table>
<tr>
<td>
<input type="text" name="inputName1">
</td>
<td>
<input type="text" name="inputName2">
</td>
<td>
<input type="submit">
</td>
</tr>
</table>
</form>
Secondly, you're creating a number of <input> elements of type "hidden", each with their own form tags, so they are each part of a different form. Reorder your code so that the hidden elements appear within the same <form> as the rest of your <input>

Need to send an array in PHP

I'm trying to send multiples values to a PHP form.
This is the form that I use to send values:
<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
<table width="561" height="79" border="1">
<tr>
<td width="30%" height="32">ProductID</td>
<td width="30%" height="32">SKU</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_test1['ProductID']; ?>
<input
type="hidden"
name="Product[id][ProductID]"
value="<?php echo $row_test1['ProductID']; ?>"
/>
</td>
<td><?php echo $row_test1['SKU']; ?>
<input
type="hidden"
name="Product[id][SKU]"
value="<?php echo $row_test1['SKU']; ?>"
/>
</td>
</tr>
<?php } while ($row_test1 = mysqli_fetch_assoc($test1)); ?>
</table>
<input type="submit" value="Update" name="Facturar">
</form>
And this is the action file:
if(isset($_POST['Update']))
{
$ProductID=$_POST['Product'];
print_r(json_encode($ProductID));
}
The problem that I have is when I send multiple values, for example the below table:
ProductID SKU
103 WH004BI
137 VO007BI
I alway get this result:
{"id":{"ProductID":"137","SKU":"VO007BI"}}
When I actually want to get a result like this:
{"id":[{"ProductID":"103","SKU":"WH004BI"},{"ProductID":"137","SKU":"VO007BI"}]}
You're going to want to do something like this:
<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
<table width="561" height="79" border="1">
<tr>
<td width="30%" height="32">ProductID</td>
<td width="30%" height="32">SKU</td>
</tr>
<?php $i = 0; ?>
<?php while ($row_test1 = mysqli_fetch_assoc($test1) { ?>
<tr>
<td>
<?php echo $row_test1['ProductID']; ?>
<input
type="hidden"
name="Product[id][<?= $i; ?>][ProductID]"
value="<?php echo $row_test1['ProductID']; ?>"
/>
</td>
<td>
<?php echo $row_test1['SKU']; ?>
<input
type="hidden"
name="Product[id][<?= $i; ?>][SKU]"
value="<?php echo $row_test1['SKU']; ?>"
/>
</td>
</tr>
<?php $i++; ?>
<?php } ?>
</table>
<input type="submit" value="Update" name="Facturar">
</form>
Note that I have put a $i = 0 at the start of the loop, and $i++ at the end of the loop.
Additionally, I have changed the names to the following:
name="Product[id][<?= $i; ?>][SKU]"
Which will prevent the issue you were having in the comment section regarding an ill-formed array.

display one row of while loop

I'm making a quiz. I'm getting all the questions on one page, but I don't want that. I only want the first question. After the button is clicked, then i want the second question. How can I accomplish this? Here's my code:
$qry=mysql_query("select `id`,`question` from `paper_details` where `paper_id`='$id'") or die(mysql_error());
$num_rw= mysql_num_rows($qry);
while($f=mysql_fetch_assoc($qry))
{
$q2=mysql_query("select `id`,`que`,`unique_id`,`image` from `que_master` where `id`='".$f['question']."'") or die(mysql_error());
$f2=mysql_fetch_row($q2);
$i=$pid;?>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="4" class="form_border" id="form_table">
<tr>
<th width="3%" rowspan="2" align="left" valign="top" class="form_label"></th>
<th width="5%" height="27" align="left" valign="top" class="form_label">Que<?php echo $i;?> : </th>
<th width="92%" align="left" valign="top" class="form_label"> <?php echo $f2[1]; if($f2[3]!=""){?>
<img src="admin/que_image/<?php echo $f2[3];?>" border="noborder" width="20px" height="20px"/><?php }?>
<input type="hidden" id="que_id" name="que_id" value="<?php echo $f2[0];?>"/>
</th>
</tr>
<tr>
<th height="78" align="left" valign="top" class="form_label">Ans : </th>
<td align="left" valign="top" style="line-height:2.0em;">
<input type="hidden" id="id" name="id" value="<?php echo $id;?>"/>
<input type="hidden" id="pid" name="pid" value="<?php echo $pid+1;?>"/>
<input type="hidden" id="num_raw" name="num_raw" value="<?php echo $num_rw;?>"/>
<input type="hidden" id="time" name="time" value="<?php echo $time;?>" />
<input type="hidden" id="que_id" name="que_id" value="<?php echo $f2[0];?>" />
<?php
$q3=mysql_query("select `id`,`ans`,`ans_img` from `ans_master` where `unique_id`='$f2[2]'") or die(mysql_error());
while($f3=mysql_fetch_row($q3)){
?>
<input type="radio" value="<?php echo $f3[0]; ?>" name="ans" id="ans"><?php echo $f3[1]; ?><?php if($f3[2]!=""){?>
<img src="admin/ans_image/"<?php echo $f3[2];?>" border="noborder" width="20px" height="20px"/>
<?php }
}}
?>
</td>
</tr>
<tr>
<th height="33" align="left" valign="middle" class="form_label"> </th>
<th colspan="2" align="left" valign="middle" class="form_label">
<input type="submit" name="Submit" value="Next >" />
Please help me.
If I understand correctly, you want to have a single-page application to run your quiz.
For this purpose, you could use ajax requests with jquery.
Or else, you can make a separate page for each question.

how to save radio button has been selected at the time the page is reloaded?

how to save radio button has been selected at the time the page is reloaded? suppose I have selected some of the radio button and then reload the browser page. but after the page is reloaded radio buttons have been still the same. I use Codeigniter.
here is my view code
<div class="container">
<?php
$no=1;
foreach($hasil->result() as $row):
?>
<form action="<?php echo base_url();?>mahasiswa/hasil" method="post" class="form" enctype="multipart/form-data" name="form" onsubmit="stopCounter();">
<input type="hidden" name="id_soal[<?php echo $row->id_soal;?>]" value="<?php echo $row->id_soal;?>" />
<table>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><b><?php echo $no.'. ';?></b></td>
<td><?php echo $row->pertanyaan;?></td>
</tr>
<tr>
<td rowspan="5"> </td>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="A" />a. <?php echo $row->a;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="B" />b. <?php echo $row->b;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="C" />c. <?php echo $row->c;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="D" />d. <?php echo $row->d;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="E"/>e. <?php echo $row->e;?></label></span></td>
</tr>
<?php
$no++;
?>
<input type="hidden" name="id_sesi" value="<?php echo $row->id_sesi;?>" />
<input type="hidden" name="jumlah" value="<?php echo $jumlah;?>" />
<?php
endforeach;
?>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Selesai" class="btn btn-default"/></td>
</tr>
</table>
</form>
</div>
here my controller code
public function mulai_tes(){
$id=$this->uri->segment(3);
$this->load->model('m_mahasiswa');
$result=$this->m_mahasiswa->data_mahasiswa();
foreach($result->result() as $row):
$data['user']=$row->username;
$data['nama']=$row->nama;
endforeach;
$cek=$this->m_mahasiswa->validasi_tes($id,$data['user']);
foreach($cek->result() as $c){
if($c->id_sesi==$id){ ?>
<script type="text/javascript" language="javascript">
alert("Anda telah mengikuti tes soal ini");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."mahasiswa/tes'>";
}
}
$data['hasil']=$this->m_mahasiswa->mulaites($id);
$data['jumlah']=$data['hasil']->num_rows();
$data['judul']='Mulai Tes';
$this->load->view('elearning/template',$data);
}
How I set cookie and where I can put cookie code in controller?
Check which radio(s) was selected in controler (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html).
Pass this data to view (http://ellislab.com/codeigniter/user-guide/general/views.html).
Mark radio(s) as selected within view depending on data passed by controller (Assign an initial value to radio button as checked).

Post variable is set but shows undefined index error

I want to select a row from MySQL database which there are multiple rows.
I've chosen radio button for selection and I've put id of that row in radio button value.
The problem is the posted radio button is set and when I echo it as you can see in the code it shows me the right id.
When I want to select from database with MySQL query it gives me and index undefined error, the strange point is when I select a table that contains just one row this code is working but when number of rows are more than one I am not able to select the chosen row.
Table code:
<?
if($_SESSION["count"]!=0 || $_SESSION["count"]!="")
{
?>
<!-- begining of levels informations -->
<form name="form2" method="POST" dir="rtl" action="" style="font-family:'B_yekan';">
<div align="center" width="900" >
<table class="styled-table" cellspacing="0" width="900" border="1">
<tr>
<th width="10" scope="col" ></th>
<th width="60" scope="col">level</th>
<th width="60" scope="col">date</th>
<th width="60" scope="col">time</th>
<th width="54" scope="col">price</th>
<th width="60" scope="col">mark</th>
</tr>
<?php
$id = array();
while($rows=mysql_fetch_array($result)){
$id[]=$rows['id'];
?>
<tr>
<td><input class="styled-input" type="hidden" name="id[]" id="id" value= "<? echo $rows['id']; ?>" /></td>
<td><input class="styled-input" type="text" name="lev" id="lev" value="<? echo $tbl_name; ?>" /></td>
<td><input class="styled-input" type="text" name="date[]" id="date" value="<? if($rows['date']=="1"){echo "even";}else{echo "odd";}?>" /></td>
<td><input class="styled-input" type="text" name="time[]" id="time"
value="<?if($rows['time']=="pm1"){ echo"16 - 17:30";}
elseif($rows['time']=="pm2"){ echo "17:45 - 19:15";}
elseif($rows['time']=="pm3"){echo "19:30 - 21";}?>" />
</td>
<td><input class="styled-input" type="text" name="price[]" id="price" value= "<? echo $rows['price']; ?>" /></td>
<td><input class="styled-input" style="padding: 5px;width:20px" type="radio" name="mark[]" id="mark" value="<? echo $rows['id']; ?>" /></td>
</tr>
<?php
}//end of loop
?>
</table>
</div>
<input class="styled-button-8" type="Submit" value="firstchoose" name="firstchoose" />
<?}//end of if check for count?>
</form>
Select code:
<!-- first choose level -->
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['firstchoose']) && $_POST['firstchoose'] == 'firstchoose')
{
$tbl_name=$_POST['lev'];
for($i=0;$i<$_SESSION["count"];$i++)
{
$checked =mysql_real_escape_string($_REQUEST['mark'][$i]);
echo $checked;
$sql2="SELECT * FROM $tbl_name WHERE `id`='".mysql_real_escape_string($checked)."' ";
$result2=mysql_query($sql2);
$data = mysql_fetch_assoc($result2);
$count2=mysql_num_rows($result2);
}
if(isset($result2)){
?>
<script language="javascript">alert('the level is selected successfully.'); </script>
<?php
}
}
?>
<!-- end of first choose level -->

Categories