Welcome Guest, Not a member yet? Register   Sign In
checkbox issue
#1

[eluser]scoobie[/eluser]
Thanks to all who read and hope someone can see an easy fix to this.

My code is

Code:
if (! is_array($attrs)) { $attrs = array( );  }
  function make_checkboxes ($name, $query, $options) {
       foreach ($options as $value => $label) {
         printf('<input type="checkbox" name="%s[]" value="%s" /> %s', $name, $value, $label);        
              if (in_array($value, $query)) { echo "checked "; }    
         echo "<br />\n";
       }
}

this currently works but it puts the word 'checked' next to the checkbox that has been ticked after it is submitted.

I would like it to 'remember' the tick after submit and show the tick rather than the word.

I have found from google a few posts that point to

Code:
&lt;input type="checkbox" name="xxx" &lt;?php if ($xxx == 'VALUE') echo "checked=\"checked\""; ?&gt;" /&gt;

I've tried (and tried) lots of combinations and derivations of above but i think it's not working due to an issue with printf and %. ??

Any suggestions appreciated
#2

[eluser]scoobie[/eluser]
I have been playing around with - still with no joy - and am wondering if someone could tell me where in this part of the syntax it's failing

Code:
printf('&lt;input type="checkbox" name="%s[]" value='&lt;?php if (in_array($value, $query)) { echo "checked "; } '?&gt; /&gt; %s', $name, $value, $label);

the original is like below, and this gives the word 'checked' once the form is submitted, and i need it to show the tick as user entered rather than the word once submitted - so user ticks the checkbox and once it's submitted it redisplays page with the checkoxes ticked

Code:
printf('&lt;input type="checkbox" name="%s[]" value="%s" /&gt; %s', $name, $value, $label);
          if (in_array($value, $query)) { echo "checked "; }

i'm not sure what exactly to put in the value= part to get it to work. this where it's failing.

the error it returns with is

Parse error: syntax error, unexpected '?' in C:\xampp\htdocs\application\views\account\main22.php on line 20

and line 20 is this (same as first code above)

Code:
printf('&lt;input type="checkbox" name="%s[]" value='&lt;?php if (in_array($value, $query)) { echo "checked "; } '?&gt; /&gt; %s', $name, $value, $label);

Any suggestions welcome

Thanks
#3

[eluser]CroNiX[/eluser]
I'd look into the form helper in conjunction with set_value(). It tremendously simplifies what you are doing.
#4

[eluser]scoobie[/eluser]
thx for your suggestion ...

could i ask for further clarification / help ?

when i looked up user guide for set_value it gave an eg of ....

set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function.
The second (optional) parameter allows you to set a default value for the form. Example:
&lt;input type="text" name="quantity" value="&lt;?php echo set_value('quantity', '0'); ?&gt;" size="50" /&gt;

The above form will show "0" when loaded for the first time.

so i put this in the code=

Code:
printf('&lt;input type="checkbox" name="%s[]" value="&lt;?php echo set_value('%s[]', '0'); ?&gt;"  ', $name, $value, $label);

and gave an error of

Parse error: syntax error, unexpected '[' in C:\xampp\htdocs\application\views\account\main22.php on line 20

and then i changed it to

Code:
printf('&lt;input type="checkbox" name="%s[]" value="&lt;?php echo set_value('%s', '0'); ?&gt;"  ', $name, $value, $label);

and error is

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\xampp\htdocs\application\views\account\main22.php on line 20

i'm not sure how to use this function properly (in fact the whole codeigniter i'm having trouble with, perhaps should have put 'newbie' in question),
could you please have a look
at the code below that will give you a working example of what i'm trying to achieve...may be easier ?

make a controller called hello.php
Code:
&lt;?php
class Hello extends CI_Controller{
    public function you()
    {
    $this->load->view('you_view');
    }

}
?&gt;

then in a view called 'you_view'
Code:
&lt;title&gt;Planets&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
if( !isset($_POST['attributes'])) {
$attrs = array( );
$_POST['attributes']=$attrs;
}else{
$attrs = $_POST['attributes'];
}

if (! is_array($attrs)) { $attrs = array( );  }

  function make_checkboxes ($name, $query, $options) {
       foreach ($options as $value => $label) {
         printf('&lt;input type="checkbox" name="%s[]" value="%s" /&gt; %s', $name, $value, $label);

    
         if (in_array($value, $query)) { echo "checked "; }
    // above line echo'es word "checked" instead of tick - not sure how to change the code so tickbox ticked ??
         echo "<br />\n";
       }
}
  
// the list of values and labels for the checkboxes
$planets = array(

   '1' => 'Pluto',
   '2' => 'Mars',
   '3' => 'saturn',
   '4' => 'Earth',
   '6' => 'All Planets / All of the above'
);
?&gt;
&lt;form acti $_SERVER['PHP_SELF'] ?&gt;" method="POST"&gt;
<b><u>STEP ONE</u></b> Select the Planets you want to see (Tick item/s below):<br /> <br /><br />
&lt;?php make_checkboxes('attributes', $attrs, $planets); ?&gt;
<br />
&lt;input type="submit" id="submit" name="s" value="STEP TWO - See these Planets (CLICK here)" /&gt;
&lt;/form&gt;

if you run this controller you'll see that the tickbox never gets reset to a checked state, but
the word 'checked' comes out next to tickbox once STEP TWO submit button is clicked

If you/ anyone knows how to fix this i'd relly appreciate the help - it's driving me mad! thanks.







Theme © iAndrew 2016 - Forum software by © MyBB