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!
Related
I didn't build this PHP file, but I am trying to figure out why it is giving me errors. If the "Block layout mode" ACF variable is set to "Modal" then the block works, but gives me this error. "Undefined variable $vids on line 176". If I select the "Embed & Thumbnail" mode, then none of the block is displayed and I get this error. "Call to undefined function vc_embed_layout() on line 252". I can't pin down why this is happening, and I haven't been able to find any unclosed tags or anything in my php file. This is the file in question, and I've marked the lines that trigger errors.
<?php
global $theme_text_domain;
$block_class_identifier = 'blk__vidcol';
// default variables
$block_id = $top_class = $top_style = $inner_class = $inner_style = '';
// check for custom id
$block_id .= BLOCK::set_id( get_sub_field('content_block_id') );
// set block class, $i comes from FLEX class loop and represents the block order on a page
$top_class .= BLOCK::set_standard_classes( $i );
$top_class .= BLOCK::set_custom_classes( get_sub_field('content_block_classes') );
// check for top padding adjustment
$top_class .= BLOCK::set_padding_class( get_sub_field('block_padding_adjustment') );
$top_style .= BLOCK::set_padding_style( get_sub_field('block_padding_adjustment'), get_sub_field('custom_top_padding'), get_sub_field('custom_bottom_padding'));
// check for custom color theme
$top_style .= BLOCK::set_colors( get_sub_field('color_theme'), get_sub_field('text_color'), get_sub_field('custom_background_color') );
// check for width settings
$inner_class .= BLOCK::set_width_class( get_sub_field('content_width') );
$inner_style .= BLOCK::set_width_style( get_sub_field('content_width'), get_sub_field('custom_maximum_content_width') );
$block_assets = get_stylesheet_directory_uri() . '/inc/blocks/' . basename(__FILE__, '.php') . '/assets/';
/*
CUSTOM BLOCK SPECIFIC SETUP
*/
$layout = get_sub_field('block_layout_mode');
$top_class .= ' layout-' . $layout;
// title color
$h2_style = (get_sub_field('color_theme') == 'custom') ? ' color: ' . get_sub_field('custom_title_color') . ';' : '';
/**
* outputs code for the modal version of the video collection block
* #return html output
*/
if (!function_exists('vc_modal_layout')) {
function vc_modal_layout() {
global $block_class_identifier;
$cta_label = get_sub_field('cta_label');
$counter = 0;
$cta = array(
'style' => get_sub_field('cta_style'),
'button_color' => get_sub_field('button_color'),
'button_text_color' => get_sub_field('button_color')
);
$cta_class = BLOCK::get_cta_style( $cta, '.' . $block_class_identifier . ' .button');
?>
<?php // list section ?>
<?php if( have_rows('videos') ): ?>
<div class="vid-container">
<?php while ( have_rows('videos') ) : the_row(); ?>
<?php if ($counter == 0): ?>
<div class="vid-featured">
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
<a class="vid_overlay" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_0" href="javascript:;"></a>
</div>
<!-- modal -->
<div id="<?php echo $block_class_identifier; ?>_vid_0" class="modal video-modal" style="display: none;">
<div class="modal-inner">
<h3><?php echo get_sub_field('video_title'); ?></h3>
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
<div class="vid-list">
<?php else: ?>
<div class="vid-item">
<div class="item-btn">
<a class="button <?php echo $cta_class; ?>" data-fancybox data-src="#<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" href="javascript:;"><?php echo $cta_label; ?></a>
</div>
<div class="item-title"><?php echo get_sub_field('video_title'); ?></div>
</div>
<!-- modal -->
<div id="<?php echo $block_class_identifier; ?>_vid_<?php echo $counter; ?>" class="modal video-modal" style="display: none;">
<div class="modal-inner">
<h3><?php echo get_sub_field('video_title'); ?></h3>
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo get_sub_field('youtube_embed_code'); ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
<?php endif; ?>
<?php $counter++; ?>
<?php endwhile; ?>
</div>
</div>
<?php else: ?>
No videos defined.
<?php endif; ?>
<?
}
}
/**
* outputs code for the embed version of the video collection block
* #return html output
*/
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids, $i) {
?>
<?php if ( $vids ): ?> **this is line 176**
<div class="vid-container2">
<h2 class="vid-title"><?php echo $vids[0]['video_title']; ?></h2>
<div class="vid-container2-inner">
<div class="vid-embed">
<div class="video-wrapper">
<iframe src="https://www.youtube.com/embed/<?php echo $vids[0]['youtube_embed_code']; ?>?rel=0&showinfo=0" frameborder="0" allowfullscreen onload="this.style.visibility = 'visible';"></iframe>
</div>
</div>
<div class="vid-thumbnails">
<?php foreach ($vids as $key => $vid): ?>
<?php $wrap_class = ($key == 0) ? ' tn-hide' : ''; ?>
<div class="tn-wrapper <?php echo $wrap_class; ?>" data-embed="<?php echo $vid['youtube_embed_code']; ?>" data-title="<?php echo $vid['video_title']; ?>">
<img src="https://img.youtube.com/vi/<?php echo $vid['youtube_embed_code']; ?>/mqdefault.jpg" alt="">
</div>
<?php endforeach; ?>
</div>
</div>
<script>
var vid_swap = function() {
$('.block_<?php echo $i; ?> .vid-embed').css({opacity: 1});
$('.block_<?php echo $i; ?> .tn-wrapper').on('click', function(e) {
var $this = $(this);
var embed_id = $this.attr('data-embed');
var title = $this.attr('data-title');
console.log('embed = ' + embed_id);
$('.block_<?php echo $i; ?> .vid-title').animate({opacity: 0}, 100);
$('.block_<?php echo $i; ?> .vid-embed').animate({opacity: 0}, 1000, function() {
$('.block_<?php echo $i; ?> .vid-embed, .block_<?php echo $i; ?> .vid-title').css('visiblity','hidden');
$('.block_<?php echo $i; ?> .vid-title').html( title );
$('.block_<?php echo $i; ?> .vid-embed iframe').attr('src', 'https://www.youtube.com/embed/' + embed_id);
$('.block_<?php echo $i; ?> .tn-wrapper.tn-hide').removeClass('tn-hide');
$this.addClass('tn-hide');
$('.block_<?php echo $i; ?> .vid-title').css('visiblity','visible').delay(500).animate({opacity: 1}, 500);
$('.block_<?php echo $i; ?> .vid-embed').delay(500).animate({opacity: 1}, 1000);
});
});
}
jQuery(document).on('block_init', vid_swap);
</script>
</div>
<?php else: ?>
No videos defined.
<?php endif; ?>
<?php
}
}
?>
<section id="<?php echo $block_id; ?>" class="<?php echo $block_class_identifier; ?> <?php echo $top_class; ?>" style="<?php echo $top_style; ?>">
<div class="inner <?php echo $inner_class; ?>" style="<?php echo $inner_style; ?>">
<?php // title section ?>
<?php if (get_sub_field('title')): ?>
<h2 style="<?php echo $h2_style; ?>"><?php echo get_sub_field('title'); ?></h2>
<?php endif; ?>
<?php $vids = get_sub_field('videos'); ?>
<?php if ($layout == 'modal') vc_modal_layout($vids); ?>
<?php if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**
</div>
</section>
You're not accepting any arguments in vc_modal_layout() function declaration.
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout() {
needs to have parameters in order to accept arguments.
something like this is what you're looking for:
if (!function_exists('vc_embed_layout')) {
function vc_embed_layout($vids=[], $i=0) {
And it's the same idea with vc_modal_layout, the function needs to be able to accept the variables from outside it.
When we call a function, the function needs to be able to accept all arguments passed to it. And all variables we use need to be defined, if there's a risk of them being undefined or empty arrays, we can check them with if(!empty($var)) or if(isset($var))
An alternative to passing the variable to the function is to access it as a global. But it's generally better to pass the variable to the function because when we pass the variable to a function in PHP it works with a copy of the variable's value inside the function. When we do something like this:
$var = 10;
function doIt(){
global $var;
$var++;
}
doit();
echo $var;
we could overcomplicate things..
let's have a closer look here:
if ($layout == 'modal') vc_modal_layout($vids);
if ($layout == 'embed') vc_embed_layout($vids, $i); ?> **This is line 252**
function vc_modal_layout(){ ... }
// should be:
function vc_modal_layout($vids){ ... }
function vc_embed_layout($vids, $i)
// looks good.
Aside from that, nothing stands out as really unusual & I'd want to have a look at the actual site to tinker with this problem further.
I am working on cakephp 1.3..
i have button and value displaying inside button coming from database.
I want to show arrow icon on my active button only..
now the active arrow icon is displaying in all button...inside forloop.
plz help me to do this..
below is my code
<?php
foreach($modes as &$mo)
{
$temp = "Road Vs "; $mo = strtolower($mo);
?>
<li class="active">
<input name="data[Customer][mode]" class="railbtn" type="submit" id="mode" value="<?php echo $temp.$mo; ?>">
<span class="arrow">
<?php echo $this->Html->image('red_arrow.png', array('alt' => '')); ?>
</span>
</li>
<?php } ?>
You should add a condition in your for loop
Like this
<?php
// you need to send button unique name to controller
foreach($modes as &$mo)
{
$temp = "Road Vs "; $mo = strtolower($mo); ?>
<li class="<?php echo ($mo == $selectedUniqueButtonID) ? 'active' : '' ?>">
<input name="data[Customer][mode]" class="railbtn" type="submit" id="mode" value="<?php echo $temp.$mo; ?>">
<?php if($mo == $selectedUniqueButtonID){ ?>
<span class="arrow">
<?php echo $this->Html->image('red_arrow.png', array('alt' => '')); ?>
</span>
<?php } ?>
</li>
<?php
} ?>
Controller code
$selectedUniqueButtonID = $this->data['Customer']['mode'];
$this->set('selectedUniqueButtonID',$selectedUniqueButtonID);
It always drops the first checkbox, but not the actual part of the record the checkbox represents. If I keep clicking 'save' it drops off the next checkbox, and the first record. For instance, if the record is 'DMG, SCR, KLS, AST' when I click 'save' and the page refreshed, the DMG checkbox is not checked but the record still says 'DMG, SCR, KLS, AST' If I click it again, the record becomes 'SCR, KLS, AST' and the SCR checkbox is unchecked. The checkbox part of the form is near the bottom.
Thanks!
// delete
if(isset($_GET['id']) && $_GET['x'] == "d") {
$eventType = EventType::find_by_id($_GET['id']);
if($eventType && $eventType->delete()) {
$message = "The Event Type '{$eventType->name}' was deleted.";
} else {
$message = "The Event Type could not be deleted.";
}
}
// save
if(isset($_POST['submit'])) {
$eventType = new EventType();
if(isset($_POST['id'])) {
$eventType->id = mysql_prep($_POST['id']);
}
$eventType->name = mysql_prep($_POST['name']);
$scoreTypes = ScoreType::find_all();
$score_set = array();
foreach($scoreTypes as $scoreType) {
if(isset($_POST[$scoreType->name]) && $_POST[$scoreType->name] == $scoreType->name) {
array_push($score_set, mysql_prep($_POST[$scoreType->name]));
}
}
if(!empty($score_set)) {
$scores = implode(", ", $score_set);
} else {
$scores = "empty";
}
if(isset($scores)) { $eventType->score_types = $scores; } else { $eventType->score_types = ""; }
if(isset($_POST['is_disp'])) { $eventType->is_disp = mysql_prep($_POST['is_disp']); }
$eventType->save();
}
// edit
if(isset($_GET['id']) && ($_GET['x'] == "e")) {
$eventType = EventType::find_by_id($_GET['id']);
}
//view
$eventTypes = EventType::find_all($ord="name");
$scoreTypes = ScoreType::find_all();
?>
<div id="admin_table">
<div id="admin_thead">
<table>
<tr>
<td width="35%">Name</td>
<td width="40%">Score Types</td>
<td width="5%">Display</td>
<td width="7%">Actions</td>
<td width="12"></td>
</tr>
</table>
</div>
<div id="admin_tdata">
<table>
<colgroup></colgroup>
<colgroup class="odd"></colgroup>
<colgroup></colgroup>
<colgroup class="actions"></colgroup>
<?php foreach($eventTypes as $eventType_v): ?>
<tr>
<td width="35%"><?php echo $eventType_v->name; ?></td>
<td width="40%" title="<?php echo $eventType_v->score_types; ?>"><?php echo $eventType_v->score_types; ?></td>
<td width="5%" class="bool"><img src="../images/<?php echo ($eventType_v->is_disp == 1) ? "x_yes.png" : "x_no.png"; ?>"></td>
<td width="7%" class="but_admin"><a class="but_admin_edit" title="edit" href="index.php?con=event_types&id=<?php echo $eventType_v->id; ?>&x=e">edit</a><a class="but_admin_delete" title="delete" href="index.php?con=event_types&id=<?php echo $eventType_v->id; ?>&x=d" onclick="return confirm('Are you sure you want to delete the Event Type <?php echo "\'{$eventType_v->name}\'"; ?>?')">delete</a></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
<div id="admin_ops_cont">
<div id="admin_lt_panel">
<div id="admin_msg">
<h2>Messages</h2>
<p><?php if(!$message) { echo "no message"; } else { echo $message; } ?></p>
</div>
</div>
<div id="admin_form">
<?php if(isset($_GET['id'])) { if(isset($_GET['x'])) { if($_GET['x'] == "e") { $edit = 1; } else { $edit = 0; }}} else { $edit = 0; } ?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?con=event_types<?php if(isset($_GET['id'])) { echo "&x=e&id={$eventType->id}"; } ?>" method="post">
<fieldset>
<legend><?php if($edit == 1) { echo "Edit: <span>{$eventType->name}</span>"; } else { echo "New EventType"; } ?></legend>
<?php if($edit == 1): ?>
<input type="hidden" name="id" id="id" value="<?php echo $eventType->id; ?>">
<?php endif; ?>
<p>
<label for="name">Name</label>
<input name="name" id="name" type="text"<?php if($edit == 1): ?> value="<?php echo $eventType->name; ?>"<?php endif; ?>>
</p>
<p>
<?php foreach($scoreTypes as $scoreType): ?>
<label for="<?php echo $scoreType->name; ?>"><?php echo $scoreType->description; ?></label>
<input type="checkbox" id="<?php echo $scoreType->name; ?>" name="<?php echo $scoreType->name; ?>" value="<?php echo $scoreType->name; ?>"
<?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")): ?> checked<?php endif; endif; ?>>
<?php endforeach; ?>
</p>
<p>
<label for="is_disp">Display?</label>
<input type="checkbox" id="is_disp" name="is_disp" value="1"<?php if($edit == 1) { if($eventType->is_disp == 1) { echo " checked"; }} ?>>
</p>
<p>
<button type="submit" name="submit">Save</button><?php if($edit == 1): ?><a class="btn_cancel" href="index.php?con=event_types">done</a><?php endif; ?>
</p>
</fieldset>
</form>
</div><!-- end .admin_form -->
</div><!-- end #admin_ops_con -->
I think the problem is in the line
<?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")): ?> checked<?php endif; endif; ?>>
if the entry is in the first position(index in the String is 0) strpos returns 0. PHP assumes that 0 equals false, so the first entry with the match with the index 0 will not be "checked".
this code should solve the problem (checking for false !==)
<?php if($edit == 1): if(strpos("{$eventType->score_types}", "{$scoreType->name}")!==false): ?> checked<?php endif; endif; ?>>
// Not tested since i dont have PHP installed on this machine. but will try to confirm this asap
Update:
Info strpos(...) return the index in the string. Counting starts by 0 so you would have to check explicitly for false (===false) oder (!==false) to be sure, that PHP doesnt treat the index 0 as false. here is a link to the manual (Link)
Here is my code, it basically grabs $banners and displays them out, there are 2 at the moment, however , it stops after the first one and displays out the html div id="footerNews... etc and carries on again after that. This is correct...
<?php if ($banners) { $i = 1; ?>
<div id="footerBanners">
<?php foreach ($banners as $banner) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" />
<?php if ($i == 1) { ?>
<div id="footerNewsletter">
<p>Newsletter Sign Up</p>
Go
<input type="text" name="email" placeholder="Email address" />
</div>
<div id="footerEvents"><?php echo $text_events; ?></div>
<?php } ?>
<?php $i++; } ?>
</div>
<?php } ?>
The problem is that some of variables in $banners dont actually have a link. To get around this, I attempted to put an if statement in to not display <a href> if there is no link. however this messes up the order that of the content, its important that I keep the content in the correct order as above ^. Here was my attempt.
<?php if ($banners) { $i = 1; ?>
<div id="footerBanners">
<?php foreach ($banners as $banner) { ?>
<?php if ($banner['link'] == '') { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" /><?php }
else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" /><?php } ?>
<?php } ?>
<?php if ($i == 1) { ?>
<div id="footerNewsletter">
<p>Newsletter Sign Up</p>
Go
<input type="text" name="email" placeholder="Email address" />
</div>
<div id="footerEvents"><?php echo $text_events; ?></div>
<?php } ?>
<?php $i++; } ?>
</div>
While the code does what it says its supposed to do, its not displaying the correct order anymore, its displaying the 1 linkable $banner then the html, and THEN the image(ie the second $banner with no link. However I need it to display in the same layout as the first bit of code.
Here is the outputted html code, although the image is appearing last on the site.
<div id="footerBanners">
<img src="http://mysite.com/image/data/banner4.jpg" alt="Free Delivery" />
<img src="http://mysite.com/image/data/banner5.jpg" alt="Gift Vouchers" /> <div id="footerNewsletter">
<p>Newsletter Sign Up</p>
Go
<input type="text" name="email" placeholder="Email address" />
</div>
<div id="footerEvents">EVENTS</div>
</div>
Any pointers?
<?php if ($banners) {
echo '<div id="footerBanners">';
foreach ($banners as $banner) {
if ($banner['link'] == '') {
echo '<img src="'.$banner['image'].'" alt="'.$banner['title'].'" />';
}
else {
echo '<img src="'.$banner['image'].'" alt="'.$banner['title'].'" />';
}
if ($first_banner != 'displayed') {
echo '<div id="footerNewsletter">
<p>Newsletter Sign Up</p>
Go
<input type="text" name="email" placeholder="Email address" />
</div>
<div id="footerEvents">'.$text_events.'</div>';
$first_banner = 'displayed';
}
}
echo '</div>';
}
?>
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 } ?>