how to keep checkbox checked after click preview button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amel86
    New Member
    • Apr 2010
    • 25

    how to keep checkbox checked after click preview button

    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.
  • chathura86
    New Member
    • May 2007
    • 227

    #2
    Code:
    <?php
    	if (isset($_POST['day']))
    	{
    		foreach ($_POST['day'] as $selectedDay)
    			$selected[$selectedDay] = "checked";
    	}
    ?>
    
    <form action="" method="post">
    	<input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
    	<input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
    	<input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
    	<input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
    	<input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
    	<input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
    	<input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
    	<br />
    	<br />
    	<input type="submit" name="Submit" value="Preview" />
    </form>
    Regards

    Comment

    • amel86
      New Member
      • Apr 2010
      • 25

      #3
      Originally posted by chathura86
      Code:
      <?php
      	if (isset($_POST['day']))
      	{
      		foreach ($_POST['day'] as $selectedDay)
      			$selected[$selectedDay] = "checked";
      	}
      ?>
      
      <form action="" method="post">
      	<input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
      	<input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
      	<input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
      	<input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
      	<input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
      	<input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
      	<input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
      	<br />
      	<br />
      	<input type="submit" name="Submit" value="Preview" />
      </form>
      Regards
      I will try your code..thanx a lot chathura86

      Comment

      • amel86
        New Member
        • Apr 2010
        • 25

        #4
        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(39 50) : 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.

        Comment

        • chathura86
          New Member
          • May 2007
          • 227

          #5
          Code:
          <?php
          	if (isset($_POST['day']))
          	{
          		foreach ($_POST['day'] as $selectedDay)
          			$selected[$selectedDay] = "checked";
          	}
          ?>
          
          <form action="" method="post">
          	<input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
          	<input type="checkbox" name="day[]" <?php echo $selected['no days'] ?> value="no days" />no days<br />
          	<br />
          	<br />
          	<input type="submit" name="Submit" value="Preview" />
          </form>
          did you mean something like this?

          Regards

          Comment

          • amel86
            New Member
            • Apr 2010
            • 25

            #6
            Originally posted by chathura86
            Code:
            <?php
            	if (isset($_POST['day']))
            	{
            		foreach ($_POST['day'] as $selectedDay)
            			$selected[$selectedDay] = "checked";
            	}
            ?>
            
            <form action="" method="post">
            	<input type="checkbox" name="day[]" <?php echo $selected['monday'] ?> value="monday" />monday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['tuesday'] ?> value="tuesday" />tuesday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['wednesday'] ?> value="wednesday" />wednesday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['thursday'] ?> value="thursday" />thursday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['friday'] ?> value="friday" />friday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['saturday'] ?> value="saturday" />saturday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['sunday'] ?> value="sunday" />sunday<br />
            	<input type="checkbox" name="day[]" <?php echo $selected['no days'] ?> value="no days" />no days<br />
            	<br />
            	<br />
            	<input type="submit" name="Submit" value="Preview" />
            </form>
            did you mean something like this?

            Regards
            yes..exactly..b ut 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?

            Comment

            • chathura86
              New Member
              • May 2007
              • 227

              #7
              the code i just sent is working

              Regards

              Comment

              • amel86
                New Member
                • Apr 2010
                • 25

                #8
                ooo.. its worked!!.. mybe at first i make some mistake, thats why it doesnt work..

                thank you chathura!!

                Comment

                Working...