I have a form which has education div with course, year of completion, university and percent fields, and I am validating those education div fields using Ajax in PHP, and thats working perfectly, but when I clone education div and then if there is any validation error in second (cloned education) div then it reflect it in first (education field).
this is the form:
Educational Details
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="courses">Course Type</label>
<div class="col-md-6">
<select class="form-control" name="course[]" id="course1[]" onblur="validate('course', this.value)">
<option value="0">--Select--</option>
<option value="FullTime">FullTime</option>
<option value="PartTime">PartTime</option>
<option value="Distance Education">Distance Education</option>
<option value="Executive">Executive</option>
</select>
</div>
</div>
<div id="course" class="text-center course"></div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="year">Year of Completion</label>
<div class="col-md-6">
<select class="form-control" name="year[]" id="year1[]" onblur="validate('year', this.value)">
<?php require('layout/educompletionyear.php'); ?>
</select>
</div>
</div>
<div id="year" class="text-center"></div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 label_usity control-label" for="university">University/Institute</label>
<div class="col-md-6">
<select class="form-control" name="university[]" id="university1[]" onblur="validate('university', this.value)">
<?php require('layout/institute.php'); ?>
</select>
</div>
</div>
<div id="university" class="text-center"></div>
</fieldset>
</div>
<div class="col-md-6">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 label_degree control-label" for="degree">Degree</label>
<div class="col-md-6">
<select class="form-control" name="degree[]" id="degree1[]" onblur="validate('degree', this.value)">
<?php require('layout/degree.php'); ?>
</select>
</div>
</div>
<div id="degree" class="text-center"></div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 label_Grade control-label" for="grade">Grade</label>
<div class="col-md-6">
<select class="form-control" name="grade[]" id="grade1[]" onblur="validate('grade', this.value)" >
<option value="-1">--Select--</option>
<option value="A">A</option>
<option value="A+">A+</option>
<option value="B">B</option>
<option value="B+">B+</option>
<option value="C">C</option>
<option value="C+">C+</option>
<option value="D">D</option>
<option value="D+">D+</option>
<option value="E">E</option>
<option value="E+">E+</option>
<option value="F">F</option>
<option value="F+">F+</option>
</select>
</div>
</div>
<div id="grade" class="text-center"></div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="percentage">Percentage</label>
<div class="col-md-6">
<input id="percentage1[]" name="percentage[]" onblur="validate('percentage', this.value)" type="text" placeholder="Percentage" class="input_percentage form-control input-md">
</div>
</div>
<div id="percentage[]" class="text-center"></div>
</fieldset>
</div>
<a class="close" data-dismiss="alert" style="padding-right: 37px;opacity: 1;"><button type="button" class="addbtn btn btn-danger">Remove</button></a>
</div>
this is the ajax validation:
<?php
$value = $_GET['query'];
$formfield = $_GET['field'];
//EDUCATION PART
// Check Valid or Invalid Course type when user enters Course in select option input field.
if ($formfield == "course") {
if ($value =="0") {
echo "Please select Prefered location.";
}else{
}
}
// Check Valid or Invalid year when user enters Year of completion in select option input field.
if ($formfield == "year") {
if ($value =="-1") {
echo "Please select Year of completion.";
}else{
}
}
// Check Valid or Invalid University type when user enters University in select option input field.
if ($formfield == "university") {
if ($value =="-1") {
echo "Please select University.";
}else{
}
}
// Check Valid or Invalid Degree when user enters Degree in select option input field.
if ($formfield == "degree") {
if ($value =="-1") {
echo "Please select Degree.";
}else{
}
}
// Check Valid or Invalid Grade when user enters Grade in select option input field.
if ($formfield == "grade") {
if ($value =="-1") {
echo "Please select Grades.";
}else{
}
}
// Check Valid or Invalid Persentage when user enters Persentage in lastname input field.
if ($formfield == "percentage") {
if (strlen($value) ==0) {
echo "Please enter your Persentage.";
}elseif (!preg_match("/^((0|[1-9]\d?)(\.\d{1,2})?|100(\.00?)?)$/",$value)) {
echo "Only enter valid persentage";
}else {
}
}
?>
Please help to resolve cloned div validation messages part.
Use Jquery validate built-in library to validate forms very easily.
http://jqueryvalidation.org/
I got the way of implementing this....
i am creating an array of error message showing div and then i am also creating one more array for input fields attribut onblur
<div id="percentage[1]" class="text-center percentage"></div>
onblur="validate('percentage[1]', this.value)"
using jquery.
$('#btnAdd').click(function () {
var num = $('.clonedInput').length, // Checks to see how many "duplicatable" input fields we currently have
newNum = new Number(num + 1), // The numeric ID of the new input field being added, increasing by 1 each time
newElem = $('#entry1').clone().attr('id', 'entry' + newNum).fadeIn('slow'); // create the new element via clone(), and manipulate it's ID using newNum value
newElem.find('.percentage').attr('id', 'percentage[' + newNum + ']');
newElem.find('.input_percentage').attr('onblur', 'validate("percentage[' + newNum + ']", this.value)');
and then i am validating these fields in ajjax page.
thank you for helping me...
Related
The scenario is, I have 3 level user (Employer,Manager,Cashier), if the employer login the page, the employer just can see Manager and Cashier option in the select box;if the manager login the page, the manager just can see Cashier option in the select box
User level number I have stored in this variable $user_level, when the user login the id of the user will record in this variable $user_level. In the user value I have set the user level Employer id is 1, Manager id is 2, Cashier id is 3. For example, when the user level 1 (Employer) login the page, the option just can show Manager and Cashier option.When the user level 2 (Manager) login the page, the option just can Cashier option. My problem is how to change below the coding to if/else for conditional option ?
I have declared $user_level = $row['user_level'];
Below is my code:
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Position<span style="color:red;"> *</span></label>
<div class="col-lg-7">
<select class="form-control required" id="position" name="position">
<option value="">Please Select</option>
<option value="2">Manager</option>
<option value="3">Cashier</option>
</select>
<br>
</div>
</div>
Below the coding is check the user login current position(user level number),this part in the future will hidden:
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Current Login User Level<span style="color:red;"> *</span></label>
<div class="col-lg-7">
<input disabled type="text" class="form-control required" id="user_level" name="user_level" value="<?php echo $user_level; ?>">
</div>
</div>
Below is my output example:
Hope someone can guide me to solve this problem. Thanks a lot.
You can add if-else logic inline with the options:
<?php if($user_level != 3) { ?>
<div class="form-group col-lg-6">
<label class="control-label col-lg-4">Position<span style="color:red;"> *</span></label>
<div class="col-lg-7">
<select class="form-control required" id="position" name="position">
<option value="">Please Select</option>
<?php if($user_level == 1) { ?>
<option value="2">Manager</option>
<?php } ?>
<?php if($user_level == 1 || $user_level == 2) { ?>
<option value="3">Cashier</option>
<?php } ?>
</select>
<br>
</div>
</div>
<?php } ?>
Updated for revised requirements.
$batch = htmlentities($_POST['batch']);
if($batch === 'NULL'){
echo "Batch Failed";
} else {
echo "$bacth";
}
<div class="col-md-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<select class="form-control" name="batch" id="select">
<option selected disabled>Batch Preference</option>
<option value="Weekends">Weekends</option>
<option value="Holidays">Holidays</option>
</select>
</div>
</div>
</div>
How to select those select options in form using php, how i can validate them...
Thank You
sorry if the title is bit confusing.i have html form with dropdown and text field and what i want is if user submit the form with errors. an error message will be displayed. Any fields already completed will be left unchanged, allowing the user to simply adjust her input and re-submit the form without having to enter all of the data again,i have manage to do with text field but i having hard time with dropdown list.
here is my html code
<form method="POST" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h4>Choose Type of product</h4>
<div class="row">
<div class="col">
<div class="form-group">
<label >Select type of Product: </label>
<select id="slct3" class="browser-default custom
select" name="Productytype"
onchange="populate1(this.id,'slct4')" >
<span class="error"><?php
echo $ProductytypeErr;?></span>
<option value="">Select</option>
<option value="Import">Import </option>
<option value="Export"></option>
</select>
<span class="error"><?php echo $ProductytypeErr;?>
</span>
</div>
</div>
<div class="col">
<div class="form-group">
<label>Choose Document Type: </label>
<select id="slct4"
class="browser-default custom-select"
name="Documenttype" value="<?php echo
htmlspecialchars($Documenttype);?>"></select>
</div>
<span class="error"><?php echo
$Documenttype;?></span>
</div>
</div>
<h4>Exporter Detail</h4>
<div class="row">
<div class="col">
<div class="form-group">
<label>Name</label>
<input type="text" name="ExportName" class="form-control" value="<?=htmlspecialchars($ExportName);?>">
<span class="error" style="color:red"><?php echo $ExportNameErr;?></span>
</div>
</div>
</div>
</form>
it's long form but that is the main part
here is my php code
<?php
// variable for storing user input
$Productytype = $Documenttype = $ExportName = $ExportEmail = "";
// variable for storing error messages
$ProductytypeErr = $DocumenttypeErr = $ExportNameErr =
$ExportEmailErr="";
$ProductytypeErr = $DocumenttypeErr = $ExportNameErr =
$ExportEmailErr="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Productytype"])) {
$ProductytypeErr = "Missing";
} else {
$Productytype = $_POST["Productytype"];
}
if (isset($_REQUEST['Documenttype']) &&
$_REQUEST['Documenttype'] == '0') {
$DocumenttypeErr = "Missing";
} else {
$DocumenttypeErr = $_POST["DocumenttypeErr"];
}
if (empty($_POST["ExportName"])) {
$ExportNameErr = "Please Enter name of the exporter";
} else {
$ExportName = $_POST["ExportName"];
}
This is an image of how the page looks like,
The 2 big blue buttons at the top are buttons that represent a value from the database. The main function of this button is to help the user to fill in the form in a convenient way leaving only the "Description" to be filled in.
This is my code for the page,
<div class="bodycontainer">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">My Records</h3>
</div>
<div class="panel-body">
<?php
require 'dbfunction.php';
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE day='" . $day . "'");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<div class="admininfobox">
<a class="btn btn-primary">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</a>
</div>
</div>
<?php
}
mysqli_close($con);
}
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Record Activity</div>
<div class="panel-body">
<form name="Create New Admin" class="form-horizontal" method="post" action="handlerecord.php">
<div class="form-group">
<div class="col-sm-4">
<label>Academic Institution</label>
<input list="AcadInst" type="text" class="form-control" placeholder="Institution Name" name="academicInstitution">
<datalist id="AcadInst">
<option value="Singapore Polytechnic (SP)">
<option value="Ngee Ann Polytechnic (NP)">
<option value="Temasek Polytechnic (TP)">
<option value="Republic Polytechnic (RP)">
<option value="Nanyang Polytechnic (NYP)">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Level of Teaching</label>
<input list="LvTeaching" type="text" class="form-control" placeholder="Teaching Stage" name="levelofteaching">
<datalist id="LvTeaching">
<option value="Undergraduate Teaching">
<option value="Postgraduate Teaching">
<option value="Continuing Education">
<option value="Others (Please specify)">
</datalist>
</div>
<div class="col-sm-4">
<label>Type of Teaching</label>
<input list="TyTeaching" type="text" class="form-control" placeholder="Teaching Type" name="typeofteaching">
<datalist id="TyTeaching">
<option value="Clinical Teaching">
<option value="Academic Teaching">
<option value="Talk">
<option value="Others (Please specify)">
</datalist>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<label for="startdate">Start Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="starttime">
</div>
<div class="col-sm-4">
<label for="enddate">End Time</label>
<input type="text" class="form-control" placeholder="Select Time" name="endtime">
</div>
<div class="col-sm-4">
<label for="enddate">Description</label>
<input type="text" class="form-control" placeholder="Optional" name="Description">
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<input type="submit" value="Add" class="btn btn-primary">
</input>
</div>
</div>
</form>
</div>
</div>
An overview of the database,
I am not sure on how to code it. Like Once the user click onto the button, the button goes through the database and input the data accordingly into the form ready for the user to submit.
My suggestion is to create an AJAX request on click.
Create a page loadFormData.php
Accept key data to search in database. For eg: id. Then search in database with this key and get the results. Finally encode this to json (json_encode()) and echo the result.
When the user clicks on the button, trigger an ajax (Javascript) function and send the key values to the loadFormData.php
From the returned json result, extract institution name, start time, end time etc and fill this to form fields using JavaScript
$("#blueButton").click(function() {
$.ajax({
type: "GET",
url: "loadFormData.php",
dataType: "json",
success : function(data) {
// extract each item from the variable 'data'
}
});
});
You should create a form and use action to state the method of entering the values that you want.
For example
http://www.w3schools.com/php/php_forms.asp
You can use this to enter the data easily.
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-name">Type</label>
<div class="col-sm-10">
<select name="name" id="input-name" class="form-control">
<option value="image" selected="selected">image</option>
<option value="text">text</option>
</select>
</div>
</div>
I am using this code . In these if i select option image display only
<div class="function1">Function1</div>
If select option text display only
<div class="function2">Function2</div>
Any one please help me.
Opencart 2 has jQuery included. Assuming the divs with class function1 and function2 are already hidden, you would have:
$('#input-name').change(function(){
if($(this).val() == 'image') {
$('.function1').show();
$('.function2').hide();
}
else if($(this).val() == 'text') {
$('.function2').show();
$('.function1').hide();
}
});