Question

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?

Was it helpful?

Solution

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; 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top