EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: andyj on May 26, 2014, 02:25:41 AM



Title: Get Checkbox value from EditForm modal dialog in Basic CRUD application
Post by: andyj on May 26, 2014, 02:25:41 AM
I have a datagrid with a pop-up modal dialog to update and insert records based on the Basic CRUD Application in the demo.
On my dialog form I have a checkbox field.
The corresponding database field is a MySQL tinyint which is 0=false and 1=true.
This numeric value has to be converted from and into the form's checkbox.prop('checked',false) or checkbox.prop('checked',true).

To set the value before opening the dialog, I code as follows ("IsActive" is my checkbox field = 1 or 0):
Code:
	function editRecord(){
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg').dialog('open').dialog('setTitle','Edit Class');
$('#fm').form('load',row);
//CHECKBOX START
if(row.IsActive=='1'){
$('#idformIsActive').prop('checked',true);
} else {
$('#idformIsActive').prop('checked',false);
}
//CHECKBOX END
url = 'data_update.php?class_id='+row.id;
}
}
That works fine.

THIS IS WHAT DOESN'T WORK AND I CAN'T WORK OUT WHY.

Translating the checkbox property back to a 1 or 0 when submitting the popup dialog edit form
Code:
	function updateRecord(){
//CHECKBOX START
if ($('#idformIsActive').prop('checked') == true) {
$('#idformIsActive').val('1');
} else {
$('#idformIsActive').val('0');
}
//CHECKBOX END
$('#fm').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
var result = eval('('+result+')');
if (result.success){ //WORKS TO HERE - FORM SUBMITTED BUT DOESN'T CLOSE DIALOG AND RELOAD GRID
$('#dlg').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
} else {
$.messager.show({
title: 'Error',
msg: result.msg
});
}
}
});
}
For some unknown reason it ONLY works when the checkbox is checked so the if condition is true:
Code:
if ($('#idformIsActive').prop('checked') == true) {
$('#idformIsActive').val('1');
} else {
$('#idformIsActive').val('0');
}

What happens when the else clause fires (checkbox is unchecked) is:
  • It correctly converts the checkbox property to a zero
  • The dialog does NOTclose
  • The datagrids does NOT reload
  • The database is however correctly updated with the zero value

It's like this clause is failing when it shouldn't
Code:
if (result.success){

Any ideas? Thanks for your insights!


Title: Re: Get Checkbox value from EditForm modal dialog in Basic CRUD application
Post by: stworthy on May 26, 2014, 03:38:33 AM
What does 'data_update.php?class_id='+row.id return? You can show it out in 'success' function when submitting the form.
Code:
$('#fm').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
  console.log(result);
}
});


Title: Re: Get Checkbox value from EditForm modal dialog in Basic CRUD application
Post by: andyj on May 26, 2014, 07:34:39 AM
In the developer console it says;

Notice: Undefined index: IsActive in /volume1/web/govportal/app/committee2/data_update.php on line 5
{"success":true}
Uncaught SyntaxError: Unexpected token : committee.php:107
$.form.success committee.php:107
cb jquery.easyui.min.js:6091
x.event.dispatch jquery.min.js:5
y.handle

line 5 in data_update.php
Code:
$IsActive 	   = $_REQUEST['IsActive'];
Update file in full:
Code:
<?php
$committee_id  
intval($_REQUEST['committee_id']);
$CommitteeName $_REQUEST['CommitteeName'];
$school_id     = intval($_REQUEST['school_id']);
$IsActive     = $_REQUEST['IsActive'];
$SortOrder     $_REQUEST['SortOrder'];

include 
'../../common/conn.php';

$sql "UPDATE committee SET CommitteeName='$CommitteeName',school_id='$school_id',IsActive='$IsActive',SortOrder='$SortOrder' WHERE committee_id=$committee_id";
$result mysql_query($sql);
if (
$result){
echo json_encode(array('success'=>true));
} else {
echo json_encode(array('msg'=>'Errors occured while attempting to update the record.'));
}
?>

lines 106 and 107 in calling file:
Code:
	return $(this).form('validate');
},


Title: Re: Get Checkbox value from EditForm modal dialog in Basic CRUD application
Post by: stworthy on May 26, 2014, 08:14:03 AM
Some errors occur in your 'data_update.php' file. You need to verify if a variable is set before using it.
Code:
$IsActive = isset($_REQUEST['IsActive']) ? $_REQUEST['IsActive'] : 0;


Title: Re: Get Checkbox value from EditForm modal dialog in Basic CRUD application
Post by: andyj on May 26, 2014, 08:33:11 AM
Thanks very much.
That has certainly resolved the issue.

I still don't understand why the update() function didn't work though... It ought to?