EasyUI Forum
May 07, 2024, 03:05:52 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Get Checkbox value from EditForm modal dialog in Basic CRUD application  (Read 13661 times)
andyj
Jr. Member
**
Posts: 57



View Profile
« 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!
« Last Edit: May 26, 2014, 02:28:20 AM by andyj » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 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);
}
});
Logged
andyj
Jr. Member
**
Posts: 57



View Profile
« Reply #2 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');
},
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #3 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;
Logged
andyj
Jr. Member
**
Posts: 57



View Profile
« Reply #4 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?
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!