How to remember checkbox choice after submit? - php

I use this code to check which tags are used within a Wordpress category. It renders a checkbox list. I want the form to remember my choice after submit.
This is my code:
<form name="tags" onChange="document.forms.tags.submit();">
<?php
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
$tag_slug[$tag->term_id] = $tag->slug;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
if (!empty($tag_IDs)){
echo '<h3>Het meest geschikt voor</h3>';
echo "<input type=\"radio\" name=\"tag\" value=\"\"> Alles weergeven<br>";
}
foreach($tag_IDs as $tag_ID){
echo '<input type="radio" name="tag" value="'.$tag_slug[$tag_ID].'"> '.$tag_names[$tag_ID].'<br>';
}
?>
</form>
Before I was using this code to remember the choice, but it doesn't work with the above code anymore:
if((isset($_GET["tag"])) && $_GET["tag"] == "tag-example") { echo "checked";}
I thought it would work like this, but it doesn't:
echo '<input type="radio" name="tag" value="'.$tag_slug[$tag_ID].'" '.if((isset($_GET["tag"])) && $_GET["tag"] == "oudere-kinderen") { echo "checked";}.'> '.$tag_names[$tag_ID].'<br>';
How can I get the form to remember the checkbox choice?
Solution:
With help from #Jouke I came to this solution:
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
$tag_slug[$tag->term_id] = $tag->slug;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
if (!empty($tag_IDs)){
echo '<h3>Het meest geschikt voor</h3>';
echo "<input type=\"radio\" name=\"tag\" value=\"\"> Alles weergeven<br>";
}
foreach($tag_IDs as $tag_ID){
$test = $tag_slug[$tag_ID];
echo '<input type="radio" name="tag" value="'.$test.'"' ;
if((isset($_GET["tag"])) && $_GET["tag"] == $test) {
echo ' checked="checked"';
}
echo '> '.$tag_names[$tag_ID].'<br>';
}

Function(wordpress documentation):
<?php checked( $checked, $current, $echo ); ?>
Example:
<input type='radio' name='tag' value='1' <?php checked(isset($variable)); ?>>variable
Another example(php):
$test=test;
echo "<input type='radio' name='tag' value='$test' checked( isset( $test ) ); >";
With escaping:
$tagslug = $tag_slug[$tag_ID];
echo '<input type="radio" name="tag" value=/"$tagslug/" checked( isset( $tagslug ) );>';
Another example without the wordpress function checked:
Paste it in here and test it
$test = "slug";
echo '<input type="radio" name="tag" value="$test"' ;
if (isset($test)) {
echo 'checked="checked"';
}
echo '>';

Try this
echo '<input type="radio" name="tag" value="'.$tag_slug[$tag_ID].'" '.if((isset($_POST["tag"])) && $_POST["tag"] == "oudere-kinderen") { echo "checked=checked";}.'> '.$tag_names[$tag_ID].'';

Related

Select a checkbox by default

I know that this question has been asked before, but I am struggling with selecting a default checkbox. I want the checkbox to be "Kenyan Used" by default as in the picture below:Selected "Kenyan Used. What i have tried is $taxonomy= $wpdb->get_results("SELECT term_id FROM par_taxonomy WHERE (taxonomy = 'condition' AND term_taxonomy_id= '181')");. Though I don't how the below code implements this, I know it is a for loop, but how do i modify it to work for my case?. Below is the specific code from where the taxonomies are fetched from:
<?php
if (!empty($modern_filter)){ $counter = 0;
foreach ($modern_filter as $modern_filter_unit) {
$counter++;
$terms = get_terms(array($modern_filter_unit['slug']), $args);
if (empty($modern_filter_unit['slider']) && $modern_filter_unit['slug'] != 'price') { /*<!--If its not price-->*/
//if ($modern_filter_unit['slug'] != 'price') { /* != 'price'<!--If its not price-->*/
/*First one if ts not image goes on another view*/
if ($counter == 1 and empty($modern_filter_unit['use_on_car_modern_filter_view_images'])
and !$modern_filter_unit['use_on_car_modern_filter_view_images']) {
if (!empty($terms)) { ?>
<div class="stm-accordion-single-unit <?php echo esc_attr($modern_filter_unit['slug']); ?>">
<a class="title" data-toggle="collapse"
href="#<?php echo esc_attr($modern_filter_unit['slug']) ?>" aria-expanded="true">
<h5><?php esc_html_e($modern_filter_unit['single_name'], 'motors'); ?></h5>
<span class="minus"></span>
</a>
<div class="stm-accordion-content">
<div class="collapse in content" id="<?php echo esc_attr($modern_filter_unit['slug']); ?>">
<div class="stm-accordion-content-wrapper">
<?php foreach ($terms as $term): ?>
<?php if (!empty($_GET[$modern_filter_unit['slug']]) and $_GET[$modern_filter_unit['slug']] == $term->slug) { ?>
<script type="text/javascript">
jQuery(window).load(function () {
var $ = jQuery;
$('input[name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"]').click();
$.uniform.update();
});
</script>
<?php } ?>
<div class="stm-single-unit">
<label>
<input type="checkbox"
name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
data-name="<?php echo esc_attr($term->name); ?>"
/>
<?php echo esc_attr($term->name); ?>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<?php } ?>
Thanks for the help in advance.
Assuming that this is the checkbox you want to do this for: Add an if statement to determine if the checkbox should be checked by default
<input type="checkbox"
name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
data-name="<?php echo esc_attr($term->name); ?>"
<?php if($condition){?> checked <?php } ?>
/>
Replace $condition with something to determine if this is the checkbox you want checked by default. If you want all checkbox checked by default just add 'checked' inside the input tag without any if statements.
You need to add the checked attribute to your checkbox. You can check for the required term with an if statement. You need to add the following:
<?php if ($term->name == 'Kenyan Used') { echo ' checked'; } ?>
So then your input code looks like this:
<input type="checkbox"
name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
data-name="<?php echo esc_attr($term->name); ?>"
<?php if ($term->name == 'Kenyan Used') { echo ' checked'; } ?> />
<?php echo esc_attr($term->name); ?>
Hope this helps!

Wordpress Category Thumbnail List Plugin

I'm trying to tweak a Wordpress Plugin - Category Thumbnail List to display "Coming Soon" when the shortcode calls for posts in a category but there are none.
I've tried contacting the developer but had no luck.
I've modified the original code using the following but it doesn't display. Any ideas?
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
Here's the full code:
<?php
/*
Plugin Name: Category Thumbnail List
Plugin URI: http://jonk.pirateboy.net/blog/category/bloggeriet/wordpress/plugins/
Description: Creates a list of thumbnail images, using the_post_thumbnail() in WordPress 2.9 and up.
Version: 1.11
Author: Jonk
Author URI: http://jonk.pirateboy.net
*/
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
$categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
$categoryThumbnailList_OrderType = 'DESC';
}
$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";
define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");
define("categoryThumbnailList_TARGET", "###CATTHMBLST###");
function categoryThumbnailList_callback($listCatId) {
global $post;
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
$tmp_post = $post;
$myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
$output = '<div class="categoryThumbnailList">';
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
$post = $tmp_post;
wp_reset_postdata();
return ($output);
$output = '';
}
function categoryThumbnailList($content) {
return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}
function categoryThumbnailList_css() {
global $categoryThumbnailList_Path;
echo "
<style type=\"text/css\">
#import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>
<?php
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_options_page('Category Thumbnail List Options', 'Category Thumbnail List', 'manage_options', 'category-thumbnail-list', 'my_plugin_options');
}
function my_plugin_options() {
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
if( $_POST['save_category-thumbnail-list_settings'] ) {
// update order type
if( !$_POST['category-thumbnail-list_ordertype'] )
{
$_POST['category-thumbnail-list_ordertype'] = 'date';
}
update_option('category-thumbnail-list_ordertype', $_POST['category-thumbnail-list_ordertype'] );
// update order
if( !$_POST['category-thumbnail-list_order'] )
{
$_POST['category-thumbnail-list_order'] = 'DESC';
}
update_option('category-thumbnail-list_order', $_POST['category-thumbnail-list_order'] );
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
echo "<div id=\"message\" class=\"updated fade\"><p>Your settings are now updated</p></div>\n";
}
?>
<div class="wrap">
<h2>Category Thumbnail List Settings</h2>
<form method="post">
<table class="form-table">
<tr valign="top">
<th scope="row">Order by</th>
<td>
<select name="category-thumbnail-list_ordertype" id="category-thumbnail-list_ordertype">
<option <?php if ($categoryThumbnailList_OrderType == 'date') { echo 'selected="selected"'; } ?> value="date">Date</option>
<option <?php if ($categoryThumbnailList_OrderType == 'title') { echo 'selected="selected"'; } ?> value="title">Title</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Display order</th>
<td>
<select name="category-thumbnail-list_order" id="category-thumbnail-list_order">
<option <?php if ($categoryThumbnailList_Order == 'DESC') { echo 'selected="selected"'; } ?> value="DESC">Descending (z-a/9-1/2010-2001)</option>
<option <?php if ($categoryThumbnailList_Order == 'ASC') { echo 'selected="selected"'; } ?> value="ASC">Ascending (a-z/1-9/2001-2010)</option>
</select>
</td>
</tr>
</table>
<div class="submit">
<!--<input type="submit" name="reset_category-thumbnail-list_settings" value="<?php _e('Reset') ?>" />-->
<input type="submit" name="save_category-thumbnail-list_settings" value="<?php _e('Save Settings') ?>" class="button-primary" />
</div>
<div>
Update the thumbnail sizes here
</div>
<div>
You may need to update your css when changing the thumbnail size
</div>
</form>
</div>
<?php
}
?>
Many Thanks,
Mike
Haven't tested your code, but a quick glance shows at least one problem... you're using this outside of the loop. When not used in the loop, has_post_thumbnail() doesn't know what post you're checking. With just that modification, your code should be this:
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail( $post->ID ) ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;

Remove post count from Wordpress custom widget

I'm a noob running a project theme Wordpress website. There is a widget that shows a "Browse by Category" list in columns.
The widget shows the post count in brackets next to the category heading. I want to remove that.
Any help would be greatly appreciated.
This is the code for the widget:
add_action('widgets_init', 'register_browse_by_category_widget');
function register_browse_by_category_widget() {
register_widget('ProjectTheme_browse_by_category');
}
class ProjectTheme_browse_by_category extends WP_Widget {
function ProjectTheme_browse_by_category() {
$widget_ops = array( 'classname' => 'browse-by-category', 'description' => 'Show all categories and browse by category' );
$control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'browse-by-category' );
$this->WP_Widget( 'browse-by-category', 'ProjectTheme - Browse by Category', $widget_ops, $control_ops );
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
if ($instance['title']) echo $before_title . apply_filters('widget_title', $instance['title']) . $after_title;
$loc_per_row = $instance['loc_per_row'];
$widget_id = $args['widget_id'];
$nr_rows = $instance['nr_rows'];
$only_these = $instance['only_these'];
$only_parents = $instance['only_parents'];
if($only_parents == "on") $only_parents = true;
else $only_parents = false;
$nr = 4;
if(!empty($loc_per_row)) $nr = $loc_per_row;
echo '<style>#'.$widget_id.' #location-stuff li>ul { width: '.(round(100/$nr)-0.5).'%}</style>';
if($nr_rows > 0) $jk = "&number=".($nr_rows * $loc_per_row);
$terms_k = get_terms("project_cat","parent=0&hide_empty=0");
$terms = get_terms("project_cat","parent=0&hide_empty=0".$jk);
//$term = get_term( $term_id, $taxonomy );
if($only_these == "1")
{
$terms = array();
foreach($terms_k as $trm)
{
if($instance['term_' . $trm->term_id] == $trm->term_id)
array_push($terms, $trm);
}
}
//-----------------------------
if(count($terms) < count($terms_k)) $disp_btn = 1;
else $disp_btn = 0;
$count = count($terms); $i = 0;
if ( $count > 0 ){
echo "<ul id='location-stuff'>";
foreach ( $terms as $term ) {
if($i%$nr == 0) echo "<li>";
$total_ads = 0;
$terms2 = '';
$terms2 = get_terms("project_cat","parent=".$term->term_id."&hide_empty=0");
$mese = '';
$mese .= '<ul>';
$mese .= "<img src=\"".get_bloginfo('template_url')."/images/posted.png\" width=\"20\" height=\"20\" />
<h3><a class='parent_taxe' rel='taxe_project_cat_".$term->term_id."' href='".get_term_link($term->slug,"project_cat")."'>" . $term->name;
//."</a></h3>";
$total_ads = ProjectTheme_get_custom_taxonomy_count('project',$term->slug);
$mese2 = '';
if($terms2 && $only_parents == false)
{
foreach ( $terms2 as $term2 )
{
$tt = ProjectTheme_get_custom_taxonomy_count('project',$term2->slug);
$total_ads += $tt;
//$mese2 .= "<li><a href='".get_term_link($term2->slug,"project_cat")."'>" . $term2->name." (".$tt.")</a></li>";
$mese2 .= "<li><a href='".get_term_link($term2->slug,"project_cat")."'>" . $term2->name." </a></li>";
}
}
echo $mese."(".$total_ads.")</a></h3>";
echo '<ul id="_project_cat_'.$term->term_id.'">'.$mese2."</ul>";
echo '</ul>';
if(($i+1) % $nr == 0) echo "</li>";
$i++;
}
//if(($i+1) % $nr != 0) echo "</li>";
echo "</ul>";
}
if($disp_btn == 1)
{
echo '<br/><b>'.__('See More Categories','ProjectTheme').'</b>';
}
echo $after_widget;
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) { ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:95%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('only_parents'); ?>"><?php _e('Only show parent categories'); ?>:</label>
<input type="checkbox" id="<?php echo $this->get_field_id('only_parents'); ?>" name="<?php echo $this->get_field_name('only_parents'); ?>"
<?php echo (esc_attr( $instance['only_parents'] ) == "on" ? "checked='checked'" : ""); ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('loc_per_row'); ?>"><?php _e('Number of Columns'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('loc_per_row'); ?>" name="<?php echo $this->get_field_name('loc_per_row'); ?>"
value="<?php echo esc_attr( $instance['loc_per_row'] ); ?>" style="width:20%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Number of Rows'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('nr_rows'); ?>" name="<?php echo $this->get_field_name('nr_rows'); ?>"
value="<?php echo esc_attr( $instance['nr_rows'] ); ?>" style="width:20%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Only show categories below'); ?>:</label>
<?php echo '<input type="checkbox" name="'.$this->get_field_name('only_these').'" value="1" '.(
$instance['only_these'] == "1" ? ' checked="checked" ' : "" ).' /> '; ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Categories to show'); ?>:</label>
<div style=" width:220px;
height:180px;
background-color:#ffffff;
overflow:auto;border:1px solid #ccc">
<?php
$terms = get_terms("project_cat","parent=0&hide_empty=0");
foreach ( $terms as $term ) {
echo '<input type="checkbox" name="'.$this->get_field_name('term_'.$term->term_id).'" value="'.$term->term_id.'" '.(
$instance['term_'.$term->term_id] == $term->term_id ? ' checked="checked" ' : "" ).' /> ';
echo $term->name.'<br/>';
}
?>
</div>
</p>
<?php
}
}
?>
this is the part of code that writes the number of posts by category ...
change this
echo $mese."(".$total_ads.")</a>
with
echo $mese."</a>";

Replace &ct_= with %2c or comma

I've been wrestling with this function for far to long and I wish I had enough respect to offer a bounty. Help would be greatly appreciated.
// Advanced Search Check
function ct_search_form_check($name, $taxonomy_name = null) {
global $search_values;
if (!$taxonomy_name) {
$taxonomy_name = $name;
} ?>
<?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t) : ?>
<?php if ($search_values[$name] == $t->slug) { $selected = 'checked="checked"'; } else { $selected = ''; } ?>
<div><input id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>" type="checkbox" style="margin-right:5px; margin-left:5px" <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?><span style="margin-left:10px"></span></input></div>
//recently added this part to replace duplicate taxonomy_name in url
<?php $data = array();
while (list($name, $t->slug) = each($arr)) {
$data[] = "$name";
}
echo implode($data); ?>
<?php endforeach; ?>
<?php
}
How do I change my output from
?ct_zipcode=&ct_event_type=alumni&ct_=anneverisy&ct_setting=ballroom&ct_=bar&Venue-search=true
to
?ct_zipcode=&ct_event_type=alumni%2canneverisy&ct_setting=ballroom%2cbar&Venue-search=true
This worked. hopefully it will help someone else
function ct_search_form_check($name, $taxonomy_name = null) {
global $search_values;
if (!$taxonomy_name) {
$taxonomy_name = $name;
} ?>
<input type="hidden" value="" name="ct_<?php echo $name; ?>" />
<?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t) : ?>
<?php if ($search_values[$name] == $t->slug) { $selected = 'checked="checked"'; } else { $selected = ''; } ?>
<div><input id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>" type="checkbox" style="margin-right:5px; margin-left:5px" <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?><span style="margin-left:10px"></span></input></div>
<?php
$data = array();
while (list($name, $t->slug) = each($arr)) {
$data[] = "$name";
}
echo implode($data);
if (!empty($_GET['ct_'])) {
$url = str_replace('&ct_=', '%2c', $_SERVER['QUERY_STRING']);
header("Location: ?$url");
} ?>
<?php endforeach; ?>
<?php
}

Pass checkbox results through step by step form

I have a step by step form that I want to pass checkbox results through to a review page at the end.
There's checkboxes in step 1 and 2, how can I show these on step 3?
I've attempted to do it below but can't get it to echo out the results.
<form class="form" method="POST" action="<?php the_permalink(); ?>">
<? if (!$_POST['step']) { ?>
<input type="hidden" name="step" value="1" />
<div class="steps" style="float:left;">
<p style="font-size:17px!IMPORTANT;"><b>Step 1 of 3</b></p>
</div>
<div class="progress-buttons" style="float:right;">
<button class="next" type="submit" name="submit">Next</button>
</div>
<div class="clear"></div>
<?php $posts = get_field('options');
if( $posts ):
$items = 0;
foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post); ?>
<p style="clear:right;float:right;margin-right:60px;">
<input type="checkbox" name="hardware[]" value="<?php the_title(); ?>">
Select</p>
<?php $items++; endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif; ?>
</div>
</div>
<? } else if ($_POST['step'] == 1) {
foreach($_POST as $name => $value) {
if ($name == "hardware") {
$_SESSION[$name] = $_POST[$name];
} else if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<input type="hidden" name="step" value="2" />
<div class="steps" style="float:left;">
<p style="font-size:17px!IMPORTANT;"><b>Step 2 of 3</b></p>
</div>
<div class="progress-buttons" style="float:right;"> <a class="back" href="<?php the_permalink(); ?>?<?= $field ?>" >Back</a>
<button class="next" type="submit" name="submit">Next</button>
</div>
<?php $posts = get_field('accessories');
if( $posts ):
$items = 0;
foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post); ?>
<p style="clear:right;float:right;margin-right:60px;">
<input type="checkbox" name="accessories[]" value="<?php the_title(); ?>">
Select</p>
<?php $items++; endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
endif; ?>
</div>
</div>
<? } else if ($_POST['step'] == 2) {
foreach($_POST as $name => $value) {
if ($name == "accessories") {
$_SESSION[$name] = $_POST[$name];
} else if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<input type="hidden" name="step" value="3" />
<div class="steps" style="float:left;">
<p style="font-size:17px!IMPORTANT;"><b>Step 3 of 3</b></p>
</div>
<div class="progress-buttons" style="float:right;"> <a class="back" href="<?php the_permalink(); ?>?<?= $field ?>" >Back</a>
<button class="next" type="submit" name="submit">Next</button>
</div>
<div class="clear"></div>
<p>System spec</p>
<?php
$hardware = $_POST['hardware'];
$accessories = $_POST['accessories'];
if( is_array($_SESSION['hardware']) ){
foreach ($_SESSION['hardware'] as $val) {
$hardwareresults .= $val.",\n";
}
}
if( is_array($_SESSION['accessories']) ){
foreach ($_SESSION['accessories'] as $val) {
$accessoriesresults .= $val.",\n";
}
}
?>
<ul>
<li><?php echo $hardwareresults; ?></li>
<li><?php echo $accessoriesresults; ?></li>
</ul>
<? } else if ($_POST['step'] == 3) { //do stuff ?>
Last step
<?php } ?>
From this, it's a bit difficult to tell what your problem is, but you are doing some strange and unnecessary things in the end.
$accessoriesresults .= $val.",\n";
.= is a string operator, dont use it to concatenate arrays. So instead:
$accessoriesresults[] = $val;
But why are you doing this anyway? You already have these values in $_SESSION, why do you want to make another similar array? Why not just print out whats in $_SESSION?
if( is_array($_SESSION['accessories']) ){
foreach ($_SESSION['accessories'] as $val) {
echo "<li>$val</li>";
}
}
You appear to be overwriting your $_SESSION keys:
foreach($_POST as $name => $value) {
if ($name == "hardware") {
//here you just overwrite the 'hardware' key continuously
$_SESSION[$name] = $_POST[$name];
} else if ($name <> "step") {
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
}
}
Written out it looks like:
if ($name == "hardware") {
//here you just overwrite the 'hardware' key continuously
$_SESSION['hardware'] = $_POST['hardware'];
}
So are you storing multiple values or just a single value?
Further you should check you aren't loosing values along the way using var_dump
e.g:
var_dump($_SESSION['hardware']);
if( is_array($_SESSION['hardware']) ){
foreach ($_SESSION['hardware'] as $val) {
$hardwareresults .= $val.",\n";
}
}
var_dump($hardwareresults);
Form persistence, at it's simplest implementation, requires session management. i.e. you need to set the http post values into session. The way you implement this code will vary depending on architecture (i.e. what framework or not you are using), but essentially that's all you need to do.
e.g.
foreach($_POST as $key=>$value){$_SESSION[$key]=$value;}
There nothing seems to be a problem in your script, you are pushing values properly into the session just use these session values in third step, try the following code
<?php } else if ($_POST['step'] == 3) { //do stuff ?>
<?php
echo '<pre>';
print_r($_SESSION['hardware']);
print_r($_SESSION['accessories']);
echo '</pre>';
?>
<?php } ?>

Categories