简体   繁体   中英

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

I have a site based on 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 :

    <?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.

<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 ); . In the processing part it is assigned a meta_key areas . I display it in the single.php with the code below :

<?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. 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. 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. I am doing print_r() for everything I am trying and till now have only been getting empty array. 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 . 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.

<?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 .

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

You can do a print_r($result) at this point and check. 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. areas to $result and use the code below to add it as a custom field :

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. Do not avoid if(is_array) statement else wordpress might throw an error warning: invalid arguments supplied 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;?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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