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.
Related
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
}
}
I'm trying to create a changing drop down list in php, but without a database. Here's what I came up with so far
<body>
<form method = "post">
<table>
<tr>
<td>
<p>Name</p>
</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td><p>State</p>
</td>
<td>
<select name = "State">
<option value = "PA"> Pennsylvania</option>
<option value = "CA"> California</option>
<option value = "AZ"> Arizona</option>
<option value = "NY"> New York</option>
<option value = "FL"> Florida</option>
</select>
</td>
</tr>
<td><input type="submit" name = "formSubmit" value="Submit" /></td>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['formSubmit']))
{
if(empty($_POST['Name']))
{
echo("You forgot your name");
}
else
{
}
}
?>
</body>
I had planned to use the 'else' statement to generate my second drop down list, but so far nothing I've tried has worked. I've looked all over for ideas, but most of the information I've come across has dealt with databases. This isn't a database, it's a single PHP program. Maybe I have the wrong idea of how this works. Should I try calling a function that creates the form ahead of time or am I completely off base for what I'm attempting?
To do so you need javascript
There is already a question posted related to yours
javascript-dynamic-drop-down-box-update
jQuery would give you a true dynamic result without having to reload the page. However if you want to use PHP to add what is in the textbox to the list of values then you can use PHP session as an array to hold the values like this.
<?php
session_start();
if(isset($_POST['formSubmit']))
{
if(empty($_POST['Name']))
{
echo("You forgot your name");
}
else
{
$name = $_POST['Name'];
if(isset($_SESSION['List']))
{
$index = count($_SESSION['List']);
$_SESSION['List'][$index+1] = $name;
}
else
{
$_SESSION['List'][0] = $name;
}
}
}
?>
<body>
<form method = "post">
<table>
<tr>
<td>
<p>Name</p>
</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td><p>State</p>
</td>
<td>
<select name = "State">
<option value = "PA"> Pennsylvania</option>
<option value = "CA"> California</option>
<option value = "AZ"> Arizona</option>
<option value = "NY"> New York</option>
<option value = "FL"> Florida</option>
<?php
if(isset($_SESSION['List']))
{
foreach($_SESSION['List'] as $name)
{
echo '<option value="'.$name.'">'.$name.'</option>';
}
}
?>
</select>
</td>
</tr>
<td><input type="submit" name = "formSubmit" value="Submit" /></td>
</td>
</tr>
</table>
</form>
</body>
<head>
<title> Add User page</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<form name='f2' action="insert_ac.php" method="post" >
<script src="validation.js" type="javascript/text"></script>
</head>
<body onload="firstfocus();">
<table align="center" border="0" cellpadding="3" cellspacing="1">
<tr>
<td> First Name</td><td > : </td>
<td> <input type='text' name='fname' id='fid' size="50" style="background-color:#abcddd; height:18px;" value='' maxlength="100" onblur="fname_validation(5,12);"> </td>
</tr>
<tr>
<td> Last Name</td><td> : </td>
<td> <input type='text' name='lname' id='lid' size="50" style="background-color:#abcddd; height:18px; "value='' maxlength="100" onblur="lname_validation(5,12);"> </td>
</tr>
<tr>
<td> Gender</td><td> : </td>
<td> <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
</tr>
<tr>
<td> Phone Number</td><td> : </td>
<td> <input type='number_format' name='phone' id='phno'size="50" style="background-color:#abcddd; height:18px; " value=''onblur="allnumeric();"></td>
</tr>
<tr>
<td> Work Experiance</td><td> : </td>
<td><select name="exp" onblur="expselect();">
<option > Select One</option>
<option selected="" value="Default"> Select One </option>
<option value="F"> Fresher </option>
<option value="E"> Experiance</option>
</select></td>
</tr>
<tr>
<td>User Name</td><td>:</td><td> <input type='txt' name='uname' id='uid'size="50" style="background-color:#abcddd; height:18px; " value=''onblur="userid_validation(5,10);"> </td>
</tr>
<tr>
<td>Password</td><td>:</td><td> <input type='password' name='pwd' id='pid' size="50" style="background-color:#abcddd; height:18px; " value=''onblur="passid_validation(7,12);"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td width="84"><input name="enter" class="btn_login" type="submit" value="Submit" onsubmit="alert('Data stored successfully');"/> <input name="cancle" class="btn_login" type="reset" value="Cancle" /></td>
</tr>
</table>
</form>
</body>
</html>
I included the external javascript file using script tag but the validations are not working.
This is my external javascript code which was saved as validation.js and i am unable to find errors in it please help me.
// After form loads focus will go to first name field.
function firstfocus()
{
var fname = document.f2.fname.focus();
return true;
}
// This function will validate First name
function fname_validation(mx,my)
{
var fname = document.f2.fname;
var fname_len = fname.value.length;
var letters = /^[A-Za-z]+$/;
if (fname_len == 0 || fname_len >= my || fname_len < mx)
{
alert("First Name should not be empty / length be between "+mx+" to "+my);
fname.focus();
return false;
if(fname.value.match(letters))
{
// Focus goes to next field i.e.Last Name
document.f2.lname.focus();
return true;
}
}
}
// This function will validate Last name
function lname_validation(mx,my)
{
var lname = document.f2.lname;
var lname_len = lname.value.length;
var letters = /^[A-Za-z]+$/;
if (lname_len == 0 || lname_len >= my || lname_len < mx)
{
alert("Last Name should not be empty / length be between "+mx+" to "+my);
lname.focus();
return false;
if(fname.value.match(letters))
{
// Focus goes to next field i.e.Phone Number
document.f2.phone.focus();
return true;
}
}
}
// This function will validate Phone Number.
function allnumeric()
{
var phone = document.f2.phone;
var numbers = /^[0-9]+$/;
if(phone.value.match(numbers))
{
// Focus goes to next field i.e. Experiance.
document.f2.exp.focus();
return true;
}
else
{
alert('Phone Number must have numeric characters only');
phone.focus();
return false;
}
}
// This function will select Experiance.
function expselect()
{
var exp = document.f2.exp;
if(exp.value == "Default")
{
alert('Select your Experiance from the list');
exp.focus();
return false;
}
else
{
// Focus goes to next field i.e. Username Code.
document.f2.uname.focus();
return true;
}
}
// This function will validate User Name.
function allLetter()
{
var uname = document.f2.uname;
var letters = /^[A-Za-z]+$/;
if(uname.value.match(letters))
{
// Focus goes to next field i.e. Password.
document.f2.pwd.focus();
return true;
}
else
{
alert('Username must have alphabet characters only');
uname.focus();
return false;
}
}
// This function will validate Password.
function passid_validation(mx,my)
{
var passid = document.registration.passid;
var passid_len = passid.value.length;
if (passid_len == 0 ||passid_len >= my || passid_len < mx)
{
alert("Password should not be empty / length be between "+mx+" to "+my);
passid.focus();
return false;
}
}
try to change <script type="javascript/text"> to <script type="text/javascript">
and double check the reference of your js file. Also, put an alert check to determine if it is really going through the firstFocus() function.
You have to reference the .js if its not in the same folder as html
EDIT
function fname_validation(mx,my)
{
is missing a closing bracket!
I changed the java script file and now i can find validations in my form...
Thanks for your help..
here i am sharing my code...
HTML code
<form name='f1' action="insert_ac.php" method="post" onSubmit="return validateForm();" >
<tr>
<td height="25" height="25" colspan="2" bgcolor="#EC6921" class="form_heading">Add User Form</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> First Name:</td>
<td bgcolor="#FFFFFF"> <input type='text' name='fname' id='fid' size="50" style="height:18px;" value='' maxlength="100" ></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Last Name:</td>
<td bgcolor="#FFFFFF" > <input type='text' name='lname' id='lid' size="50" style=" height:18px; "value='' maxlength="100" ></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Gender:</td>
<td bgcolor="#FFFFFF" > <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Phone Number:</td>
<td bgcolor="#FFFFFF" > <input type='number_format' name='phone' id='phno'size="50" style=" height:18px; " value=''></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Work Experiance:</td>
<td bgcolor="#FFFFFF" ><select name="exp" >
<option selected="" value="Default"> Select One </option>
<option value="Fresher"> Fresher </option>
<option value="Experiance"> Experiance</option>
</select></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt">User Name:</td><td bgcolor="#FFFFFF"> <input type='txt' name='uname' id='uid'size="50" style=" height:18px; " value=''></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt">Password:</td><td bgcolor="#FFFFFF"> <input type='password' name='pwd' id='pid' size="50" style=" height:18px; " value=''></td>
</tr>
</table>
<table align="center">
<tr>
<td ><input name="enter" class="btn_login" type="submit" value="Submit" align="center"><input name="cancle" class="btn_login" type="reset" value="Cancle" valign="right" /></td>
</tr>
</table>
</div>
</div>
</div>
</form>
</body>
</html>
Here is my java script file which is working fine....
var RE = /^.+#.+\..{3}$/;
var RE1 = /^[a-zA-Z]+$/;
var RE2 = /^[0-9]{10}$/;
var RE3 = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
function validateForm()
{
if (document.f1.fname.value == "")
{
window.alert("first name should NOT BE empty");
document.f1.fname.focus();
return false;
}
else
if (RE1.test(document.f1.fname.value) == false)
{
alert("Invalid first name\n\
");
return false;
}
else
if (document.f1.fname.value.length < 3)
{
window.alert("Firstname must have atleast Three characters");
document.f1.fname.focus();
return false;
}
else
if (document.f1.lname.value == "")
{
window.alert("Lastname should not be empty");
document.f1.lname.focus();
return false;
}
else
if (RE1.test(document.f1.lname.value) == false)
{
alert("Invalid last name");
return false;
}
else
if (document.f1.lname.value.length < 4)
{
window.alert("Lastname must have atleast four characters");
document.f1.lname.focus();
return false;
}
else
if (document.f1.lname.value == "")
{
window.alert("Last name should not be empty");
document.f1.lname.focus();
return false;
}
else
if (document.f1.phone.value == "")
{
window.alert("phne no should NOT BE empty");
document.f1.phone.focus();
return false;
} else
if (RE3.test(document.f1.phone.value) == false)
{
alert("Invalid phone number");
document.f1.phone.focus();
return false;
}
else
if (document.f1.exp.selectedIndex == 0)
{
window.alert("please select work experiance ");
return false;
}
else
if (document.f1.uname.value == "")
{
window.alert("UserName should not be empty");
document.f1.uname.focus();
return false;
}
else
if (RE1.test(document.f1.uname.value) == false)
{
window.alert("Invalid userName ");
document.f1.uname.focus();
return false;
}
else
if (document.f1.pwd.value == "")
{
window.alert("password should not be empty");
document.f1.pwd.focus();
return false;
}
else
if (document.f1.pwd.value.length < 6)
{
window.alert("password must have atleast six characters");
document.f1.pwd.focus();
return false;
}
else
{
window.alert("User has been added successfully.");
return true;
}
}
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...