The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
PostPosted: Tue Apr 14, 2009 2:21 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Apr 14, 2009 1:32 pm
Posts: 7
I started building my own joomla component in component.php & component.html.php style (not mvc)

I'd like to transform my form to a custom Joomla component and everything works fine, except for the checkboxes.

The problem: If the checkbox is checked in the add-function and I go to the edit-function, uncheck the checkbox. I stays checked. Below is my code. I can understand the it stays checked with my code, but can't think of a solution, without using javascript(don't have any javascript knowledge).

Hopefully someone can help.

Quote:

<tr>
<td><label for="label">label</label></td>
<td><input id="label" name="energielabel" type="checkbox" value="1"<?php if($task = 'edit' and $row->label == '1'){echo 'checked';}?> ></td>
</tr>


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 2:31 pm 
Joomla! Ace
Joomla! Ace

Joined: Thu Jul 10, 2008 2:51 am
Posts: 1284
$task = 'edit' should be $task == 'edit'

The way you have it, you are saying set $task equal to 'edit' (which always returns true as the assignment of value never fails)

_________________
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 3:12 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Apr 14, 2009 1:32 pm
Posts: 7
you are right! Thanks.

I still have the problem that unselecting a checkbox saving it, doesn't put the value to 0 in my db.


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 5:28 pm 
Joomla! Ace
Joomla! Ace

Joined: Thu Jul 10, 2008 2:51 am
Posts: 1284
value="1"

You set the value of it to 1. It's always going to be 1 (or null if it is not checked).

_________________
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 5:49 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Apr 14, 2009 1:32 pm
Posts: 7
I know what you mean, but what would be a solution?

I just enabled tamper data in my firefox browser. And if I select a checkbox it is send by $_POST, but if I unselect a checkbox it will not be send by $_POST.


Top
 Profile  
 
PostPosted: Tue Apr 14, 2009 7:44 pm 
Joomla! Ace
Joomla! Ace

Joined: Thu Jul 10, 2008 2:51 am
Posts: 1284
Yes, that's correct behavior. So, check each checkbox in $_POST - if the value is 1, you know it's checked, and if it is '' (null) you know it wasn't.

_________________
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates


Top
 Profile  
 
PostPosted: Wed Apr 15, 2009 3:50 am 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4785
Location: Toronto, Canada
jnodwell wrote:
Yes, that's correct behavior. So, check each checkbox in $_POST - if the value is 1, you know it's checked, and if it is '' (null) you know it wasn't.

As Jennifer has said, the item will only be sent if the checkbox is selected.

This has always made the checkbox seem to me an odd and difficult form element type because you often have to use special logic to decode it as opposed to other form types where you can generally count on the variable being there in the request.

Ian


Top
 Profile  
 
PostPosted: Wed Apr 15, 2009 6:36 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Apr 14, 2009 1:32 pm
Posts: 7
So checkboxes are not done if I understand it right?

There is no simple solution so I could better choose a dropdown with yes/no or something like it?


Top
 Profile  
 
PostPosted: Wed Apr 15, 2009 10:39 am 
Joomla! Ace
Joomla! Ace

Joined: Thu Jul 10, 2008 2:51 am
Posts: 1284
I don't see how it's not simple. Check for the variable, if it exists, it's set. If it doesn't, it's not set.

If it's easier to conceptualize for you, use a dropdown. Sure.

_________________
http://nodwell.net
http://cjeweb.com
Custom Web Development & Joomla! Templates


Top
 Profile  
 
PostPosted: Wed Apr 15, 2009 1:33 pm 
User avatar
Joomla! Virtuoso
Joomla! Virtuoso

Joined: Sat Sep 24, 2005 11:01 pm
Posts: 4785
Location: Toronto, Canada
jnodwell wrote:
I don't see how it's not simple. Check for the variable, if it exists, it's set. If it doesn't, it's not set.

If it's easier to conceptualize for you, use a dropdown. Sure.


What's more complicated, IIRC (and it has been a while since I've used it), is that if you have a checkbox on your form and you use JTable::bind, you will never be able to uncheck your checkbox unless you add specific code for your checkbox, which makes things more complicated than if checkboxes sent a true or false value.

If you aren't using bind, then you should have no trouble.

Ian


Top
 Profile  
 
PostPosted: Sun Aug 08, 2010 11:26 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Aug 08, 2010 11:10 pm
Posts: 2
Sorry for bumping an old topic, just sharing my solution (more for MVC type component) in case anyone googles here.

It's ugly, but works with current Joomla.

1) Adding the value="1" is good, as else it would be "on" instead, but noone would want to keep that string in database. Polluting the html is the down-side, though.

2) In model, where table store is called set an argument with true, which would set updateNulls to true, so that unchecked checkboxes would update the old value.

3) Set values in JTable inherited classes as null and default values in model, if you would set default value other than null also in JTable, it would be used when it's not sent via POST (when it's unchecked).

That's all. Hope it's better in Joomla 1.6 framework.

EDIT: Oh, one more thing: if you don't want to have null and 1 in database but 0 and 1, then set the value in JTable inherited class as 0 instead of null.


Top
 Profile  
 
PostPosted: Thu Sep 01, 2011 6:43 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jan 12, 2010 6:46 pm
Posts: 10
Having this same problem where an unchecked checkbox doesn't get automatically adjusted by JTable::bind().

Luckily, I had some experience with inserting variables into the $_POST array artificially using JRequest::setVar.

So I just check for the existence of the variable in the $_POST array, and if it's not there then I insert it with a blank value.

Example:

Code:
// Because we have checkboxes on our form, we have to take special precautions.
// An unchecked checkbox does not get included in the $_POST send.
// So we have to check for it's existence and add it if not present.
if(JRequest::getVar("attendeeInterest1") == ""){
   JRequest::setVar( "attendeeInterest1", "");
}
if(JRequest::getVar("attendeeInterest2") == ""){
   JRequest::setVar( "attendeeInterest2", "");
}
if(JRequest::getVar("attendeeInterest3") == ""){
   JRequest::setVar( "attendeeInterest3", "");
}


I added the above just before calling using $row->bind(JTable::get('post')), which marries the $_POST array to your database table instance.

Additional info:
http://api.joomla.org/Joomla-Framework/Environment/JRequest.html#setVar

Anyway, hope this helps. =)
Voila, it works! =)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 



Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group