Add Data After Adding Table Rows Dynamically Into Table with PHP - php

Kindly can anyone help per below concept.
I need to post data into a table after successful adding dynamic table row
Below pages is working fine but when i try to post those data into table am getting error message on results5.php line 11 error:Warning: Invalid argument supplied for foreach()
resultskati.php
<head>
<script>
function addRows()
{
var a_name=document.form1.a_name.value;
var a_in=document.form1.a_in.value;
var tbl = document.getElementById('t');
var lastRow = tbl.rows.length;
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
var cell1 = row.insertCell(0);
var e1 = document.createElement('input');
s=lastRow+1;
e1.type = 'text';
e1.size=1;
e1.readonly="readonly";
e1.name = 'sno';
e1.value = iteration-1;
e1.id = 'txtRow' + iteration;
cell1.appendChild(e1);
//another cell
var cellRightSel = row.insertCell(1);
var sel = document.createElement('select');
sel.name = 'a_name[]';
var rs=document.forms.form2.rws.value;
var j=0;
for(j=0;j<=rs;++j)
{
sel.options[j] = new Option(document.forms.form2.elements[j].value,document.forms.form2.elements[j].name);
if(document.forms.form2.elements[j].value==a_name)
{
sel.options[j].selected=true;
}
}
cellRightSel.appendChild(sel);
var cell2 = row.insertCell(2);
var e2 = document.createElement('input');
//s=lastRow+1;
e2.type = 'text';
e2.size=4;
e2.value=a_in;
e2.name = 'a_in[]';
e2.id = 'txtRow' + iteration;
cell2.appendChild(e2);
}
function removeRows()
{
var tbl = document.getElementById('t');
var lastRow = tbl.rows.length;
if (lastRow > 3) tbl.deleteRow(lastRow - 1);
}
function sbmt()
{
frmco.action="";
frmclose.submit();
}
</script>
<script language="javascript" type="text/javascript">
function Validate()
{
if(""==document.form7.edleveid1.value)
{
alert("Select Education Level");
document.form7.edleveid1.focus();
return false;
}
if(""==document.form7.tenure.value)
{
alert("Select currently tenure student belong to");
document.form7.tenure.focus();
return false;
}
else
{
window.location="results.php";
}
}
function Validate1()
{
if(""==document.form7.classid.value)
{
alert("Select class where student joined");
document.form7.classid.focus();
return false;
}
else
{
window.location="results.php";
}
}
function Validate2()
{
if(""==document.form7.examtype.value)
{
alert("Select exam type");
document.form7.examtype.focus();
return false;
}
if(""==document.form7.subjectid.value)
{
alert("Select subject");
document.form7.subjectid.focus();
return false;
}
else
{
window.location="results.php";
}
}
</script>
<script type="text/javascript" src="accodation/jquery-ui.js"></script>
<script type="text/javascript" src="Datepicker/jquery-1.9.js"></script>
<script type="text/javascript" src="Datepicker/jquery-ui.js"></script>
<link href="Datepicker/jquery-ui.css" rel="stylesheet" type="text/css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
</head>
<link href="css/all.css" rel="stylesheet" type="text/css" />
<div id="center-column">
<div class="top-bar">
<h1>Students</h1>
<div class="breadcrumbs">Homepage / Students</div>
</div><br />
<?php include('searchstudents.php'); ?>
<div class="table">
<form method="GET" name="form7" class="contentArea" id="form7" action="results.php">
<img src="img/bg-th-left.gif" width="8" height="7" alt="" class="left" />
<img src="img/bg-th-right.gif" width="7" height="7" alt="" class="right" />
<table class="listing" cellpadding="0" cellspacing="0" id="tb" bgcolor="#D6D6D6">
<tr>
<th class="full" colspan="8">UPLOAD STUDENT'S RESULTS</th>
</tr>
<!--Start of default menu -->
<tr>
<td class="first" width="10"><strong>EdLevel</strong></td>
<td class="first" colspan="7">
<select name="edleveid1" id="edleveid1">
<option value="">Select Educational Level.....</option>
<?php
include("connect.php.inc");
$sq11="select TEdlevelID,Name from streams where IsActive=1 and schoolID='".$_SESSION['schoolID']."' ";
$rs11=mysql_query($sq11) or die(mysql_query());
while($row11=mysql_fetch_array($rs11))
{
$TEdlevelID=$row11['TEdlevelID'];
$name=$row11['Name'];
echo("<option value=\"".$TEdlevelID." \">".$name."</option>");
}
?>
</select>
<select name="tenure" id="tenure">
<option value="">Select Current Student Tenure</option>
<option value="1">1 Yr</option>
<option value="2">2 Yrs</option>
<option value="3">3 Yrs</option>
<option value="4">4 Yrs</option>
<option value="5">5 Yrs</option>
</select>
<input type="submit" name="next1" value="Next" class="button" onclick="return Validate()" />
</td>
</tr>
<!--End of default input field selection -->
<!--start of selected details-->
<?php
if(isset($_GET['next1']))
{
$TEdlevelID1=$_GET['edleveid1'];
$tenure=$_GET['tenure'];
?>
<tr>
<td class="first" width="10"><strong>Class</strong></td>
<td class="first" colspan="7">
<select name="classid" id="classid">
<option value="">Select Class...</option>
<?php
include("connect.php.inc");
$sq11="select c.ClassID,c.Name from streams s,classes c where s.IsActive=1 and c.IsActive=1 and s.TEdlevelID=c.EdTlevel and s.TEdlevelID='$TEdlevelID1' and s.SchoolID='".$_SESSION['schoolID']."' and c.schoolID='".$_SESSION['schoolID']."'";
$rs11=mysql_query($sq11) or die(mysql_query());
while($row11=mysql_fetch_array($rs11))
{
$ClassID=$row11['ClassID'];
$name=$row11['Name'];
echo("<option value=\"".$ClassID." \">".$name."</option>");
}
?>
</select>
<input type="hidden" name="eduIDnenda" value="<?php echo($TEdlevelID1);?>"/>
<input type="hidden" name="mda" value="<?php echo($tenure);?>"/>
<input type="submit" name="next2" value="Next" class="button" onclick="return Validate1()"/><input type="submit" name="back" value="Back" class="button" />
</td>
</tr>
<?php
}
?>
<!--<--End start of selected details-->
<!--getting exam type -->
<?php
if(isset($_GET['next2']))
{
$classido=$_GET['classid'];
$edulevo=$_GET['eduIDnenda'];
$tenurep=$_GET['mda'];
?>
<tr>
<td class="first" width="10"><strong>Exam Type</strong></td>
<td class="first" colspan="7">
<select name="examtype" id="examtype">
<option value="">Select Exam Type</option>
<?php
include("connect.php.inc");
$sq11="select e.ExamID,e.Name from examtypescore e,classes s where e.IsActive=1 and s.IsActive=1 and e.ClassID=s.ClassID and s.SchoolID='".$_SESSION['schoolID']."' and e.schoolID='".$_SESSION['schoolID']."' and e.ClassID='$classido'";
$rs11=mysql_query($sq11) or die(mysql_query());
while($row11=mysql_fetch_array($rs11))
{
$ExamID=$row11['ExamID'];
$ainamtihani=$row11['Name'];
echo("<option value=\"".$ExamID." \">".$ainamtihani."</option>");
}
?>
</select>
<strong>Subject</strong>
<select name="subjectid" id="subjectid">
<option value="">Select Subject</option>
<?php
include("connect.php.inc");
$sq11="select s.SubjectName,s.SubjectID from subjects s,streams se where s.SchoolID='".$_SESSION['schoolID']."' and se.schoolID='".$_SESSION['schoolID']."' and s.TEdLevelID='$edulevo' and s.TEdLevelID=se.TEdLevelID";
$rs11=mysql_query($sq11) or die(mysql_query());
while($row11=mysql_fetch_array($rs11))
{
$SubjectID=$row11['SubjectID'];
$SubjectName=$row11['SubjectName'];
echo("<option value=\"".$SubjectID." \">".$SubjectName."</option>");
}
?>
</select>
<input type="hidden" name="classidnenda" value="<?php echo($classido);?>"/><input type="hidden" name="elimu" value="<?php echo($edulevo);?>"/><input type="hidden" name="somo" value="<?php echo($SubjectName);?>"/><input type="hidden" name="aina" value="<?php echo($ExamID);?>"/>
<input type="hidden" name="mdaa" value="<?php echo($tenurep);?>"/>
<input type="submit" name="next3" value="Start" class="button" onclick="return Validate2()"/><input type="submit" name="back" value="Back" class="button" />
</td>
</tr>
<?php
}
?>
<!--end getting exam type -->
</form>
<form method="GET" name="form1" class="contentArea" id="form1" action="results5.php">
<!-- starting input data -->
<?php
if(isset($_GET['next3']))
{
$classid=$_GET['classidnenda'];
$elimulevo=$_GET['elimu'];
$examid=$_GET['aina'];
$somo=$_GET['subjectid'];
$tenure1=$_GET['mdaa'];
//$mdakamili=RIGHT($tenure1,1);
$sq113="select Name from streams where IsActive=1 and schoolID='".$_SESSION['schoolID']."' and TEdlevelID='$elimulevo'";
$rs113=mysql_query($sq113) or die(mysql_query());
while($row113=mysql_fetch_array($rs113))
{
$a=$row113['Name'];
}
$sq1131="select Name from examtypescore where IsActive=1 and schoolID='".$_SESSION['schoolID']."' and ExamID='$examid'";
$rs1131=mysql_query($sq1131) or die(mysql_query());
while($row1131=mysql_fetch_array($rs1131))
{
$c=$row1131['Name'];
}
$sq11312="select Name from classes where IsActive=1 and schoolID='".$_SESSION['schoolID']."' and ClassID='$classid'";
$rs11312=mysql_query($sq11312) or die(mysql_query());
while($row11312=mysql_fetch_array($rs11312))
{
$b=$row11312['Name'];
}
$sq11312="select SubjectName from subjects where IsActive=1 and schoolID='".$_SESSION['schoolID']."' and SubjectID='$somo'";
$rs11312=mysql_query($sq11312) or die(mysql_query());
while($row11312=mysql_fetch_array($rs11312))
{
$d=$row11312['SubjectName'];
}
?>
<tr>
<td class="first" width="10"><strong>EdLevel</strong></td>
<td class="first" ><?php echo($a);?><input type="hidden" class="text" size="10" name="elimuyake"value="<?php echo($elimulevo);?>"/></td>
<td class="first" width="10"><strong>Class</strong></td>
<td class="first" ><?php echo($b);?> yrs:(<?php echo($tenure1); ?>)<input type="hidden" class="text" name="darasa"size="10" value="<?php echo($classid);?>" /></td>
<td class="first" width="10"><strong>ExamType</strong></td>
<td class="first" ><?php echo($c);?><input type="hidden" class="text" size="10" name="ainamtihani"value="<?php echo($examid);?>" /></td>
<td class="first" width="10"><strong>Subject</strong></td>
<td class="first" ><?php echo($d);?><input type="hidden" name="somolake" class="text" size="10" value="<?php echo($somo);?>" /></td>
</tr>
</table>
<table class="listing" cellpadding="0" cellspacing="0" id="t" bgcolor="#D6D6D6">
<td class="first" width="10">
<input type="text" name="sn" value="1" readonly="" size="1" /></td>
<td class="first" colspan="3">
<select name="a_name" id="a_name">
<option value=""></option>
<?php
include("connect.php.inc");
$sql="SELECT s.Lname, s.Mname, s.Fname, s.studentID,s.ey,s.sy, s.ClassID, RIGHT( s.ey - s.sy, 1 ) AS yrs, str.tenure, s.schoolID, cl.EdTlevel, s.IsActive, cl.ClassID
FROM students s, streams str, classes cl
GROUP BY s.StudentID
HAVING s.schoolID ='".$_SESSION['schoolID']."'
AND s.ClassID ='$classid' and s.IsActive=1 and RIGHT( s.ey - s.sy, 1 )='$tenure1'";
$resultsx=mysql_query($sql) or die(mysql_error());
while($row11=mysql_fetch_array($resultsx))
{
$studentID=$row11['studentID'];
$ClassID=$row11['ClassID'];
$yrs=$row11['yrs'];
$Fname=$row11['Fname'];
$Mname=$row11['Mname'];
$Lname=$row11['Lname'];
echo("<option value=\"".$studentID." \">".$Fname.' '.$Mname.' '.$Lname."</option>");
}
?>
</select> <strong>Marks</strong>
<input type="text" class="text" name="a_in" size="4"/></td>
</tr>
<tr>
<td class="first" colspan="4" align="right">
<input name="Submit2" type="reset" class="button" id="Submit2" value="RESET">
<input name="Submit3" type="button" class="button" id="Submit3" value="ADD ROW" onClick="addRows()">
<input name="Submit4" type="button" class="button" id="Submit4" value="DELETE ROW" onClick="removeRows()">
<input name="save" type="submit" class="button" id="save" value="SAVE">
</td>
</tr>
<?php
}
?>
</table>
</div>
</form>
<form name="form2" class="contentArea">
<?php
if(isset($_GET['next3']))
{
error_reporting (E_ALL ^ E_NOTICE);
include("connect.php.inc");
$sq11="SELECT s.Lname, s.Mname, s.Fname, s.ey,s.sy,s.studentID, s.ClassID, RIGHT( s.ey - s.sy, 1 ) AS yrs, str.tenure, s.schoolID, cl.EdTlevel, s.IsActive, cl.ClassID
FROM students s, streams str, classes cl
GROUP BY s.StudentID
HAVING s.schoolID ='".$_SESSION['schoolID']."'
AND s.ClassID ='$classid' and s.IsActive=1 and RIGHT( s.ey - s.sy, 1 )='$tenure1'";
$resultsx=mysql_query($sql) or die(mysql_error());
while($row11=mysql_fetch_array($resultsx))
{
$studentID=$row11['studentID'];
$ClassID=$row11['ClassID'];
$yrs=$row11['yrs'];
$Fname=$row11['Fname'];
$Mname=$row11['Mname'];
$Lname=$row11['Lname'];
echo("<input name='$StudentID'\" type=\"hidden\" value='$Fname $Mname $Lname'\">");
++$n;
}
?>
<input name="rws" type="hidden" id="rws" value="<?php echo($n);?>">
</form>
<?php
}
?>
</div>
results5.php
<?php
if(isset($_GET['save']))
{
error_reporting (E_ALL ^ E_NOTICE);
$somolake=$_GET['somolake'];
$darasa=$_GET['darasa'];
$elimuyake=$_GET['elimuyake'];
$ainamtihani=$_GET['ainamtihani'];
$jina=$_GET['a_name'];
include("connect.php.inc");
foreach($_GET['a_name'] as $row=>$name)
{
//$jina=mysql_real_escape_string($name);
$jina=mysql_real_escape_string($_GET['a_name'][$row]);
//$jina=mysql_real_escape_string($_GET['a_name'][$row]);
$alama=mysql_real_escape_string($_GET['a_in'][$row]);
//$alama=mysql_real_escape_string($_GET['a_in']);
$sq2="insert into results(StudentID,SubjectID,ClassID,ExamID,Marks,SchoolID,PostedBy)
value ('$jina','$somolake','$darasa','$ainamtihani','$alama','".$_SESSION['schoolID']."','".$_SESSION['userid']."')";
mysql_query($sq2) or die(mysql_error());
}
include("success.php");
}

You can always check whether your variable is an array or not before supplying it to foreach.Try doing this:
if(is_array($item))
{
foreach($item as $items)
{
//your code
}
}

Related

How to display selected value Form drop down list?

I have a list proxy.Once I select and save it I want to display the selected data. How can I do this?.I want to display it in selected list and once I reload or get back to that page `
<?php
include_once("../noaccess.php");
include_once(CLASS_PATH."fetch_service.php");
$objfetch = new fetchService();
include_once(CLASS_PATH."proxy.php");
$objproxy = new Proxy();
include_once(CLASS_PATH."log.php");
$objlog= new changelog();
$account_id=$_SESSION['account_id'];
global $mysqli;
if($_POST['submit']=="Save")
{
$pname=$_POST['proxy'];
$query="UPDATE `proxy` SET `proxy_default`='1' where `proxyname`='$pname'";
$mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
setcookie("msg","Proxy Seleted",time()+5,"/");
header("location:".SITE_URL."index.php?view=default_proxy");
}
?>
<form name="frmcr" id="frmcr" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" id="mode" value="insert" />
<table align="left" id="tblworking_hours" class="tbl_altcolor shadow" style="width:25%;margin-left:30px">
<thead>
<tr>
<th align="center"><b>Proxy</b></th>
<td><select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<!-- <option value="<?php echo $i ;?>"<?php echo $i==$ring21 ? "selected":"";?> ><?php echo $i;?></option> -->
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']== $resrproxy['proxyname'] ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="Save" class="btn" style="margin-left:35px;"/></td>
</tr>
</thead>
</table>
</div>
You've to compare $resrproxy['proxyname'] value with $pname. Your code should be like this:
// your code
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy){
$option = "<option value=\"{$resrproxy['proxyname']}\"";
if(isset($pname)){
if($pname == $resrproxy['proxyname']){
$option .= " selected=\"selected\"";
}
}
$option .= ">{$resrproxy['proxyname']}</option>";
echo $option;
}
?>
</select>
// your code `
<td>
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' and `proxy_default`='1'");
$pname=$result[0]['proxyname'];
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']==$pname ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select>
</td>

How to make a dynamic form in Codeigniter

I want to ask something as a Codeigniter newbie.
I want make a dynamic form in CI, if I want increase a form, I can click " Increase" then the form will increase at the bottomw, and if click "Increase" too many times I can click "Delete" then the form will decrease..
I made it with the dynamic form, but my problem arises when I want to save it to the SQL database.
My code is like this :
tambah_material.php
<div id="isi">
<h1>Material Order</h1><br />
<div class="submitButton">List Material Order</div>
<br><br><br><br><table width="100%">
<form method="post" id="myfrm" action="<?php echo base_url(); ?>index.php/adminweb/simpanmaterial">
<tr><td width="200">Project Name</td><td width="10" align="center">:</td><td><input type="text" class="input" name="nama_lengkap" size="100" /></td></tr>
<tr><td width="200">Project Status</td><td width="10" align="center">:</td><td>
<select name="nama" class="input">
<option value="Under Project Implementation" selected="selected">Under Project Implementation</option><option value="Under Warranty">Under Warranty</option>
<option value="Project Warranty">Project Warranty</option><option value="Out of Warranty">Out of Warranty</option>
</select></td></tr>
<tr><td width="200">Site/Hop Name</td><td width="10" align="center">:</td><td><input type="text" class="input" name="nim" size="50" /></td></tr>
<tr><td width="200">Material Description</td><td width="10" align="center">:</td><td>
<script type="text/javascript">
$(document).ready(function() {
var nomor = 0;
var status="";
$(".tambah").click(function(){
nomor ++;
$('#konten').append(
'<tr class="baris">'
+ '<input name="nomor[]" value="'+ nomor +'" type="hidden"></td>'
+ '<td><input name="tempat_'+ nomor +'" type="text" size="40"></td>'
+ '<td><input name="sn_'+ nomor +'" type="text" size="60"></td>'
+ '<td><input type="button" id="hapus" value="Hapus"></td></tr>'
);
});
$("#hapus").live('click', function () {
$(this).parents(".baris").remove();
});
$("#myfrm").validate({
debug: false,
rules: {
},
messages: {
},
});
});
function tampilTabel()
{
if(status=="")
{
$('#tabel').slideDown();
status="1";
}
else
{
$('#tabel').slideUp();
status="";
}
}
</script>
<a class="tambah" style="cursor:pointer; padding:6px; border:1px solid #000; background-color:#FF9900; color:#000000;">Tambah Form</a>
<br><br>
<tr><td></td><td></td><td>Nama Material :
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
Serial Number :</td>
</tr>
<tr id="konten"><td></td><td>
<tr><td width="150">Reason/Suspect Problem</td><td width="10">:</td><td>
<select name="jk" class="input">
<option value="Interference" selected="selected">Interference</option><option value="Lightning Strike">Lightning Strike</option>
<option value="Leakage Shelter">Leakage Shelter</option><option value="Replace Broken One">Replace Broken One</option>
<option value="Borrow">Borrow</option><option value="Others">Others</option>
</select>
</td></tr>
<tr><td width="200">Returned to RWH</td><td width="10" align="center">:</td><td><input type="text" class="input" name="alamat" size="70"/></td></tr>
<tr><td width="200">Quantity</td><td width="10" align="center">:</td><td><input type="text" class="input" name="jml" size="70"/></td></tr>
<tr><td width="200">Status</td><td width="10" align="center">:</td><td>
<select name="status" class="input">
<option value="yes" selected="selected">Yes</option><option value="no">No</option></select>
</select>
</td></tr>
<tr><td width="200"></td><td width="10" align="center"></td><td><input type="submit" value="Simpan Data" class="input" /> <input type="reset" value="Hapus" class="input" /></td></tr>
</form>
</table>
</div>
adminweb.php (controller)
function simpanmaterial()
{
$data=array();
$data2=array();
$session=isset($_SESSION['username_belajar']) ? $_SESSION['username_belajar']:'';
if($session!=""){
$pecah=explode("|",$session);
$data["username"]=$pecah[0];
$data["nama"]=$pecah[1];
$data["status"]=$pecah[2];
$data['scriptmce'] = $this->scripttiny_mce();
if($data["status"]=="admin"){
$tgl = " %Y-%m-%d";
$time = time();
$in["nama"]=$this->input->post('nama');
$in["nim"]=$this->input->post('nim');
$in["nama_lengkap"]=$this->input->post('nama_lengkap');
$in["alamat"]=$this->input->post('alamat');
$in["jk"]=$this->input->post('jk');
$in["jml"]=$this->input->post('jml');
$in["status"]=$this->input->post('status');
$in["tempat_1"]=$this->input->post('tempat_1');
$in["tempat_2"]=$this->input->post('tempat_2');
$in["tempat_3"]=$this->input->post('tempat_3');
$in["tempat_4"]=$this->input->post('tempat_4');
$in["sn_1"]=$this->input->post('sn_1');
$in["sn_2"]=$this->input->post('sn_2');
$in["sn_3"]=$this->input->post('sn_3');
$in["sn_4"]=$this->input->post('sn_4');
$in["tanggal"] = mdate($tgl,$time);
$in["penulis"] = $data["username"];
if($in["nama_lengkap"]=="" || $in["nim"]=="" || $in["alamat"]=="")
{
echo "Data masih kosong..!!!";
}
else{
$this->Admin_model->Simpan_Artikel("tbl_mahasiswa",$in);
echo "<meta http-equiv='refresh' content='0; url=".base_url()."index.php/adminweb/insbaru'>";
}
}
else{
?>
<script type="text/javascript" language="javascript">
alert("Anda tidak berhak masuk ke Control Panel Admin...!!!");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."index.php/'>";
}
}
else{
?>
<script type="text/javascript" language="javascript">
alert("Anda belum Log In...!!!\nAnda harus Log In untuk mengakses halaman ini...!!!");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."index.php/'>";
}
}
thanks for your help
Best Regards,
Karisma
For making your code Dynamic you can grab a existing MY_MODEL from github,and if you want to make your design responsive use Bootstrap.

How to move in a mysql table with ajax and php to the next and previous rows?

After the bad communication and unclear questions from yesterday from my side, I prepared something. I was the first time in a forum like this. It is tough, but good! I realized how professional the people in this forum. I know not one forum that comes near to your quality of informations!
I am not a sweet talker, this is fact! Here is the problem:
I connect to a mysql table but I can not go to the next and next and next row, the same for the previous records.
I still dont understand it deeply. What is wrong on this code? I am really very curious to know it.
When I click on previous come "undefined objects."
When I skip next then comes only one record, and then it freeze. I guess that I am on the end of the table.
I was reading so many posts here, at least 50, but I did not get it. I just did not understand it.
nice greetings from Bangkok
+++
I dont use jquery commands. I use just json parse to fetch the data, but I get not a lot ;)
It is very basic. All what I want to do is get the previous and next rows. Always only one row.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function jsRecordNext()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
alert("no luck");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'nextarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//previous row
<?php include "accesscontrol.php" ;?>
<?php
$result = mysql_query("SELECT * FROM articles WHERE id<$current_id ORDER BY id DESC LIMIT 1");
$current_id = mysql_insert_id();
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//next row
<?php include "accesscontrol+.php" ;?> // connection
<?php
$result = mysql_query("SELECT * FROM articles WHERE id>$current_id ORDER BY id ASC LIMIT 1");
$current_id = mysql_insert_id();
$result = mysql_fetch_row($resulat);
echo json_encode($result);
?>
++++++++++++++++++++++++++++++++
this is index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<META HTTP-Equiv="FileTransfer" Content="TransferEvent:url('Javascript:alert('%s')'); Transfer">
<head>
<meta charset="UTF-8" />
<title>SoB - Administration</title>
<link rel="stylesheet" href="../wp-content/themes/Kuraya-Theme/style.css" tppabs="http://www.spiritofbushido.com/wp-content/themes/Kuraya-Theme/style.css">
<link rel="pingback" href="http://www.spiritofbushido.com/xmlrpc.php">
<script type="text/javascript" src="../wp-content/themes/Kuraya-Theme/js/scroll.js" tppabs="www.spiritofbushido.com/wp-content/themes/Kuraya-Theme/js/scroll.js"></script>
<script type="text/javascript" src="../includes/jquery.js"></script>
<script type="text/javascript" src="userinfo.0.0.1.min.js"></script>
<link rel="alternate" type="application/rss+xml" title="Kuraya » Feed" href="http://www.scoobeedo.com/feed/" />
<link rel="alternate" type="application/rss+xml" title="Kuraya » Comments Feed" href="http://www.scoobeedo.com/comments/feed/" />
<link rel="alternate" type="application/rss+xml" title="Kuraya » Join Our E-mail List Comments Feed" href="http://www.scoobeedo.com/email-signup/feed/" />
<link rel='stylesheet' id='shadowbox-css-css' href="../wp-content/uploads/shadowbox-js/src/shadowbox.css-ver=3.0.3.css" tppabs="http://www.scoobeedo.com/wp-content/uploads/shadowbox-js/src/shadowbox.css?ver=3.0.3" type='text/css' media='screen' />
<link rel='stylesheet' id='shadowbox-extras-css' href="../wp-content/plugins/shadowbox-js/css/extras.css-ver=3.0.3.10.css" tppabs="http://www.scoobeedo.com/wp-content/plugins/shadowbox-js/css/extras.css?ver=3.0.3.10" type='text/css' media='screen' />
<link rel='stylesheet' id='contact-form-7-css' href="../wp-content/plugins/contact-form-7/styles.css-ver=3.1.2.css" tppabs="http://www.scoobeedo.com/wp-content/plugins/contact-form-7/styles.css?ver=3.1.2" type='text/css' media='all' />
<script type='text/javascript' src="../../ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js-ver=3.3.2.js" tppabs="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js?ver=3.3.2"></script>
<script type='text/javascript' src="../wp-includes/js/comment-reply.js-ver=20090102.js" tppabs="http://www.scoobeedo.com/wp-includes/js/comment-reply.js?ver=20090102"></script>
<link rel='prev' title='東方古玩 蔵屋' href='http://www.scoobeedo.com/%e4%b8%ad%e6%96%87%e7%89%88/' />
<link rel='next' title='Sale 20% – 40% Off Entire Inventory Before Our Newshipment Arrived. Sales End April 30th.' href='http://www.scoobeedo.com/12131-2/' />
<link rel='canonical' href='http://www.scoobeedo.com/email-signup/' />
<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
<script id="source" language="javascript" type="text/javascript">
<!--
var js_string;
document.getElementById("recordWrite").disabled = true;
function jsRecordPrevious()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
$( '#recordnumber').html( '-' );
alert("f p");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'previousarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordCurrent()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
alert("f c");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'currentarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordNext()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
alert("f n");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'nextarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsShowArticle()
{
js_articles = JSON.parse(json_string);
document.form_articles.newItem.value=js_articles[1];
document.form_articles.newItemNo.value=js_articles[2];
document.form_articles.newMaterial.value=js_articles[3];
document.form_articles.newAge.value=js_articles[4];
document.form_articles.newItemSize.value=js_articles[5];
document.form_articles.newPrice.value=js_articles[6];
document.form_articles.newInfo.value=js_articles[7];
document.form_articles.newInfoRed.value=js_articles[8];
document.form_articles.newArrivalDate.value=js_articles[9];
if (js_articles[10]="Y")
{
document.form_articles.newArrivalDateShown.checked=true;
}
else
{
document.form_articles.newArrivalDateAhown.checked=false;
}
document.form_articles.newMainPicLink.value=js_articles[11];
document.form_articles.newItemCondition.value=js_articles[12];
}
function jsBlankArticle()
{
document.form_articles.newItem.value="";
document.form_articles.newItemNo.value="";
document.form_articles.newMaterial.value="";
document.form_articles.newAge.value="";
document.form_articles.newItemSize.value="";
document.form_articles.newPrice.value="";
document.form_articles.newInfo.value="";
document.form_articles.newInfoRed.value="";
document.form_articles.newArrivalDate.value="";
document.form_articles.newArrivalDateShown.checked=false;
document.form_articles.newMainPicLink.value="";
document.form_articles.newItemCondition.value="";
}
function jsNewArticle()
{
jsBlankArticle();
document.getElementById('formheadline').innerHTML='Article Database - Insert New Item';
document.getElementById('newItem').focus();
document.getElementById("recordWrite").disabled = false;
}
function jsWriteArticle()
{
document.getElementById('formheadline').innerHTML='Article Database - I save the new item';
document.getElementById("recordWrite").disabled = true;
}
function jsResetForm()
{
jsBlankArticle();
document.getElementById('formheadline').innerHTML='Article Database';
document.getElementById("recordWrite").disabled = true;
jsRecordCurrent()
}
jsResetForm();
// -->
</SCRIPT>
</head>
<body class="page page-id-11505 page-template-default" onload="jsRecordCurrent()">
<div id="page-wrap">s
<?php
include('../includes/header.html');
?>
<div id="container-main">
<div id="main-content">
<div class="post" id="post-11505">
<h2 class="title-page">SoB - Administration</h2>
<title>SoB - Administration</title>
<div id="recordnumber"></div>
<div class="entry">
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" name="form_articles">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td align="right">
</td>
<td align="left">
<span id="formheadline"><h2>Article Database</h2></span>
</td>
</tr>
<tr>
<td align="right">
</td>
<td align="left">
<span style="padding-right:20px"><font color="orangered" size="+1"><tt><b>*</b></tt></font>indicates a required field</span>
</td>
</tr>
<br>
<tr>
<td align="right">
<span style="padding-right:20px">Item</span>
</td>
<td>
<input id="newItem" name="newItem" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item No.</span>
</td>
<td>
<input name="newItemNo" type="text" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Arrival Date</span>
</td>
<td>
<input name="newArrivalDate" type="date" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Show in New Arrivals</span>
</td>
<td>
<input name="newArrivalDateShown" type="checkbox" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Material</span>
</td>
<td>
<input name="newMaterial" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Condition of item</span>
</td>
<td>
<input id="newItemCondition" name="newItemCondition" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Age</span>
</td>
<td>
<input name="newAge" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item Size</span>
</td>
<td>
<input name="newItemSize" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Price</span>
</td>
<td>
<input name="newPrice" type="text" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Info Red</span>
</td>
<td>
<input name="newInfoRed" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr valign="top">
<td align="right">
<span style="padding-right:20px">Infos</span>
</td>
<td>
<textarea wrap="soft" name="newInfo" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">PicLink</span>
</td>
<td>
<input id="newMainPicLink" name="newMainPicLink" type="text" maxlength="100" size="50" />
</td>
</tr>
<br><br>
<tr>
<td align="right" colspan="2">
<hr noshade="noshade" />
<input type="button" name="recordPrevious" value=" < " onclick="jsRecordPrevious()"/>
<input type="button" name="recordNext" value=" > " onclick="jsRecordNext()"/>
<input type="button" name="recordNew" value=" New " onclick="jsBlankArticle()"/>
<input type="button" name="recordEdit" value=" Edit " onclick="jsShowArticle()"/>
<span style="padding-right:20px"></span>
<input type="reset" value="Reset Form" />
<input id="recordWrite" type="button" name="recordWrite" value=" Write " onclick="jsWriteArticle()"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
<?php
include('../includes/sidemenu.html');
?>
<br class="clearfloat" />
</div> <!-- End of main container -->
</div><!-- END Page Wrap -->
<?php
include('../includes/footer.html');
?>
</body>
</html>
Hello you lack lots of Logocal and Programming skills, please improve it.
Files() are as below ->
Your index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<META HTTP-Equiv="FileTransfer" Content="TransferEvent:url('Javascript:alert('%s')'); Transfer">
<head>
<meta charset="UTF-8"/>
<title>SoB - Administration</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style type="text/css">.recentcomments a {
display: inline !important;
padding: 0 !important;
margin: 0 !important;
}</style>
<script id="source" language="javascript" type="text/javascript">
var js_string;
document.getElementById("recordWrite").disabled = true;
function jsRecordPrevious() {
// ... the AJAX request is successful
var updatePage = function (response) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function (req, status, err) {
$('#recordnumber').html('-');
alert("f p");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'previousarticle.php',
data : { currentRecord: $("[name='recordPrevious']").attr("id")},
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordCurrent() {
// ... the AJAX request is successful
var updatePage = function (response) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function (req, status, err) {
alert("f c");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'currentarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordNext() {
// ... the AJAX request is successful
var updatePage = function (response) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function (req, status, err) {
alert("f n");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'nextarticle.php',
data : { currentRecord: $("[name='recordNext']").attr("id")},
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsShowArticle() {
js_articles = JSON.parse(json_string);
//Dude , I added it (Pratik)->
$('[name="recordNext"]').attr("id",js_articles[0]);
$('[name="recordPrevious"]').attr("id",js_articles[0]);
document.form_articles.newItem.value = js_articles[1];
document.form_articles.newItemNo.value = js_articles[2];
document.form_articles.newMaterial.value = js_articles[3];
document.form_articles.newAge.value = js_articles[4];
document.form_articles.newItemSize.value = js_articles[5];
document.form_articles.newPrice.value = js_articles[6];
document.form_articles.newInfo.value = js_articles[7];
document.form_articles.newInfoRed.value = js_articles[8];
document.form_articles.newArrivalDate.value = js_articles[9];
if (js_articles[10] = "Y") {
document.form_articles.newArrivalDateShown.checked = true;
}
else {
document.form_articles.newArrivalDateAhown.checked = false;
}
document.form_articles.newMainPicLink.value = js_articles[11];
document.form_articles.newItemCondition.value = js_articles[12];
}
function jsBlankArticle() {
document.form_articles.newItem.value = "";
document.form_articles.newItemNo.value = "";
document.form_articles.newMaterial.value = "";
document.form_articles.newAge.value = "";
document.form_articles.newItemSize.value = "";
document.form_articles.newPrice.value = "";
document.form_articles.newInfo.value = "";
document.form_articles.newInfoRed.value = "";
document.form_articles.newArrivalDate.value = "";
document.form_articles.newArrivalDateShown.checked = false;
document.form_articles.newMainPicLink.value = "";
document.form_articles.newItemCondition.value = "";
}
function jsNewArticle() {
jsBlankArticle();
document.getElementById('formheadline').innerHTML = 'Article Database - Insert New Item';
document.getElementById('newItem').focus();
document.getElementById("recordWrite").disabled = false;
}
function jsWriteArticle() {
document.getElementById('formheadline').innerHTML = 'Article Database - I save the new item';
document.getElementById("recordWrite").disabled = true;
}
function jsResetForm() {
jsBlankArticle();
document.getElementById('formheadline').innerHTML = 'Article Database';
document.getElementById("recordWrite").disabled = true;
jsRecordCurrent()
}
jsResetForm();
</script>
</head>
<body class="page page-id-11505 page-template-default" onload="jsRecordCurrent()">
<div id="page-wrap">
<div id="container-main">
<div id="main-content">
<div class="post" id="post-11505">
<h2 class="title-page">SoB - Administration</h2>
<title>SoB - Administration</title>
<div id="recordnumber"></div>
<div class="entry">
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="form_articles">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td align="right">
</td>
<td align="left">
<span id="formheadline"><h2>Article Database</h2></span>
</td>
</tr>
<tr>
<td align="right">
</td>
<td align="left">
<span style="padding-right:20px"><font color="orangered" size="+1"><tt><b>*</b></tt></font>indicates a required field</span>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item</span>
</td>
<td>
<input id="newItem" name="newItem" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item No.</span>
</td>
<td>
<input name="newItemNo" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Arrival Date</span>
</td>
<td>
<input name="newArrivalDate" type="date"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Show in New Arrivals</span>
</td>
<td>
<input name="newArrivalDateShown" type="checkbox"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Material</span>
</td>
<td>
<input name="newMaterial" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Condition of item</span>
</td>
<td>
<input id="newItemCondition" name="newItemCondition" type="text" maxlength="100"
size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Age</span>
</td>
<td>
<input name="newAge" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item Size</span>
</td>
<td>
<input name="newItemSize" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Price</span>
</td>
<td>
<input name="newPrice" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Info Red</span>
</td>
<td>
<input name="newInfoRed" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr valign="top">
<td align="right">
<span style="padding-right:20px">Infos</span>
</td>
<td>
<textarea wrap="soft" name="newInfo" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">PicLink</span>
</td>
<td>
<input id="newMainPicLink" name="newMainPicLink" type="text" maxlength="100"
size="50"/>
</td>
</tr>
<br><br>
<tr>
<td align="right" colspan="2">
<hr noshade="noshade"/>
<input type="button" name="recordPrevious" value=" < "
onclick="jsRecordPrevious()"/>
<input type="button" name="recordNext" value=" > " onclick="jsRecordNext()"/>
<input type="button" name="recordNew" value=" New " onclick="jsBlankArticle()"/>
<input type="button" name="recordEdit" value=" Edit " onclick="jsShowArticle()"/>
<span style="padding-right:20px"></span>
<input type="reset" value="Reset Form"/>
<input id="recordWrite" type="button" name="recordWrite" value=" Write "
onclick="jsWriteArticle()"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
<br class="clearfloat"/>
</div>
<!-- End of main container -->
</div>
<!-- END Page Wrap -->
</body>
</html>
This is currentarticle.php
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('testDb', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$result = mysql_query("SELECT * FROM articles ORDER BY id ASC LIMIT 1");
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
This is previousarticle.php
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('testDb', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$current_id = $_REQUEST['currentRecord'];
$result = mysql_query("SELECT * FROM articles WHERE id < ".$current_id." ORDER BY id ASC LIMIT 1");
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
This is nextarticle.php
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('testDb', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$current_id = $_REQUEST['currentRecord'];
$result = mysql_query("SELECT * FROM articles WHERE id > ".$current_id." ORDER BY id ASC LIMIT 1");
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
I made changes in all files in some extent.So please modify accordingly in your code.And do proper coding from next time.
And you add validation like If its last row , disable NEXT button , if its 1st row , disable PREV button.I solved what you asked.
Thanks!

for update code in same page need ajax or else?

when i press on edit link on detail.php it will show me data.php page with data filled in form... now if i want to edit something then press edit button it would be update.. code of data.php is given as below
<?php if(isset($_GET['name']) && !empty($_GET['name'])):?>
<script>
window.onload=function()
{
document.getElementById("sbmt").style.visibility="hidden";
};
function editform()
{
i need code at this place _what should it be?? i guess i need ajax. when i press edit button it would be edit record i tried below something like below bt for that i need value name=??? how can i get that value _
<?php
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// mysqli_query($con,"UPDATE user_detail SET name=,address=,gender=,hoby=,country=,place=,
//WHERE name='$_GET['name']'");
mysqli_close($con);
?>
};
</script>
<?php
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$p= $_GET['name'];
//echo "$p";
$result = mysqli_query($con,"SELECT * FROM user_detail where name ='$p' ");
$data =mysqli_fetch_array($result);
mysqli_close($con);
?>
<?php else:?>
<script>
window.onload=function()
{
document.getElementById("edit").style.visibility="hidden";
};
</script>
<?php $data =array();?>
<?php endif;?>
<!DOCTYPE html>
<?php
session_start();
if (!isset($_SESSION['txt_user'])) {
header('Location: Login.php');
}
?>
<html>
<head>
<title> Form with Validation </title>
<script language="JavaScript">
var flag=0;
var file_selected = false;
var file_selected1 = false;
function validform()
{
var x=document.forms["form1"]["tname"].value;//Name
var y=document.forms["form1"]["address"].value;//Address
if (x==null || x=="")
{
flag = 1;
document.getElementById('tn').value="Please! Enter name ";
//alert("Please Enter Name:");
//return false;
}
else
{
flag=0;
}
if (y==null || y==" ")
{
flag=flag+1;
document.getElementById('ta').innerHTML="Please! Enter address ";
//alert("Please Enter Address");
//return false;
}
else
{
flag=0;
}
if ((form1.gender[0].checked == false) && (form1.gender[1].checked == false))
{
flag +=1;
document.getElementById('gne').innerHTML="Please! Select gender ";
//alert("Pleae Select Gender");
//return false;
}
else
{
flag =0;
document.getElementById('gne').innerHTML="";
}
if (form1.hobby.checked == false && form1.hobby.checked == false && form1.hobby.checked == false)
{
flag +=1;
document.getElementById('hbe').innerHTML="Please! Select hobby ";
//alert ('Please!!!, Select any hobby!');
//return false;
}
else
{
flag =0;
document.getElementById('hbe').innerHTML="";
}
var psel=document.getElementById('fp'); //Favourite place
var valid = false;
for(var i = 0; i < psel.options.length; i++) {
if(psel.options[i].selected) {
valid = true;
break;
}
}
if(valid==false)
{
flag +=1;
document.getElementById('fp_error').innerHTML="Please! Select Any Favourite Place ";
//return false;
// alert("Please! Select Any Favourite Place ");
}
else
{
flag =0;
document.getElementById('fp_error').innerHTML="";
}
if(!file_selected)
{
flag +=1;
document.getElementById('f_pic').innerHTML="Please! Select Any Picture ";
//alert('Please Select any Picture');
//return false;
}
else
{
flag =0;
document.getElementById('f_pic').innerHTML="";
}
if(!file_selected1)
{
flag +=1;
document.getElementById('f_doc').innerHTML="Please! Select Document";
//alert('Please Select any Document');
//return false;
}
else
{
flag =0;
}
if(flag ==0)
{
document.getElementById('data_form').action = "Data_con.php";
document.getElementById('data_form').submit();
}
else
{
return true;
}
return false;
}
function Logout()
{
document.getElementById('data_form').action = "Logout.php";
}
</script>
</head>
<body>
<!--onsubmit="return validform()"-->
<form id="data_form" name="form1" method="post" enctype="multipart/form-data" action="">
<table align="center" border="2">
<tr>
<td>Name:</td>
<td><input id="tn" type="text" name="tname" <?php if($data['name']):?>value="<?php echo $data['name'];?>" <?php endif;?>></td>
<!--<td id="tne"></td>-->
</tr>
<tr>
<td>Address:</td>
<td><textarea id= "ta" rows="3" cols="16" name="address" >
<?php echo $data['address'];?>
</textarea> </td>
<!--<td id="tae"></td>-->
</tr>
<tr>
<td>Gender:</td>
<?php
$x=$data['gender'];
?>
<td> <input type="radio" id="gn" name="gender" value="male" <?php if($x=='male'):?> checked<?php endif;?> > Male
<input type="radio" id="gn" name="gender" value="female" <?php if($x=='female'):?> checked<?php endif;?> > Female
</td>
<td id="gne"></td>
</tr>
<tr>
<td>Hobby:</td>
<?php
$y=$data['hoby'];
?>
<td>
<input type="checkbox" name="hobby" value="hockey" <?php if($y=='hockey'):?> checked='checked';<?php endif;?>> Hockey
<input type="checkbox" name="hobby" value="reading" <?php if($y=='reading'):?> checked='checked';<?php endif;?>> Reading<br>
<input type="checkbox" name="hobby" value="traveling" <?php if($y=='traveling'):?> checked='checked';<?php endif;?>> Traveling
<br>
</td>
<td id="hbe"></td>
</tr>
<tr>
<td>Country: </td>
<?php
$z=$data['country'];
?>
<td>
<select name="helo" id="hl">
<option value="germany"<?php if($z=='germany'):?> selected<?php endif;?> >Germany </option>
<option value="india" <?php if($z=='india'):?> selected <?php endif;?> >India </option>
<option value="japan" <?php if($z=='japan'):?> selected<?php endif;?> >Japan </option>
</select>
</td>
<td id="hle"></td>
</tr>
<tr>
<td>Favourite Place:</td>
<?php
$w =$data['place'];
?>
<td>
<select id="fp" name="place" multiple="multiple">
<option value="ahmedabad" <?php if($w=='ahmedabad'):?> selected<?php endif;?> >Ahmedabad</option>
<option value="nadiad" <?php if($w=='nadiad'):?> selected<?php endif;?> >Nadiad</option>
<option value="anand" <?php if($w=='anand'):?> selected<?php endif;?> >Anand</option>
<option value="vadodara" <?php if($w=='vadodara'):?> selected<?php endif;?> >Vadodara</option>
<option value="surat" <?php if($w=='surat'):?> selected<?php endif;?> >Surat</option>
</select>
</td>
<td id="fp_error"></td>
</tr>
<tr>
<td>Photo:</td>
<td><input type="file" onchange="file_selected=true;" name="pic" ></td>
<td id="f_pic"></td>
</tr>
<tr>
<td>Resume:</td>
<td><input type="file" onchange="file_selected1=true;" name="doc" ></td>
<td id="f_doc"></td>
</tr>
<tr>
<td colspan="2"><center>
<input type="Submit" id="edit" value="edit" Name="Edit" onclick="editform();">
<input type="button" id="sbmt" value="Submit" Name="Submit" onclick="validform();">
<input type="submit" value="Logout" Name="Submit" onclick="Logout();">
<center></td>
</tr>
</table>
</form>
</body>
</html>
Thanks friend...

error message should be below particular box

in this code,error message shows error by alert box,but i want to see error message below particular text box. mean when i click on submit button which field not fill, below that field it should display message below that field.
and also there is one problem if i not fill radio button or checkbox and other all are filled and when i click on submit button it store in database other all detail except that radio or checkbox.
so how can i solve it
Thanks.....
<!DOCTYPE html>
<?php
session_start();
if (!isset($_SESSION['txt_user'])) {
header('Location: Login.php');
}
?>
<html>
<head>
<title> Form with Validation </title>
<script language="JavaScript">
var file_selected = false;
var file_selected1 = false;
function validform()
{
var x=document.forms["form1"]["tname"].value;//Name
var y=document.forms["form1"]["address"].value;//Address
var psel=document.getElementById('fp'); //Favourite place
var valid = false;
for(var i = 0; i < psel.options.length; i++) {
if(psel.options[i].selected) {
valid = true;
break;
}
}//Favourite place
if (x==null || x=="")
{
alert("Please Enter Name:");
//flag = 0;
}
if (y==null || y==" ")
{
alert("Please Enter Address");
}
if ((form1.gender[0].checked == false) && (form1.gender[1].checked == false))
{
alert("Pleae Select Gender");
}
if (form1.hobby.checked == false && form1.hobby.checked == false && form1.hobby.checked == false)
{
alert ('Please!!!, Select any hobby!');
}
if(valid==false)
{
alert("Please! Select Any Favourite Place ");
}
if(!file_selected)
{
alert('Please Select any Picture');
}
if(!file_selected1)
{
alert('Please Select any Document');
return false;
}
{
document.getElementById('data_form').action = "Data_con.php";
return false;
}
return false;
}
function Logout()
{
document.getElementById('data_form').action = "Logout.php";
}
</script>
</head>
<body>
<!--onsubmit="return validform()"-->
<form id="data_form" name="form1" method="post" enctype="multipart/form-data" action="">
<table align="center" border="2">
<tr>
<td>Name:</td>
<td><input type="text" name="tname"></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea rows="3" cols="16" name="address"> </textarea> </td>
</tr>
<tr>
<td>Gender:</td>
<td> <input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
</td>
</tr>
<tr>
<td>Hobby:</td>
<td>
<input type="checkbox" name="hobby" value="hockey"> Hockey
<input type="checkbox" name="hobby" value="reading"> Reading<br>
<input type="checkbox" name="hobby" value="traveling"> Traveling
<br>
</td>
</tr>
<tr>
<td>Country: </td>
<td>
<select name="helo">
<option value="germany">Germany </option>
<option value="india" selected>India </option>
<option value="japan">Japan </option>
</select>
</td>
</tr>
<tr>
<td>Favourite Place:</td>
<td>
<select id="fp" name="place" multiple="multiple">
<option value="ahmedabad">Ahmedabad</option>
<option value="nadiad">Nadiad</option>
<option value="anand">Anand</option>
<option value="vadodara">Vadodara</option>
<option value="surat">Surat</option>
</select>
</td>
</tr>
<tr>
<td>Photo:</td>
<td><input type="file" onchange="file_selected=true;" name="pic" ></td>
</tr>
<tr>
<td>Resume:</td>
<td><input type="file" onchange="file_selected1=true;" name="doc" ></td>
</tr>
<tr>
<td colspan="2"><center>
<input type="submit" value="Submit" Name="Submit" onclick="validform();">
<input type="submit" value="Logout" Name="Submit" onclick="Logout();">
<center></td>
</tr>
</table>
</form>
</body>
</html>
you first create tds with different id's for each and every input field
instead of alert
document.getElementById('td id').innerHTML="your message";
return false;
like this
<tr>
<td>Favourite Place:</td>
<td>
<select id="fp" name="place" multiple="multiple">
<option value="ahmedabad">Ahmedabad</option>
<option value="nadiad">Nadiad</option>
<option value="anand">Anand</option>
<option value="vadodara">Vadodara</option>
<option value="surat">Surat</option>
</select>
</td>
<td id="fp_error"></td>
</tr>
and in javascript
document.getElementById('fp_error').innerHTML="your message";
return false;
download jquery.js and jquery.validate.js and validate form elements using below demo code.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"> </script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.validate.js"> </script>
<script>
$('document').ready(function() {
$('#data_form').validate({
rules: {
'tname': {
required: true
},
'address': {
required: true
}
},
messages:{
'tname': {
required: 'Please Enter Name'
},
'address': {
required: 'Please Enter address'
}
}
});
});
</script>
It is better way. Easy of implementation and also saves time.

Categories