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 :
                                                            
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.
Related
At present I have set 4 variables, the values of which are then stored into mysql. This works fine. However, I don't want to set the values but write a line of code that takes these values from my form (on the same page). I have set the form method to POST and added specialchars to help security. Can someone pretty please show me one or two lines of code so I don't have to write ="John Doe". Please remember that I am very new all of this
<?php
// Connect to the Database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "topsecretDontTell";
$dbname = "gaming";
$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Show error if connection fails
if(mysqli_connect_errno()){
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() .")"
);
}
?>
<?php
// ordertbl
$customer_name = "John Doe";
$game_id = 3;
$reservation_start = "2015-01-05";
$requested_days = 1;
// removes single quotes (escapes strings)
$customer_name = mysqli_real_escape_string($connection, $customer_name);
//add into ordertbl
$query = "INSERT INTO ordertbl (customer_name,game_id,reservation_start,requested_days) VALUES ('{$customer_name}',{$game_id},'{$reservation_start}', {$requested_days})";
//Run query and test if there was a query error
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
?>
<?php
//determine the name of the game via its id using a function
function GameTitle ($game_id){
$message = "";
if ($gameid ==1){
$message = "Fantasy World";
}
else if ($gameid ==2){
$message = "Sir Wags A Lot";
}
else if ($gameid ==3){
$message = "Take a Path";
}
else if ($gameid ==4){
$message = "River Clean Up";
}
else if ($gameid ==5){
$message = "PinBall";
}
else if ($gameid ==6){
$message = "Ghost girl";
}
else if ($gameid ==7){
$message = "Dress up";
}
else if ($gameid ==8){
$message = "Where is my hat?";
}
else {
$message = "Invalid ID";
}
return $message;
}
?>
</body>
</html>
<!--Link to the style sheet-->
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<!--Create Header (logo, title and navigation bar)-->
<body>
<div id='main'>
<div id='titleImage'><img title='Home' src='images/GLLogo.png' width='700' height='190' alt='Games Library Title' /></div>
<div id='menu-wrapper'>
<div id='menu'>
<ul>
<li><a href='index.html'>Home</a></li>
<li class='current_page_item'><a href='#'>Reservations</a></li>
</ul>
</div>
</div>
<!--Make the form-->
<div class="form">
<h1>Reservations</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="755" border="3" cellpadding="6">
<tr>
<td width="195" align="right" bgcolor="#FF0000"><label for="customer_name">Name:</label></td>
<td width="370"><input name="customer_name" autofocus type="text" id="customer_name" size="35" maxlength="90" required autocomplete="off" /></td>
</tr>
<tr>
<td align="right" bgcolor="#FF0000"><label for="game_id">Game's ID:</label></td>
<td><input name="game_id" type="number" id="game_id" size="35" maxlength="50" min="1" /></td>
</tr>
<tr>
<td width="195" align="right" bgcolor="#FF0000"><button onClick="GameTitle(); return false">Search</button></td>
<td><input name="Result" type="text" id="demo" size="35" maxlength="50" /></td>
</tr>
<tr>
<td align="right" bgcolor="#FF0000"><label for="Loan">Number of Days you wish to borrow the Game</label></td>
<td><select name="requested_days" id="requested_days">
<option selected="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
</tr>
<tr> <!--put date into value field to get a calendar-->
<td align="right" bgcolor="#FF0000"><label for="reservation">Reservation Date:</label></td>
<td><input id="reservation_start" input name="reservation_start" type="" value="" placeholder="YYYY/MM/DD" pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" title="The date should be in the exact format: YYYY-MM-DD with leading zeros where necessary"/>
</tr>
<tr>
<td align="right" bgcolor="#FF0000"><label for="mysearch2">Enter your search string here : </label></td>
<td><input {background-colour: #E5F5EF;} id="mysearch2" type="search" placeholder="search"size="35" maxlength="50"/>
</tr>
<tr>
<td align="right" bgcolor="#FF0000"><input type="reset" name="Reset" id="button" value="Reset Form" /></td>
<td><input type="submit" name="button2" id="button2" value="Submit Form" /></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
<?php
// get rid of data in cache and close
mysqli_close($connection);
?>
Use the following, taking the POST variable from your form's <input name="customer_name"... element:
$customer_name=stripslashes($_POST['customer_name']);
$customer_name=mysqli_real_escape_string($connection,$_POST['customer_name']);
which will allow for names containing apostrophes like John O'Reilly.
Plus, you have function GameTitle ($game_id) therefore you most likely meant to use function GameTitle ($gameid)
You should use $_POST. In that array are post data. For example:
$customer_name = $_POST['name'];
I am using smarty templete engine and on the below page I have a form if I disabled my submit button then values of form not submited on the same page if i not disabled then resubmitting problem arise how to resolve this
collection.php
collection.tpl
collection.js
$(document).ready(function(){
$("#fee_collection_form").on('submit', function () {
var class_id = $('#class_id').val();
var cs_id = $('#class_section_list').val();
var student_registration_no = $('#student_list').val();
var net_fee_value = $('#net_fee_value').val();
var paid_amount = $('#paid_amount').val();
var collection_date = $('#collection_date').val();
var due_date = $('#due_date').val();
if(class_id == 'null'){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Please Select Class</div>');
$('#class_id').focus();
return false;
}
else if(cs_id == 'null'){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Please Select Class Section</div>');
$('#class_section_list').focus();
return false;
}
else if(student_registration_no == 'null'){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Please Select Student</div>');
$('#student_list').focus();
return false;
}
else if(net_fee_value == ''){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Please Select Months</div>');
$('#monthlist').focus();
return false;
}
else if(paid_amount == ''){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Enter Paid Amount</div>');
$('#paid_amount').focus();
return false;
}
else if(collection_date == ''){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Enter Collection Date</div>');
$('#collection_date').focus();
return false;
}
else if(due_date == ''){
$('#error-msg').html('<div class="alert alert-danger">×<strong>Error</strong> Enter Enter Due Date</div>');
$('#due_date').focus();
return false;
}
else{
$('#fee_collection_form').submit();
$('#submit).prop('disabled', true);
}
}) ;
});
{* student_admission.tpl *}
{load_presentation_object filename="fee_collection" assign="obj"}
<div class="form-coverup-div">
{if $obj->mErrorMessage}<p class="error">{$obj->mErrorMessage}</p> {/if}
<form id="fee_collection_form" name="fee_collection_form" class="form" method="post" action="{$obj->mLinkToFeeCollection}" autocomplete="off" enctype="multipart/form-data" >
<h3>Student Fee Collection Form</h3>
<div id="error-msg"></div>
<table>
<tr>
<fieldset>
<legend>Search Student</legend>
<td><span>Class Name</span>
<select tabindex="1" id="class_id" name="class_id">
<option value="null">--Select Class --</option>
{foreach from=$obj->mClassList item=value}
{html_options values=$value.class_id output=$value.class_name}
{/foreach}
</select>
</td>
<td><span>ClassSection <img style="display:none;" id="loader" src="{$obj->mSiteUrl}images/ajax-loader.gif"></span>
<select tabindex="2" name="class_section_id" id="class_section_list">
<option value="null">--Select Section --</option>
<!--populated using ajax-->
</select>
</td>
<td><span>Student Name <img style="display:none;" id="loader2" src="{$obj->mSiteUrl}images/ajax-loader.gif"></span>
<select tabindex="3" name="student_registration_no" id="student_list">
<option value="null">--Select Student --</option>
<!--populated using ajax-->
</select>
</td>
</fieldset>
</tr>
<tr>
<td><span>Mother Name</span><input value="" type="text" disabled="disabled" id="student_mother_name"/></td>
<td><span>Father Name</span><input value="" id = "student_father_name"type="text" disabled="disabled" /></td>
<td><span>Address</span><input value="" type="text" disabled="disabled" id="address"/></td>
</tr>
</table>
<table style="width:100%;">
<fieldset>
<legend>Fee Status <img style="display:none;" id="loader3" src="{$obj->mSiteUrl}images/ajax-loader.gif">
</legend>
<tr>
<td>
<div id="fee_structure" style="overflow:auto;float:left;width:70%; height:150px; border:1px solid #ddd;">
Calculated Fee
</div>
<div style="overflow:auto;width:20%;float:left; height:150px; border:1px solid #ddd;">
<div id="monthlist">
Months List
</div>
</div>
<div>
<!-- <input type="button" value="Ok" tabindex="4" id="buttonParent">-->
</div>
</td>
</tr>
</table>
<table>
<tr>
<td><span>Previous Balance</span><input value="" placeholder="previous balance" type="text" readonly ="readonly" placeholder="" name="previous_balance" id="previous_balance"/></td>
<td><span>Discount[%]</span><input value="" tabindex="5" placeholder="in percentage" type="text" placeholder="" name="discount_in_percentage" maxlength="5" id="discount_in_percentage"/></td>
<td><span>Paid Amount</span><input value="" tabindex="8" placeholder="paid amount" type="text" placeholder="" name="paid_amount" id="paid_amount"/></td>
</tr>
<tr>
<td><span>Net Amount</span><input value="" type="text" readonly ="readonly" placeholder="fee value.." name="net_fee_value" id="net_fee_value"/></td>
<td><span>Discount Amount</span><input value="" tabindex="6" placeholder="discount amount" type="text" placeholder="" name="discount_amount" id="discount_amount"/></td>
<td><span>Balance</span><input value="" placeholder="balance amount" type="text" placeholder="" readonly ="readonly" name="balance_amount" id="balance_amount"/></td>
</tr>
</fieldset>
<tr>
<td><span>Payable Amount</span><input value="" placeholder="payable amount" type="text" placeholder="" readonly ="readonly" name="amount_payable" id="amount_payable"/></td>
<td><span>Remark</span><input tabindex="7" value="" type="text" placeholder="Remark" name="remark" id="remark"/> </td>
</tr>
</table>
<table>
<fieldset>
<legend>Date Details</legend>
<tr>
<td><span>CollectionDate</span><input type="text" value="" tabindex="9" placeholder="collection date" name="collection_date" class="collection_date" id="collection_date"/></td>
<td><span>DueDate</span><input type="text" value="" tabindex="10" placeholder="due date" name="due_date" class="due_date" id="due_date"/></td>
</tr>
</table>
</br>
<table>
<tr>
<td><button tabindex="11" name="submit" id="submit" type="submit" class="btn btn-primary">Make Transaction</button></td>
<td><button type="reset" value="Reset" class="btn btn-default">Reset</button></td>
</tr>
</table>
</form>
</div>
<!--THIS IS MY PHP CODE-->
<?php
class FeeCollection{
public $mErrorMessage;
public $mLinkToFeeCollection;
//get class for admission
public $mClassList;
public function __construct(){
$this->mLinkToFeeCollection = Link::ToFeeCollection();
}
public function init(){
//submit the form values
if(isset($_POST['submit'])){
$class_id = $_POST['class_id'];
$class_section_id = $_POST['class_section_id'];
$student_registration_no = $_POST['student_registration_no'];
if(isset($_POST['months_list'])){
$months = implode(', ',$_POST['months_list']);
}
$net_amount = $_POST['net_fee_value'];
$discount_amount = $_POST['discount_amount'];
$remark = $_POST['remark'];
$amount_payable = $_POST['amount_payable'];
$paid_amount = $_POST['paid_amount'];
$balance_amount = $_POST['balance_amount'];
$academic_year_id = $_SESSION['academic_year'] ;
$collection_date = $_POST['collection_date'];
$due_date = $_POST['due_date'];
if(empty($class_id) || $class_id =='null'){
$this->mErrorMessage = 'Please Select Class';
}elseif(empty($class_section_id) || $class_section_id =='null'){
$this->mErrorMessage = 'Please Select Class Section';
}elseif(empty($student_registration_no) || $student_registration_no == 'null'){
$this->mErrorMessage = 'Please Select Student';
}elseif(empty($net_amount)){
$this->mErrorMessage = 'Please First Calculate Fee';
}elseif(empty($paid_amount)){
$this->mErrorMessage = 'Please Enter Paid Amount';
}elseif(empty($collection_date)){
$this->mErrorMessage = 'Please Enter Collection Date';
}elseif(empty($due_date)){
$this->mErrorMessage = 'Please Enter Due Date';
}elseif($this->mErrorMessage == null){
SchoolErp::AddStudentFee($class_section_id,$student_registration_no,$months,$net_amount,$discount_amount,
$remark,$amount_payable,$paid_amount,$balance_amount,$academic_year_id,
$collection_date,$due_date);
header('Location: '.htmlspecialchars_decode($this->mLinkToFeeCollection));
}
}
//get all classes for select drop down menu
$this->mClassList = SchoolErp::ShowClassDetails();
}
}
?>
I want to know:-
IF the data process on the same page and if submit button pressed very quickly multiple times how to prevent user not to click multiple time on the submit button
I redirected the user using (header:location) on the same page but before that user click so many times
IF there any jquery or php solution ?
You can disable you submit button after click:
$(document).on('click', '#submit', function(){
$(this).prop('disabled', true);
});
You can disable by two ways ,
Either by onclick function :
document.getElementsByTagName("input")[1].onclick = function () {
this.disabled = true;
};
or
Returning false after it's been disabled to prevent the user from spamming:
document.getElementById("f2").onsubmit = function() {
this.children[1].disabled = true;
return false; // prevent form from actually posting (only for demo purposes)
Example of JSFiddle: http://jsfiddle.net/zb8CZ/
Credit doesn't goes to me.
Disable submit button on click. jsfiddle
<input type="submit" onclick="this.disabled=true;this.value='Sending, please wait...';this.form.submit();" name="btnSubmit" id="btnSubmit" value="Submit" />
I prefer to add a 2nd submit handler, which prevents additional submits.
but
this is simple:
$("form").submit(function(e) {
e.preventDefault();
$("#submit").prop('disabled', true);
$.post( "ajax/test.html", yourData, function(data) {
$( "body" ).html( data ); // or whatever; deal with your data
// handle data as you normally would
}).always(function() {
$("#submit").prop('disabled', false);
});
});
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
}
}
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.
I am trying to use a jquery to have text boxes and little pluses and minuses to add and delete them when required. It displays the data from MySQL but you can't delete them and you can only add them
Here is a link to a webpage i am using
http://adamlisik.co.uk/epq2/models/certificate.php?certid=250631166
I want to find a way that the text boxes that are displayed with the data from db can be deleted
<script type="text/javascript">
$(document).ready(function(){
// tworzymy zmienną i, wykorzystamy ją do identyfikowania wierszy
// ustawiamy jej wartość 2, ponieważ jeden wiersz jest już w tabeli
var i = 2;
// tworzymy zmienną szablonWiersza i wczytujemy do niej szablon z textarea #szablon
var szablonWiersza = jQuery.format($("#szablon").val());
// tworzymy funkcję, która dodaje szablon do właściwej tabeli
function dodajWiersz() {
var ii = i++;
// dodajemy wiersz do właściwej tabeli
$(szablonWiersza(ii)).appendTo("#listaOsob tbody");
// Nowy wiersz jest już widoczny.
// Każdy element nowego wiersza posiada swoje id
// tr na id: wiersz_2, ikona minus ma id: usunOsobe_2
// Do nowo dodanej ikony - dodajmy zdarzenie.
// W momencie kliknięcia minusa, wiersz zostanie usunięty.
$("#usunOsobe_" + ii).click(function(){
$("#wiersz_" + ii).remove();
});
}
// do przycisku #dodajOsobe dodajemy zdarzenie,
// kliknięcie na przycisk plus wywoła fukcję dodajWiersz
$("#dodajOsobe").click(dodajWiersz);
});
</script>
<h1>Sixth form baccaluraureate</h1>
<div id="content">
<textarea id="szablon" style="display:none;" cols="1" rows="1">
<tr id="wiersz_{0}">
<td><input type="text" name="name[]" ></td>
<td><input type="text" name="hours[]"></td>
<td><input type="hidden" name="certificate_id[]"value="<?php echo $table2[0][2]; ?>">
<td><img src="http://antczak.org/source/dynamic_forms/12.png" id="usunOsobe_{0}" alt="usun"></td>
</tr>
</textarea>
<!-- Właściwa tabela -->
<h4>activities form</h4>
<form action="update_certificate.php" method="post">
<!-- do not delete student is!!!! because we need it in confirm_cert.php-->
<input type="hidden" name="student_id" value="<?php echo $row[0] ?>" /> <br />
<input type="hidden" name="student_id2" value="<?php echo $row[3] ?>" /> <br />
<input type="hidden" name="certificate_number" value="<?php echo $certificate_name; ?>" />
<br/>
<table id="listaOsob">
<thead>
<tr>
<th>Activity name</th>
<th>Hours</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$a=0;
while($a<sizeof($table2)){
echo "<tr><td><input type='text' name='name[]' value='".$table2[$a][0]."'></td>";
echo "<td><input type='text' name='hours[]' value='".$table2[$a][1]."'></td>";
echo "<td><input type='hidden' name='certificate_id[]' value='".$table2[$a][2]."'></td>";
#echo $a;
if ($a==0){
echo "<td><img src='../view/11.png' id='dodajOsobe' alt='dodaj'></td></tr>";
}else{
echo "<td><img src='../view/12.png' id='usunOsobe_{0}' alt='usun'></td></tr>";
}
$a++;
}
?>
</tr>
</tbody>
</table>
<input type="submit" value="Submit" />
</form>
</div>
Perhaps you could use the jQuery .remove() function.
http://api.jquery.com/remove/