469,356 Members | 1,965 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

how to keep checkbox checked after click preview button

25
What are some simple ways to maintain checkbox value when user click preview button?

I want to submit post. Then when i click preview button, I want to keep all data on that page. When i choose monday and tuesday from 7 days in check box then when i click preview button i want monday and tuesday still checked and the others uncheck. The problem is when i click preview button all data that i just entered lost.
Apr 16 '10 #1

✓ answered by chathura86

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if (isset($_POST['day']))
  3.     {
  4.         foreach ($_POST['day'] as $selectedDay)
  5.             $selected[$selectedDay] = "checked";
  6.     }
  7. ?>
  8.  
  9. <form action="" method="post">
  10.     <input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
  11.     <input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
  12.     <input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
  13.     <input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
  14.     <input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
  15.     <input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
  16.     <input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
  17.     <br />
  18.     <br />
  19.     <input type="submit" name="Submit" value="Preview" />
  20. </form>
  21.  
Regards

7 10211
chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if (isset($_POST['day']))
  3.     {
  4.         foreach ($_POST['day'] as $selectedDay)
  5.             $selected[$selectedDay] = "checked";
  6.     }
  7. ?>
  8.  
  9. <form action="" method="post">
  10.     <input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
  11.     <input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
  12.     <input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
  13.     <input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
  14.     <input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
  15.     <input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
  16.     <input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
  17.     <br />
  18.     <br />
  19.     <input type="submit" name="Submit" value="Preview" />
  20. </form>
  21.  
Regards
Apr 16 '10 #2
amel86
25
@chathura86
I will try your code..thanx a lot chathura86
Apr 16 '10 #3
amel86
25
chathura..
i already try your code.
but how if the value like monday i want to change to other words that separated with space like 'no days'

when i put 'no days' in $selected['no days'] it show error like below:

The following error occurred when attempting to evaluate this template:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/newgadsc/public_html/includes/adminfunctions_template.php(3950) : eval()'d code on line 1403

This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.
Apr 19 '10 #4
chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if (isset($_POST['day']))
  3.     {
  4.         foreach ($_POST['day'] as $selectedDay)
  5.             $selected[$selectedDay] = "checked";
  6.     }
  7. ?>
  8.  
  9. <form action="" method="post">
  10.     <input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
  11.     <input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
  12.     <input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
  13.     <input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
  14.     <input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
  15.     <input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
  16.     <input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
  17.     <input type="checkbox" name="day[]" <?php echo $selected['no days'] ?> value="no days" />no days<br />
  18.     <br />
  19.     <br />
  20.     <input type="submit" name="Submit" value="Preview" />
  21. </form>
  22.  
did you mean something like this?

Regards
Apr 19 '10 #5
amel86
25
@chathura86
yes..exactly..but it show error when i put the code like this >>> $selected['no days']

it seem like it cant read space. how to slove it chathura?
Apr 19 '10 #6
chathura86
227 100+
the code i just sent is working

Regards
Apr 19 '10 #7
amel86
25
ooo.. its worked!!.. mybe at first i make some mistake, thats why it doesnt work..

thank you chathura!!
Apr 19 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Steph | last post: by
5 posts views Thread by DotNetJunkies User | last post: by
2 posts views Thread by Ceema M via DotNetMonster.com | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.