• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form_checkbox and set_checkbox

#1
[eluser]froginvasion[/eluser]
I'm trying to use both functions in one, as it would make it easier than just writing plain HTML tags. The code that causes me trouble is this one:

Code:
<fieldset>
    <legend>locaties</legend>
    &lt;?php echo form_checkbox('locatie','oudenaarde', set_checkbox('locatie','oudenaarde'));?&gt;3-7-2011 te Oudenaarde <br />
    &lt;?php echo form_checkbox('locatie','arendonk', set_checkbox('locatie','arendonk'));?&gt;31-7-2011 Arendonk<br />
    &lt;?php echo form_checkbox('locatie','westdonk', set_checkbox('locatie','westdonk'));?&gt;11-09-2011 Westhoek – MERKEN<br />
</fieldset>

It should repopulate the checkboxes at validation, but it only sets back one of them, and never more than one. But I don't know why...

#2
[eluser]BufferOverflow[/eluser]
Oh yeah that's my problem too. Though i solved them with my own if/else statements in view. Now for another problem:
http://ellislab.com/forums/viewthread/183935/

#3
[eluser]BrokenLegGuy[/eluser]
nevermind Smile

Ed

#4
[eluser]BufferOverflow[/eluser]
[quote author="BrokenLegGuy" date="1300483067"]nevermind Smile

Ed[/quote]

What kind of attitude is that? If you got no answer don't post anything. Especially crap like 'Never Mind'.

#5
[eluser]BrokenLegGuy[/eluser]
[quote author="BufferOverflow" date="1300484468"][quote author="BrokenLegGuy" date="1300483067"]nevermind Smile

Ed[/quote]

What kind of attitude is that? If you got no answer don't post anything. Especially crap like 'Never Mind'.[/quote]

lol, I had posted an answer but it was my best guess as to what was going on and how to fix it. When I originally went to answer no one else had, I got caught up with a task at work and came back to it to finish my response. When I submitted the post I saw yours where you've actually come across the problem and solved it. I figured that there was no need for my guess when you've provided the answer. So I edited it. Probably should have deleted it but it didn't come to mind at the time.

If you have email notification enabled for subscribed threads then you should have an email with my original response. But thank you for your advice even though it is unnecessarily aggressive and attempting to hijack the thread with a link to your own.

Ed

#6
[eluser]BufferOverflow[/eluser]
[quote author="BrokenLegGuy" date="1300485747"][quote author="BufferOverflow" date="1300484468"][quote author="BrokenLegGuy" date="1300483067"]nevermind Smile

Ed[/quote]

What kind of attitude is that? If you got no answer don't post anything. Especially crap like 'Never Mind'.[/quote]

lol, I had posted an answer but it was my best guess as to what was going on and how to fix it. When I originally went to answer no one else had, I got caught up with a task at work and came back to it to finish my response. When I submitted the post I saw yours where you've actually come across the problem and solved it. I figured that there was no need for my guess when you've provided the answer. So I edited it. Probably should have deleted it but it didn't come to mind at the time.

If you have email notification enabled for subscribed threads then you should have an email with my original response. But thank you for your advice even though it is unnecessarily aggressive and attempting to hijack the thread with a link to your own.

Ed[/quote]

How should i know that you wanted to answer in the first place. I thought you were a troll. I didn't hijack it. It is about the same problems. In forum world users chain similar threads.

#7
[eluser]Konfine[/eluser]
[quote author="froginvasion" date="1300477572"]I'm trying to use both functions in one, as it would make it easier than just writing plain HTML tags. The code that causes me trouble is this one:

Code:
<fieldset>
    <legend>locaties</legend>
    &lt;?php echo form_checkbox('locatie','oudenaarde', set_checkbox('locatie','oudenaarde'));?&gt;3-7-2011 te Oudenaarde <br />
    &lt;?php echo form_checkbox('locatie','arendonk', set_checkbox('locatie','arendonk'));?&gt;31-7-2011 Arendonk<br />
    &lt;?php echo form_checkbox('locatie','westdonk', set_checkbox('locatie','westdonk'));?&gt;11-09-2011 Westhoek – MERKEN<br />
</fieldset>

It should repopulate the checkboxes at validation, but it only sets back one of them, and never more than one. But I don't know why...[/quote]

It does work, I've used it in many applications.

One of the main problems that new starters aren't aware of is that you should include every form item in your form validation file. If you have a config file thats good but it'll also work using the set rules part. You can simply add trim or xssclean which ensures your form data is captured once submitted.

When creating form elements I always use the array function as I almost always use ID's for use with JS, consider the following:

Code:
&lt;?php $data = array('id' => 'locatie', 'name' => 'locatie', 'value' => 'oudenaarde', 'checked' => set_checkbox('locatie', 'oudenaarde', FALSE)); ?&gt;

&lt;?php echo form_checkbox($data); ?&gt;

#8
[eluser]froginvasion[/eluser]
Thanks! Well actually I'm kind of 'cheating' in that I don't validate the form, I'm sending the complete form in html via e-mail. Using these functions I could make sure the form was filled in. But then again, I understand now why it didn't work, I will check this out ASAP! Thanks for the clear answer!

Consider this topic closed.

#9
[eluser]froginvasion[/eluser]
It seems that didn't really fix my problem:
I used the array, but that really doesn't do anything different, so maybe I am missing something here:
I just group my checkboxes by name, and in my rules, it has one rule for this checkboxgroup:

Code:
array('field' => 'locatie','label' => 'Locaties','rules' => 'trim|required'),

So why does it only fill one of them up again, and don't tell me to fiddle with if/else statements to fix this,
that's just... not how it should be.

#10
[eluser]bhumes[/eluser]
set_checkbox returns ' checked="checked"' or an empty string, which is no good for us, as form_checkbox is expecting a boolean. So, I created a replacement function in my helper file. Code is below:

Code:
function set_checkbox($field = '', $value = '', $default = FALSE)
{
  if ( ! isset($_POST[$field]))
  {
   if (count($_POST) === 0 AND $default == TRUE)
   {
    return TRUE;
   }
   return FALSE;
  }

  $field = $_POST[$field];

  if (is_array($field))
  {
   if ( ! in_array($value, $field))
   {
    return FALSE;
   }
  }
  else
  {
   if (($field == '' OR $value == '') OR ($field != $value))
   {
    return FALSE;
   }
  }

  return TRUE;
}

Basically, I am returning TRUEs and FALSEs instead of the aforementioned strings. Works perfectly. Hope this helps someone..


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.