简体   繁体   English

如何以多部分形式将复选框的数组值传递给下一部分

[英]How to pass array values of checkbox to next part in multi-part form

I have a site based on wordpress. 我有一个基于wordpress的网站。 I need to allow people to create posts from frontend so I have made a multi-part form which works pretty well. 我需要允许人们从前端创建帖子,所以我制作了一个多部分的表格,效果很好。 There are three parts of the form and each part of the form is validated before moving to the next part. 表单分为三部分,并且在移至下一部分之前,将对表单的每个部分进行验证。 Data is passed to another page through hidden inputs . 数据通过隐藏的输入传递到另一页。

My form template looks somewhat like this ( complete code is pretty massive and irrelevant here, so just showing just the relevant parts ) which I hope is enough to give an idea how the form works. 我的表单模板看起来有点像这样(完整的代码在这里非常庞大且无关紧要,因此仅显示相关部分),我希望这足以使您了解表单的工作原理。

MULTI-PART FORM WP-TEMPLATE SAMPLE CODE : 多份表格WP-模板样本代码:

    <?php 
          global $wpdb;
          $this_page    =   $_SERVER['REQUEST_URI'];
          $page     =   $_POST['page'];
          if ( $page == NULL ) { ?> 

          <?php include_once('multiparts/form-files/first_part.php'); ?>    


          <?php } else if ( $page == 1 ) { ?> 

          <?php include_once('multiparts/validation/validate_first_part.php');  
                if (isset($_POST['submit-1']) && (!empty($error))) { ?>

    <div class="error-head">Cannot continue registration. Error/s highlighted below.</div><br/>
            <?php echo $error . '</br>'; ?>


    <?php } else { 
                  include_once('multiparts/form-files/second_part.php');
    }
    ?>


    <?php
    } else if ( $page == 2 ) { ?>

    //SO ON AND SO FORTH

    <?php
    }
    ?>

Recently, I have a added several checkbox fields in the form with an intention to display values of selected checkboxes , in the created posts. 最近,我在表单中添加了几个复选框字段,目的是在创建的帖子中显示所选复选框的值 So here is a relevant html form code that I am currently using. 因此,这是我当前正在使用的相关html表单代码

<fieldset class="work-areas">
<label for="areas" class="label">INTERESTED IN :</label></br>
<div class="work-class">
<input type="checkbox" name="workareas[]" value="administration"/>administration</br>
<input type="checkbox" name="workareas[]" value="technical"/>technical</br>
<input type="checkbox" name="workareas[]" value="creative"/>creative</br>
<input type="checkbox" name="workareas[]" value="fieldwork"/>fieldwork</br>
<input type="checkbox" name="workareas[]" value="marketing"/>marketing</br>
</div>
</fieldset>

I insert the values of these checkboxes into the post just like any other custom fields using this code: add_post_meta($pid, 'areas', $workareas, true ); 就像使用此代码的任何其他自定义字段一样,我将这些复选框的值插入到帖子中: add_post_meta($pid, 'areas', $workareas, true ); . In the processing part it is assigned a meta_key areas . 在处理部分中,分配了meta_key areas I display it in the single.php with the code below : 我用以下代码在single.php中显示它:

<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
    <?php if (is_array($areas)) : ?>

       <h4>INTERESTED AREAS OF WORK:</h4>
      <?php if (is_array($areas))   {
            foreach($areas as $area)    {
            echo '<li>'.$area.'</li>';
            }
         }
      ?>

    <?php endif;?>

ISSUE : All this works well when the above given html form code for checkboxes is in the last/third part of the form. 问题 :当上面给定的html复选框形式的HTML表单代码位于表单的最后/第三部分时,所有这一切都很好。 But it does work when the same checkbox fields is in the second part of the form. 但是,当表单的第二部分中具有相同的复选框字段时,它确实可以工作。 I guess it simply does not pass the array values of the selected checkboxes to the third part. 我猜想它根本不会将所选复选框的数组值传递给第三部分。 Print_r shows an empty array and obviously does not display anything in the single.php too. Print_r显示一个空数组,显然在single.php中也不显示任何内容。 Although I understand that the trouble is just the array of selected checkbox values, NOT being carried to the third part properly, I need help as I am noob to all this. 尽管我知道麻烦仅在于所选复选框值的数组,而不是正确地传递到第三部分,但我需要帮助,因为我对所有这些都不了解。

So the bottomline question is how do I save the array of the selected checkboxes' values in the second part and carry it to the third part and finally assign it to a variable which will hold the array values. 因此,最重要的问题是我如何在第二部分中保存所选复选框的值的数组,并将其带到第三部分,最后将其分配给将保存数组值的变量。 That which can be displayed in post using the code above. 使用上面的代码可以在帖子中显示的内容。

THINGS TRIED : I have looked into this thread here and I am confused where I will insert my checkbox fields and not even sure it applies to my situation. 尝试过的事情 :我在这里研究了这个线程,我很困惑在哪里插入我的复选框字段,甚至不确定它是否适用于我的情况。 I have been able to pass other text input values from one part to another part of the from using something like this : 我已经可以使用类似的方法将其他文本输入值从一个部分传递到另一部分:

<input type="hidden" name="eligible" value="<?php echo $eligible;?>" />

So, I tried using name="workareas[]" but did not work. 因此,我尝试使用name="workareas[]"但没有成功。 I am doing print_r() for everything I am trying and till now have only been getting empty array. 我正在为我正在尝试的所有事情做print_r(),直到现在一直只是空数组。 I am still going through tons of other threads looking for possible hints. 我仍在浏览大量其他线索以寻找可能的提示。 In the meanwhile if you can help, that will be great. 同时,如果您可以提供帮助,那将非常好。 Thanks in advance. 提前致谢。

UPDATE : Solved, please check the answer. 更新:解决,请检查答案。

不是WP用户,但是您的复选框被命名为“ workareas”,但是您似乎在其他任何地方都将它们称为“ areas”。

Thanks for the suggestions in the comments but I have found a graceful and robust solution in my opinion, that is using sessions . 感谢您在评论中提出的建议,但我认为我发现了一个优雅而强大的解决方案,即使用session I followed the basic idea from here which actually deals with a multipart form with sessions. 我遵循了这里的基本思想,该思想实际上是处理带有会话的多部分表单。 Now I have the following code in my third/last part of the form, at the very beginning of the document. 现在,在文档开头的第三部分/最后一部分中,我具有以下代码。 At this time please remember that the html checkbox fields as illustrated above in the question are in the second part. 目前,请记住,问题中上面显示的html复选框字段在第二部分中。

<?php 
   session_start();
     $_SESSION['workareas'] = $_POST['workareas'];
    $result=$_POST['workareas'];
?>

The code is that is repeated during the validation of the third part is the same but this way the variable $result is still holding the values of the array of the selected checkboxes from the second part of the form . 在第三部分的验证过程中重复的代码是相同的,但是通过这种方式,变量$result仍保存着表单第二部分中所选复选框的数组值。

session_start();
$result=$_SESSION['workareas'];

You can do a print_r($result) at this point and check. 您可以在此时执行print_r($result)并进行检查。 Furthermore, if you want to insert these array values to post in order for them to show up in the post just assign meta_key eg. 此外,如果要插入这些数组值以使其在帖子中显示,只需分配meta_key例如。 areas to $result and use the code below to add it as a custom field : $result areas ,并使用以下代码将其添加为自定义字段:

add_post_meta($pid, 'areas', $result, true);

In the single.php you can use the code below to pull values from the array and show. 在single.php中,您可以使用下面的代码从数组中提取值并显示。 Do not avoid if(is_array) statement else wordpress might throw an error warning: invalid arguments supplied foreach(). 不要避免使用if(is_array)语句,否则wordpress可能会引发错误警告:foreach()提供了无效的参数。 Goodluck. 祝好运。

<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
    <?php if (is_array($areas)) : ?>

       <h4>INTERESTED AREAS OF WORK:</h4>
      <?php if (is_array($areas))   {
            foreach($areas as $area)    {
            echo '<li>'.$area.'</li>';
            }
         }
      ?>

    <?php endif;?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM