I'm using form validation jquery. And form input like this
<form id="form-valid">
<select id="country" name="country" class="input-group form-control input-sm">
<option value="Asia">Asia</option>
<option value="America">America</option>
<option value="Eropa">Eropa</option>
<option value="Africa">Africa</option>
<option value="Australia">Australia</option>
</select>
<input type="text" id="codetax" name="codetax"><input>
<select id="status" name="status" class="input-group form-control input-sm">
<option value="small">samll island</option>
<option value="big">big island</option>
<option value="medium">medium island</option>
</select>
<button class="btn btn-default btn-primary btn-sm btn-form-filter form-control" data-target="tmppo" id="btnsave" type="button">Save</button>
</form>
But my problem is, i need to check if country are Asia, taxcode and status must filled. But if country not Asia, taxcode and status can be empty. How do i did that when i'm using submit button?? I'm still new about jquery form validate. Pls help me.
Update ok it's works now, i do this on my from validate using depends
$('#form').validate({
errorElement: 'span',
errorClass: 'help-block',
rules: {
country: {
required: true
},
taxcode: {
required: {
depends: function(element) {
if ($('#country').val() == 'Asia') {
return true;
} else {
return false;
}
}
}
},
},
messages: {
codetax : "Pleasa instert taxcode
},
});
And on code html i have to use "name" to get rules. For all tq for helping me :)
I think you want to use the built-in method supplied by the validate library via .addMethod() found on the documentation site. I modified your form to better conform.
<script>
$(document).ready(function() {
// Add the method here, you can modify the code inside as need be
$.validator.addMethod("country", function(value, element) {
if(value == 'Asia') {
var getCodeTax = $('input[name=codetax]').val();
if(getCodeTax === '') {
return false;
}
}
return true;
}, 'You must select tax code');
$('form').validate({
rules: {
status:"required",
// This applies the country method above to the country dropdown
country: "country"
},
messages: {
}
});
});
</script>
<form id="form-valid" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="country" id="country" class="input-group form-control input-sm">
<option value="">Select</option>
<option value="Asia">Asia</option>
<option value="America">America</option>
<option value="Eropa">Eropa</option>
<option value="Africa">Africa</option>
<option value="Australia">Australia</option>
</select>
<input type="text" id="codetax" name="codetax" />
<select id="status" name="status" class="input-group form-control input-sm">
<option value="small">samll island</option>
<option value="big">big island</option>
<option value="medium">medium island</option>
</select>
<input class="btn btn-default btn-primary btn-sm btn-form-filter form-control" value="save" data-target="tmppo" id="btnsave" type="submit">
</form>
You create a function to check what type of country selected on button click event
$( "btnsave" ).click(function() {
var country = $("country").val();
if (country == 'Asia')
{
//show required message here.
}
}
Change type of the button to submit.
$(document).on('submit', '#form-valid' function(e){
var country = $("#country").val();
if(country == 'Asia'){
if($('#codetax').val()=="" || $('#status').val()==""){
e.preventDefault();
//now form won't submit. Still need to add error messages.
}
}
});
If you don't want to change the type of button to submit then :
$(document).on('click', '#btnsave' function(e){
var country = $("#country").val();
if(country == 'Asia'){
if($('#codetax').val()!="" && $('#status').val()!=""){
$('#form-valid').submit();
}
else{
//add your error messages
}
}
});
Related
HTML:
<select id="s2_basic" class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Javascript:
function submitForm() {
var userid = $('#userid').attr('selected', 'selected');
$.ajax({
type: "POST",
url: "submit.php",
data: "userid=" + userid,
success: function(text) {
if (text == "success") {
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
Submit PHP returns error message:
Value: ([object Object])
I have also tried
var userid = $('#userid').val();
var userid = $('#userid option').val();
Still, the option value never gets passed correctly.
What am I doing wrong?
I want just the value="x" number to be posted as userid=x.
remove id="s2_basic" from your select.
change :
<select id="s2_basic" class="form-control" id="userid" name="userid">
to
<select class="form-control" id="userid" name="userid">
$('#userid').on('change',function(){
console.log('value '+$('#userid option:selected').val())
console.log('text '+$('#userid option:selected').text())
console.log('index '+$('#userid option:selected').index())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select class="form-control" id="userid" name="userid">
<option value="" selected>-- Choose option --</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
try this
My Codeigniter site has side panel and when i click or select any options in side panel appropriate content will appear to the main panel.
Home page URL is http://localhost/main/userinfo
When i click female URL is http://localhost/main/userinfo/female
I made an ajax request to get data from the database. I think i have done a mistake. don't know where it is.
Controller(main.php)
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('userinfo_model');
}
public function index() {}
public function userinfo($gender) {
$this->load->view('home/inc/header_view');
$usermain_data['user_info'] = $this->userinfo_model->get_data($gender);
$this->load->view('home/main_view', $usermain_data);
}
Model(userinfo_model.php)
class Userinfo_model extends CI_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
function get_data($gender) {
$this->db->select('*');
if($gender == 'female'){
$this->db->where('gender', 0);
}
elseif($gender == 'male'){
$this->db->where('gender', 1);
}
elseif($gender == 'allusers'){
$gNames = array(0, 1);
$this->db->where_in('gender', $gNames);
}
else {
redirect(base_url() . 'main/userinfo/allusers');
}
$query = $this->db->get('tble_proposal');
return $query->result();
}}
View(main_view.php)
<div class="container">
<div class="row">
<div class="col-md-3 side_menu">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default gender-label" id="lbl-female" onclick="displayfemale()">
<input type="radio" name="options" id="option1" autocomplete="off">
<span>Female</span>
</label>
<label class="btn btn-default gender-label" id="lbl-male" onclick="displaymale()">
<input type="radio" name="options" id="option2" autocomplete="off">
<span>Male</span>
</label>
</div> <br> <br>
<label class="label nav-label">Age</label>
<select class="btn nav-age-select" id="ageSelect1" autocomplete="off">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21" selected="selected">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
</select>
<label class="label nav-label label-to">To</label>
<select class="btn nav-age-select" id="ageSelect2" autocomplete="off">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25" selected="selected">25</option>
</select>
</div>
<div class="col-md-8 main-body">
<div id="userdata">
<?php
foreach ($user_info as $info) {
//confuse of adding date here with ajax
//echo $info->content . '<br />' . $info->added_date .'<br />';
}
?>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
var ajaxUrl = '<?php echo base_url(); ?>' + 'main/userinfo';
var gender = 'allusers'; // defual 1 for female and male both together
getAjax(ajaxUrl, gender);
$("#lbl-female").click(function () {
gender = 'female';
getAjax(ajaxUrl, gender);
});
$("#lbl-male").click(function () {
gender = 'male';
getAjax(ajaxUrl, gender);
});
});
function getAjax(ajaxUrl, gender){
ajaxUrl = ajaxUrl + '/' + gender;
$.ajax({
url: ajaxUrl,
dataType: "JSON",
type: "POST",
success: function (retdata) {
//append data, Then do i have to change the data select part from the database
}
});
}
You have 2 choices:
Can make AJAX call to different function in the controller that
return a JSON object, the process it with javascript code.
Or you can return a partial view with the just the html you need and place
in userdata container.
My Codeigniter site has side panel and when i click or select any options in side panel appropriate content will appear to the main panel.
Home page URL is http://localhost/main/userinfo/allusers
When i click female button URL is http://localhost/main/userinfo/female
i have following output when i made the Ajax request. Where i did the mistake?
Controller(main.php)
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('userinfo_model');
}
public function index() {}
public function userinfo($gender) {
$this->load->view('home/inc/header_view');
$usermain_data['user_info'] = $this->userinfo_model->get_data($gender);
$this->load->view('home/main_view', $usermain_data);
}
Model(userinfo_model.php)
class Userinfo_model extends CI_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
function get_data($gender) {
$this->db->select('*');
if($gender == 'female'){
$this->db->where('gender', 0);
}
elseif($gender == 'male'){
$this->db->where('gender', 1);
}
elseif($gender == 'allusers'){
$gNames = array(0, 1);
$this->db->where_in('gender', $gNames);
}
else {
redirect(base_url() . 'main/userinfo/allusers');
}
$query = $this->db->get('tble_userinfo');
//return $query->result();
echo(json_encode($query->result()));
}}
View(main_view.php)
<div class="container">
<div class="row">
<div class="col-md-3 side_menu">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default gender-label" id="lbl-female" onclick="displayfemale()">
<input type="radio" name="options" id="option1" autocomplete="off">
<span>Female</span>
</label>
<label class="btn btn-default gender-label" id="lbl-male" onclick="displaymale()">
<input type="radio" name="options" id="option2" autocomplete="off">
<span>Male</span>
</label>
</div> <br> <br>
<label class="label nav-label">Age</label>
<select class="btn nav-age-select" id="ageSelect1" autocomplete="off">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21" selected="selected">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
</select>
<label class="label nav-label label-to">To</label>
<select class="btn nav-age-select" id="ageSelect2" autocomplete="off">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25" selected="selected">25</option>
</select>
</div>
<div class="col-md-8 main-body">
<div id="userdata">
<?php
//foreach ($user_info as $info) {
//confuse of adding date here with ajax
//echo $info->content . '<br />' . $info->added_date .'<br />';
//}
?>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
var ajaxUrl = '<?php echo base_url(); ?>' + 'main/userinfo';
var gender = 'allusers'; // defual 1 for female and male both together
getAjax(ajaxUrl, gender);
$("#lbl-female").click(function () {
gender = 'female';
getAjax(ajaxUrl, gender);
});
$("#lbl-male").click(function () {
gender = 'male';
getAjax(ajaxUrl, gender);
});
});
function getAjax(ajaxUrl, gender){
ajaxUrl = ajaxUrl + '/' + gender;
$.ajax({
url: ajaxUrl,
dataType: "JSON",
type: "POST",
success: function (retdata) {
$("#userdata").html('');
if(retdata.hasOwnProperty("error")){
$("#userdata").html('<div">' + retdata.msg + '</div>');
}
else{
$.each(retdata, function(i){
$("#userdata").append(retdata[i].content + '<br>');
});
}
}
});
}
This answer will have main load both genders initially. Rather than use several gender specific URLs the gender buttons will return the html of the selected gender and dynamically change the html of the page.
main_view.php (with Javascript included)
<div class="container">
<div class="row">
<div class="col-md-3 side_menu">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default gender-label" id="lbl-female">
<input type="radio" name="options" id="option1" autocomplete="off">
<span>Female</span>
</label>
<label class="btn btn-default gender-label" id="lbl-male">
<input type="radio" name="options" id="option2" autocomplete="off">
<span>Male</span>
</label>
</div> <br> <br>
<label class="label nav-label">Age</label>
<select class="btn nav-age-select" id="ageSelect1" autocomplete="off">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21" selected="selected">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
</select>
<label class="label nav-label label-to">To</label>
<select class="btn nav-age-select" id="ageSelect2" autocomplete="off">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25" selected="selected">25</option>
</select>
</div>
<div class="col-md-8 main-body">
<div id="userdata">
<?php
foreach($user_info as $info)
{
echo $info->content.'<br />'.$info->added_date.'<br />';
}
?>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
var ajaxUrl = '<?php echo base_url(); ?>' + 'main/userinfo';
$("#lbl-female").click(function () {
getAjax(ajaxUrl, 'female');
});
$("#lbl-male").click(function () {
getAjax(ajaxUrl, 'male');
});
});
function getAjax(URL, gender) {
$.ajax({
url: URL,
data: {gender: gender},
dataType: "html",
type: "POST",
success: function (retdata) {
$("#userdata").html(retdata);
}
});
}
</script>
Note that in the $.ajax options with the line
data: {gender: gender},
we are "posting" data to the controller which will be retrieved by using $this->input->post("gender"); in the AJAX response function.
Models should not do anything but return data for a controller to use. It is the controller's job to determine what happens based on the data returned. So the redirect() call has been removed.
Userinfo_model.php
class Userinfo_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
// Note the default value for argument $gender.
// This means you do not have to pass an argument.
// If you don't pass one, then !empty($gender) === FALSE
function get_data($gender = NULL)
{
$this->db->select('*');
//select based on gender.
//If no $gender then a "where" clause is not needed and both genders are selected
if(!empty($gender))
//an argument was provided
{
if($gender == 'female')
{
$this->db->where('gender', 0);
}
else
{
$this->db->where('gender', 1);
}
}
$query = $this->db->get('tble_userinfo');
return $query->result();
}
}
Finally, we get to the controller. index() will display both genders by calling get_data() without passing an argument to the model. After that the AJAX responder userinfo() will return html of the desired gender to the ajax success function. The success function will replace the existing html with the new html echoed from userinfo().
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('userinfo_model');
}
public function index()
{
$usermain_data['user_info'] = $this->userinfo_model->get_data();
$this->load
->view('home/inc/header_view')
->view('home/main_view', $usermain_data);
}
//Only used to respond to an AJAX request
public function userinfo()
{
$gender = $this->input->post('gender');
if(empty($gender))
{
$out = "No Users Found";
}
else
{
$user_info = $this->userinfo_model->get_data($gender);
$out = "";
foreach($user_info as $info)
{
$out .= $info->content.'<br />'.$info->added_date.'<br />';
}
}
echo $out;
}
}
This seems reasonably secure so far. The only user input is the gender value. (Never trust user input) Should someone try to pass a value other than the string "female" the database will return only the males info. No user input is presented directly to the database so I don't see any vulnerabilities.
I am submitting a form using jquery and ajax. I have my first select menu and depending on the value of this select menu a div that contains a different menu will become visible.
<table>
<tr>
<td>
<select required="" class="input-medium" id="foulTypes" name="foul" data-original-title="" title="">
<option value="">Select Foul</option>
<option value="85">AID - Aiding Runner</option>
<option value="1">BAT - Illegal Bat</option>
<option value="2">BBW - Block Below Waist</option>
<option value="3">BOB - Blocking Out of Bounds</option>
<option value="4">CHB - Chop Block</option>
</select>
</td>
</tr>
</table
<script>
var foulTypes = $('#foulTypes');
var select = this.value;
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
} else {
$('#catDPI').hide();
}
if ($(this).val() == '85') {
$divSelectedCategory = $("#catILF");
$('#catILF').show();
} else {
$('#catILF').hide();
}
if ($(this).val() == '2') {
$('#catOPI').show();
$divSelectedCategory = $("#catOPI");
} else {
$('#catOPI').hide();
}
//We now enable only for the selected category
$divSelectedCategory.show().find(".category").attr("disabled", false).val('');
});
</script>
<div id="catDPI" style="display:none;">
<select name="category" id="dpiCategory" class='category' style="width:210px;">
<option value="Playing Through Receiver">Playing Through Receiver</option>
<option value="arm bar">arm bar</option>
<option value="Grabbing">Grabbing</option>
</select>
</div>
<div id="catILF" style="display:none;">
<select name="category" id="ilfCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="moving">Moving</option>
<option value="illegal">Illegal</option>
<option value="standing">Standing</option>
</select>
</div>
<div id="catOPI" style="display:none;">
<select name="category" id="opiCategory" class='category' style="width:210px;">
<option value="">Select</option>
<option value="restrict">Restrict</option>
<option value="holding">Holding</option>
</select>
</div>
The foulTypes .change function is working and the correct div is being displayed. The problem I am having is when I submit the form. If the #catDPI is visible the category menu selected value is submitted fine. If #catILF or #catOPI is visible the value for category is blank. I am not using the Ids of the category menus.
$("#submit-foul").submit(function(e){
e.preventDefault();
var category = $(".category").val();
var dataString = 'category=' + category;
if($("#submit-foul").valid()){
$.ajax({
url: 'myUrl',
type: 'POST',
success: function(data){
if(data == 1){
alert('Success');
};
}
});
}
return false;
});
I am not sure what I am doing wrong. Time to send it to the experts!! I appreciate all your help.
Try to change function "foulTypes" and add class in visible select.
foulTypes.change(function () {
//We first disable all, so we dont submit data for more than 1 category
$("#catDPI, #catILF, #catOPI, #catOH, #catDH, #catILD, #catUNS, #catUNR, #catTGT").hide().find(".category").attr("disabled", true);
var $divSelectedCategory;
if ($(this).val() == '1') {
$divSelectedCategory = $("#catDPI");
$('#catDPI').show();
$('#dpiCategory').addClass("visible");
} else {
$('#catDPI').hide();
$('#dpiCategory').removeClass("visible")
}
.... CONTINUE
In your ajax submit..
var category = $(".category.visible").val();
I'm trying to figure out how to have jQuery show/hide a dynamically generated textbox based on a pulldown menu selection. My JS skills are extremely limited. I have the following created in a while loop, depending on the number of people:
<select name="location[<?=$pid?>]" id="location[<?=$pid?>]">
<option value="<?=$loc_id?>" selected><?=$loc_name?> </option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<? $query_loc = mysqli_query($link, "SELECT * FROM locations WHERE loc_app = 'Y' ORDER BY loc_name ASC");
while($fetch_loc = mysqli_fetch_array($query_loc)){
$loc_id = $fetch_loc['loc_id'];
$loc_name = $fetch_loc['loc_name'];
?>
<option value="<?=$loc_id?>"><?=$loc_name?></option>
<?
} ?>
</select>
<input name="location_other[<?=$pid?>]" type="text" id="location_other[<?=$pid?>]" style="display:none" />
jQuery function:
<script type='text/javascript'>
$(window).load(function(){
$('#location').change(function () {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>
I need to keep the id of the and the serialized with the $pid variable for later processing. Is there another way to call the jquery function on these?
The HTML output looks like this:
<select name="location[287]" id="location[287]">
<option value="1" selected>A</option>
<option value="Other">Other</option>
<option value="Other">--------</option>
<option value="12">B</option>
<option value="12">C</option>
</select>
<input name="location_other[287]" type="text" id="location_other[287]" style="display:none" />
Try
$(window).load(function(){
$('#location').change(function () {
$('input[id^=location_other]').hide();
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other' + $(this).val()).hide();
}
});
});
Try this
<select name="location[287]" id="location[287]">
<option value="Other">Other</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<input name="location_other[287]" type="text" id="location_other" style="display:none" />
<script type='text/javascript'>
$(window).load(function() {
if ($('select[id^="location"]').val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
$('select[id^="location"]').change(function() {
if ($(this).val() == "Other") {
$('#location_other').show();
} else {
$('#location_other').hide();
}
});
});
</script>