【问题标题】:Unable to pass Variable Variable String Value in php无法在php中传递变量变量字符串值
【发布时间】:2014-01-18 20:09:25
【问题描述】:

我有这样的代码:

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

但是它在没有通过检查值的情况下输出。所以它不起作用。我做错了什么?

【问题讨论】:

    标签: php arrays forms checkbox variable-variables


    【解决方案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; 
        }
    }
    

    【讨论】:

    • 感谢 voodoo417。有没有办法只用没有jquery的php来做到这一点?
    • @blackops_programmer 是的,您可以在表单中添加输入 submit,并发布表单数据。但是您无法检查 index.php 上是否存在已检查的复选框(不使用任何脚本)-仅在 page_with_previews.php
    • @blackops_programmer 也可以,你可以在纯 javascript 上完成,无需 jQuery
    • 说如果我使用htmlspecialchars($_SERVER['PHP_SELF']) 进行表单操作,具有预览值的提交按钮并检查index.php 本身中的if ($_SERVER["REQUEST_METHOD"] == "POST")if(isset($_POST['preview']) 等发布数据,那么我会能够检查是否存在选中的复选框,然后将选中的值添加到仅使用 php 出现在预览页面底部的表单中的复选框中?还是 jquery 或 javascript 是我检查选定复选框的唯一选择?
    • @blackops_programmer 如果您想在没有 js 脚本的情况下执行此操作 - 这是一种方式。几分钟 - 我会更新答案。
    猜你喜欢
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多