无法在php中传递变量字符串值

时间:2014-01-18 20:09:26

标签: php arrays forms checkbox variable-variables

我有这样的代码:

    $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;
        }

此代码呈现如下形式:

<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 />

到目前为止一切顺利。我接下来要做的是,当用户从框中选择一些复选框并单击“预览”时,我希望他们转到一个页面,该页面预览了所选复选框“已选中”的表单。所以我有这样的代码来做到这一点:

//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';
        }
    }

我认为以上variable variable代码会将“已检查”值添加到表单第1行和第2行中的$1A_check$2B_check变量,但它不会和复选框未检查。我认为表单应该输出如下:

<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 />

但是它输出而不传递检查值。所以它不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:1)

做到:

index.php(例如):

<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(例如):

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

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

<强> 修改 没有JS脚本

index.php(例如):

<?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(例如):

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

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