Error with check boxes in php - php

This is my PHP & HTML code, it all works fine, except it gives me some error as "Notice: Undefined index: attend in C:\xampp\htdocs\ams\postattentry.php on line 43"
<?php
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("index.php");
exit;
}
include('header.php');
?>
<div class="row-fluid sortable">
<div class="box span8">
<div class="box-header well" data-original-title>
<h2><i class="icon-edit"></i> Attendance Entry</h2>
</div>
<div class="box-content">
<?php
$hour=$_GET["hour"];
$date=$_GET["date"];
$year=$_GET["year"];
$sem=$_GET["sem"];
$sec=$_GET["sec"];
$dept=$_GET["dept"];
$bat=$_GET["bat"];
if((strpos($fgmembersite->UserAttrib(),"i")>0) and $fgmembersite->attentrycheck($hour,$date,$year,$sem,$sec,$dept,$bat,$fgmembersite->Userempid()))
{
$error = $fgmembersite->GetErrorMessage();
if(!empty($error))
{
echo '<div class="alert alert-info">';
echo $error;
echo '</div>';
}
if(!$fgmembersite->DBLogin())
{
die("Error Locating the Students Database.");
}
if($bat=="ALL")
{
$qryae = "Select * from $fgmembersite->studtblname where dept='$dept' and year='$year' and sem='$sem' and sec='$sec'";
}
else
{
$qryae = "Select * from $fgmembersite->studtblname where dept='$dept' and year='$year' and sem='$sem' and sec='$sec' and bat='$bat'";
}
if(!($uapae=mysql_query( $qryae ,$fgmembersite->connection)))
{
die("Error Locating the Students Database.");
}
echo '<table class="table table-striped table-bordered">';
echo '<thead>';
echo '<tr>';
echo '<td align="center">S.No.</td>';
echo '<td align="center">Roll No.</td>';
echo '<td>Student Name</td>';
echo '<td align="center">Attendance</td>';
echo '</tr>';
echo '</thead>';
echo '<form id="attentry" class="form-horizontal" action="postattentry.php" method="post">';
echo '<tbody>';
$i=0;
$j=1;
while ($uapres = mysql_fetch_array($uapae)) {
echo '<tr>';
echo "<td>{$j}</td>";
echo "<td>{$uapres['rollno']}<input type='hidden' name='rollno[$i]' value='{$uapres['rollno']}' /></td>";
echo "<td>{$uapres['sname']}</td>";
echo "<td><div class='control-group'>
<div class='controls'>
<label class='checkbox'>
<input type='checkbox' name='attend[$i]' id='attend[$i]' value='1' checked=''/>
</label>
</div>
</div></td>";
echo '</tr>';
++$i;
++$j;
}
echo '<tr>';
echo '<td align="center">Total</td>';
echo '<td align="center">Hour</td>';
echo '<td>Date</td>';
echo '<td align="center">Year</td>';
echo '</tr>';
echo '<tr>';
echo "<td>{$i}<input type='hidden' name='fcount' id='fcount' value='{$i}'/></td>";
echo "<td>{$hour}<input type='hidden' name='fhour' id='fhour' value='{$hour}'/></td>";
echo "<td>{$date}<input type='hidden' name='fdate' id='fdate' value='{$date}'/></td>";
echo "<td>{$year}<input type='hidden' name='fyear' id='fyear' value='{$year}'/></td>";
echo '</tr>';
echo '<tr>';
echo '<td align="center">Dept.</td>';
echo '<td align="center">Sem</td>';
echo '<td>Section</td>';
echo '<td align="center">Batch</td>';
echo '</tr>';
echo '<tr>';
echo "<td>{$dept}<input type='hidden' name='fdept' id='fdept' value='{$dept}'/></td>";
echo "<td>{$sem}<input type='hidden' name='fsem' id='fsem' value='{$sem}'/></td>";
echo "<td>{$sec}<input type='hidden' name='fsec' id='fsec' value='{$sec}'/></td>";
echo "<td>{$bat}<input type='hidden' name='fbat' id='fbat' value='{$bat}'/></td>";
echo '</tr>';
echo '<tr>';
echo "<td></td>";
echo "<td><button type='submit' name='submit' value='Submit' class='btn btn-primary'>Register</button></td>";
echo "<td><button type='reset' class='btn'>Reset</button></td>";
echo "<td></td>";
echo '</tr>';
echo '</tbody>';
echo '</form>';
echo '</table>';
}?>
</div>
</div><!--/span-->
</div><!--/row-->
<?php include('footer.php'); ?>
The processing form code is...
<?php
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("index.php");
exit;
}
include('header.php');
?>
<div class="row-fluid sortable">
<div class="box span8">
<div class="box-header well" data-original-title>
<h2><i class="icon-edit"></i> Post Attendance Entry</h2>
</div>
<div class="box-content">
<?php
echo $hour=$_POST['fhour'];
echo $date=$_POST['fdate'];
echo $year=$_POST['fyear'];
echo $sem=$_POST['fsem'];
echo $sec=$_POST['fsec'];
echo $dept=$_POST['fdept'];
echo $bat=$_POST['fbat'];
echo $count=$_POST['fcount'];
if(!$fgmembersite->DBLogin())
{
die("Error Locating the Students Database.");
}
if(!$fgmembersite->EnsureAttTable())
{
die("Unable to Create Attendance Table.");
}
$attvars = array();
$i = 0;
while ($i < $count) {
echo $rollno= $_POST['rollno'][$i];
echo $attend= $_POST['attend'][$i];
++$i; }
?>
</div>
</div><!--/span-->
</div><!--/row-->
<?php include('footer.php'); ?>
I tried the same code by changing input type from the checkbox to text, it worked fine. I was unable to figure out where the problem is.

In a form, if a checkbox is not checked, it doesn't get posted.
change:
echo $attend= $_POST['attend'][$i];
to:
if(isset($_POST['attend']) && isset($_POST['attend'][$i]){
echo $attend =$_POST['attend'][$i]
} else {
echo $attend =0; //or whatever value you wish
}

If a checkbox is not checked, it is not sent at all as a POST variable.
In order to identidity if a checkbox is not checked, you must do a logic step.
if (isset($_POST[attend[$i]])){
{
$attended=1;
}else{
$attended=0;
}
I am officially too slow.

Related

How do i send a variable to a href='#' popup when button is pressed

This is where loop and see if there are no vehicles then a button shows up which then should send the customer ID to a popup.
else
{
global $p;
$p++;
echo '<form>';
echo '<tr>';
echo '<td colspan = "8">';
$cust = $row['ID'];
$cust .= $p;
echo '<a class="button" href="#add_veh?'.$cust.'" style="height: 100%">Add Vehicle '.$cust.'"</a>';
echo '</td>';
echo '</tr>';
echo '</form>';
}
I cannot send the customer ID to the popup. and also the popup does not work href='#
echo '<div id="add_veh" class="overlay">';
echo '<div class="popup">';
echo '<h3 class="monospace" align="center">Add a Vehicle</h3>';
echo '<a class="close" href="#">×</a>';
echo '<div class="content">';
echo '<form action="" method="POST">';
echo '<table class="monospace" style="width:100%" id="vehicle_entry" border="0">';
echo '<tbody>';
echo '<tr>';
echo '<td>'.$cust.'</td>';

PHP while loop problem, is not creating a new row

i try to get some data from a database and put it in a table.
the problem is the loop, the second row is not going to the new row, is continuing in same row. I checked the code and I can not see the problem.
Infact in the database i have 3 row and in the website is geting just 2 of them and booth in same row.
Can anybody help me?
<h1>view database</h1>
<div class="container">
<div class="page-header">
<h1>Shop list</h1>
</div>
<?php
$query = "SELECT shop_name, shop_phone_number, shop_email, shop_address_no, shop_address_street, shop_town, shop_county, shop_postcode, shop_address_country FROM shop";
$stmt = $conn->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
//button to add new shop
echo "<a href='index.php?page=shop' class='btn btn-primary m-b-1em'>Add new shop</a>";
if($num>0){
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Phone</th>";
echo "<th>Email</th>";
echo "<th>Address no/name</th>";
echo "<th>Street</th>";
echo "<th>Town</th>";
echo "<th>County</th>";
echo "<th>Post Code</th>";
echo "<th>Country</th>";
echo "<th>Action</th>";
echo "</tr>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$shop_name}</td>";
echo "<td>{$shop_phone_number}</td>";
echo "<td>{$shop_email}</td>";
echo "<td>{$shop_address_no}</td>";
echo "<td>{$shop_address_street}</td>";
echo "<td>{$shop_town}</td>";
echo "<td>{$shop_county}</td>";
echo "<td>{$shop_postcode}</td>";
echo "<td>{$shop_address_country}</td>";
echo "<td>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-info m-r-1em'>Read</a>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-primary m-r-1em' >Edit</a>";
echo "<a href='#' onclick='delete_user({$shop_name})' class='btn btn-danger>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "<div class='alert alert-danger'>No Records found.</div>";
}
?>
</div>
?>

how to get multiple checkbox value in "KOHANA FRAMEWORK"?

"KOHANA FRAMEWORK" submit button how to get selected check box value and how to insert in database.
My page has two fields textbox and second checkbox.
I trying to insert checkbox and textbox value but textbox value is possible to get but checkbox value is not.
=====> add.php
<?php echo form::open('backend/notifications/add/', array('enctype' => 'multipart/form-data')) ?>
<div class="staffWrapper">
<h2 style="float: left;">List Companies</h2>
<div class="clear"></div>
<table style="width:100%; table-layout:fixed;" border="0">
<tr>
<th width="20"><input type='checkbox' id='selectall' style='width:15px;'/></th>
<th width="210">Name</th>
<th width="100">Contact Person</th>
<th width="100">Phone</th>
<th width="210">Email</th>
<!-- <th width="80" colspan="2">Actions</th> -->
</tr>
<?php
if (count($companies)) {
$i = (isset($_GET['page']) ? ($_GET['page']-1)*50+1 : 1);
foreach ($companies as $company) {
echo $i % 2 ? '<tr class="even">' : '<tr class="odd">';
// echo "<td align='center'>" . $i . "</td>";
echo "<td align='center'>";
echo '<input type="checkbox" id="PILIH" name="PILIH[]" class="PILIH" value='.$company['id'].' style="width:15px;"/>';
echo "</td>";
echo "<td>" . $company['name'] . "</td>";
echo "<td>" . $company['contact_person'] . "</td>";
echo "<td>" . $company['phone'] . "</td>";
echo "<td>" . $company['email'] . "</td>";
$i++;
}
} else if (empty ($companies) && $searchKey){
echo '<tr class="odd">';
echo '<td colspan="7" align="center"><h3>No mathcing results were found for this search term.</h3></td>';
echo '</tr>';
} else if (empty ($companies)){
echo '<tr class="odd">';
echo '<td colspan="7" align="center"><h3>There are no companies.</h3></td>';
echo '</tr>';
}
?>
</table>
<?php echo $pagination; ?>
</div>
<div class="clear"></div>
<div style="float:right; margin-right: 335px; padding:10px 0 10px 0;">
<?php echo form::submit('', 'Create Notifications', array('class' => 'submit')) ?>
</div>
<div class="clear"></div>
<?php echo form::close(); ?>
In the same way as in pure PHP.
$PILIH = !empty($_POST['PILIH'])?$_POST['PILIH']:Array(); // pure PHP
$PILIH = Array::get($_POST, 'PILIH', Array());
$PILIH = $this->request->post('PILIH', Array()); //Where $this is Controller
$PILIH = Request::current()->post('PILIH', Array());
As a result, you will get an array from company-ID or an empty array, so:
$company_id = NULL;
$q = DB::query(Databse::INSERT, 'INSERT INTO foo (company_id) VALUES (:company_id)')->bind(':company_id', $company_id);
foreach($PILIH AS $company_id) {
$q->execute();
}
You did not give too much input, so: happy code analysis. And: RTFM.

Why my code doesn't loop in foreach

I want to update my database by using checkbox. But it seems like the code doesn't loop in a foreach. Where did I go wrong ? Is there have anything wrong with my foreach ?
<?php
foreach($_POST['listMenu'] as $checkBox1){
$strUpdateData = "
update tblMenuAkses
set MenuAkses = 'N'
where MenuAksesID = $checkBox1
";
$rsUpdateData = odbc_exec($dbconnVOT,$strUpdateData);
if($rsUpdateData){
echo "Success";
} else {
echo "ERROR <br>";
echo odbc_errormsg($dbconnVOT);
}
}
//It loops here
var_dump($_POST['listMenu']);
?>
<form method="post" action="skrinMenu.php" enctype="multipart/form-data">
<table align= "center" border="1" cellspacing="0" cellpadding="2">
<?php
$strgettable="select * from tblMenuAkses";
$varRecCount=0;
$rs=odbc_exec($dbconnVOT, $strgettable);
if($rs){
while (odbc_fetch_row($rs)) {// loop a table from database and checkbox too
$varRecCount++;
echo '<tr><td>';
echo '<center> '.$varRecCount.'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"UsrGrpNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuAkses").'</center>';
echo '</td>';
echo '<td>';
echo '<center> <input type="checkbox" name="listMenu[]" id="listMenu" value='.odbc_result($rs,"MenuAksesID").'> </center>';
echo '</td>';
echo '</td></tr>';
}
}
?>
</table>
<input type="submit" name="btnMenu" id="btnMenu" value = "Kemaskini">
</form>
The result of the var_dump($_POST['listMenu']) : NULL

link not redirecting to another page

i have a table where one column is for edit button the code for it is
echo "<td><input type='image' src='images/icn_edit.png' title='Edit'></td> ";
i don't know why but the link is not working. Can anyone tell me whats wrong with it
the code for entire table is
<form method="post" action="sendmail.php">
<div class="tab_container">
<div id="tab1" class="tab_content">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo "<table class='tablesorter' cellspacing='0'> ";
echo "<thead>
<tr>
<th></th>
<th></th>
<th>Shopname</th>
<th>Instagram account</th>
<th>Favourite</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>";
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
mysqli_data_seek($result, $i);
$data = mysqli_fetch_assoc($result);
echo "<tr " . $cls . ">";
echo '<td></td>';
?><td> <input name="checkbox[<?php echo $data['id']?>]" type="checkbox"> </td>
<?
echo '<td>' . $data['fullname_shopname'] . '</td>';
echo '<td>' . $data['username_instagramacnt'] . '</td>';
if($data['staffpick']=='yes')
{
echo '<td>Yes</td>';
}
else
{
echo '<td>No</td>';
}
echo "<td><input type='image' src='images/icn_edit.png' title='Edit'></td> ";
echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_vendor.php?id=".$data['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";
echo "</tr>";
}
echo "</table>";
echo '<div class="pagination" style="margin-left:300px;" >';
if ($total_pages > 1)
{
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul>
</div>";
?>
</div>
</div>
<footer>
<div class="submit_link">
<input type="submit" value="Send Mail" name="submit" class="alt_btn">
</div>
</footer>
</form>
If your <table> is wrapped in a <form> tag. Use an <img /> tag instead. Using <input type="image" /> will actually submit the form rather than to perform a link redirection.
echo "
<td>
<a href=\"a_add_fav_vendor.php?id=".$data['id']."\">
<img src=\"images/icn_edit.png\" title=\"Edit\"
</a>
</td>
";
Try this :
echo '<td><input type="image" src="images/icn_edit.png" title="Edit"></td>';
Do some Change like
echo "<td><a href='a_add_fav_vendor.php?id=".$data['id']."'><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

Categories