0

I have a code like this:

    $values = array('1A', '2B', '3C', '4D', '5E');
    $checked = array('1A_check', '2B_check', '3C_check', '4D_check', '5E_check');
    $description = array('Description1', 'Description2', 'Description3', 'Description4', 'Description5');

    for ($i=0; $i<count($values); $i++) {

        $$checked[$i] = ""; //Setting this to null since this variable will be set to checked or not in the later step

        $checkbox_form[] = '<input type="checkbox" name="checkbox[]" value="'. $values[$i] .'"'. $$checked[$i] .'>
'. $description[$i] .' <br />';
    }

        foreach ($checkbox_form as $value) {  //Rending the Form
            echo $value;
        }

This code renders the form like this:

<input type="checkbox" name="checkbox[]" value="1A">
Description1 <br />

<input type="checkbox" name="checkbox[]" value="2B">
Description2 <br />

<input type="checkbox" name="checkbox[]" value="3C">
Description3 <br />

<input type="checkbox" name="checkbox[]" value="4D">
Description4 <br />

<input type="checkbox" name="checkbox[]" value="5E">
Description5 <br />

So far so good. What I am trying to do next is, when the user select some of the checkboxes from the box and clicks 'preview', I want them to go to a page which previews the form with the selected checkboxes 'checked'. So I have a code like this to do that:

//After checking what values were posted in the previous screen
$checkbox_posted = array('1A_check', '2B_check');  //Storing the posted checkboxes to this array    

    if (count($checkbox_posted) != 0) {
        foreach ($checkbox_posted as $item) {
            $$item = ' checked';
        }
    }

I thought the above variable variable code will add the 'checked' value to $1A_check and $2B_check variables in line #1 and Line #2 of the form, but it doesnt and the checkboxes arent checked. I thought the form should output like this:

<input type="checkbox" name="checkbox[]" value="1A" checked>
Description1 <br />

<input type="checkbox" name="checkbox[]" value="2B" checked>
Description2 <br />

<input type="checkbox" name="checkbox[]" value="3C">
Description3 <br />

<input type="checkbox" name="checkbox[]" value="4D">
Description4 <br />

<input type="checkbox" name="checkbox[]" value="5E">
Description5 <br />

But instead it outputs without passing the checked value. So it isint working. What have I done wrong?

Neel
  • 9,352
  • 23
  • 87
  • 128

1 Answers1

1

Do it:

index.php(for example):

<form action="page_with_previews.php" id="form_preview" method="post" >
    <?php
      // there render list
    ?>
</form> 
<a id="preview" >Preview</a>

// by jQuery 
<script> 
  $("#preview").click(function(){
      e.preventDefault();
      var cnt = $("#form_preview input[type=checkbox]:checked").size();
      if ( count > 0 )
         $("#form_preview").submit();
  });
</script>

page_with_previews.php(for example):

if ( isset($_POST['checkbox']) {

    foreach (array_filter($_POST['checkbox']) as $item) {
       echo $item; 
    }
}

EDIT Without JS-scripts

index.php(for example):

<?php 
   if (strtolower($_SERVER["REQUEST_METHOD"]) == "post"){

     if ( isset ($_POST['checkbox']) ){
        $url_data = http_build_query($_POST['checkbox']);

        header('Location:page_with_previews.php?'.$url_data);
        die;
      }  
   }
?>   

<form action="" id="form_preview" method="post" >
    <?php
      // there render list
    ?>
    <input type="submit" value="View previews"/>
</form> 

page_with_previews.php(for example):

if ( isset($_GET['checkbox']) {

    foreach ($_GET['checkbox'] as $item) {
       echo $item; 
    }
}
voodoo417
  • 11,861
  • 3
  • 36
  • 40
  • thanks voodoo417. Is there a way to do it with only php without jquery? – Neel Jan 18 '14 at 20:38
  • 1
    @blackops_programmer yes, you can add input `submit` to form, and posting form data. But u could not to check if cheked checkbox exsits on `index.php` (without using any script) - only on `page_with_previews.php` – voodoo417 Jan 18 '14 at 20:41
  • @blackops_programmer also, u can do it on pure javascript, without jQuery – voodoo417 Jan 18 '14 at 20:42
  • Say if I use `htmlspecialchars($_SERVER['PHP_SELF'])` for form action, have submit button with preview value and do a check for post data like `if ($_SERVER["REQUEST_METHOD"] == "POST")` and `if(isset($_POST['preview'])` in index.php itself, would I then be able to check if there are checked checboxes exists and then add a checked value to those checkboxes in the form that will appears at the bottom of the preview page only using php? Or is jquery or javascript is my only option to get the selected checkboxes checked? – Neel Jan 18 '14 at 20:51
  • @blackops_programmer if you want to do it without js scripts - its one way. some minutes - i will update answer. – voodoo417 Jan 18 '14 at 20:54