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 } ?>
Related
I've been racking my brain for weeks trying to remove this block of code, it's from paid memberships pro plugin.
I need to remove the below section as it produces the file fields on the user profile.
need to remove it and put it in a loop of its own and then print it, as I need it to be in a separate <section> from the rest of the fields that are in that original loop.
//----Trying to remove this piece of code----//
?>
<strong><?php echo $field[0]; ?></strong>
<div class="Test"><?php echo pmpromd_display_file_field($meta_field); ?></div>
<?php
//---This is the FullSnippet with the above code included----//
if(!empty($fields_array))
{
foreach($fields_array as $field)
{
if(empty($field[0]))
break;
// Fix for a trailing space in the 'fields' shortcode attribute.
if ( $field[0] === ' ' ) {
break;
}
$field_val = $field[1];
$meta_field = $pu->$field_val;
if(!empty($meta_field))
{
?>
<section class="pmpro_member_directory_<?php echo esc_attr($field[1]); ?>">
<?php
if(is_array($meta_field) && !empty($meta_field['filename']) )
{
//this is a file field
?>
<strong><?php echo $field[0]; ?></strong>
<div class="brendan"><?php echo pmpromd_display_file_field($meta_field); ?></div>
<?php
}elseif(is_array($meta_field)){
//this is a general array, check for Register Helper options first
if(!empty($rh_fields[$field[1]])) {
foreach($meta_field as $key => $value)
$meta_field[$key] = $rh_fields[$field[1]][$value];
}
?>
<strong><?php echo $field[0]; ?></strong>
<ul class="servicesList"><?php echo '<li>'.implode("</li>\n<li>",$meta_field).'</li>';?></ul>
<?php
}
elseif(!empty($rh_fields[$field[1]]) && is_array($rh_fields[$field[1]]) )
{
?>
<strong><?php echo $field[0]; ?></strong>
<?php echo $rh_fields[$field[1]][$meta_field]; ?>
<?php
}
else
{
if($field[1] == 'user_url')
{
?>
<?php echo $field[0]; ?>
<?php
}
else
{
?>
<strong><?php echo $field[0]; ?></strong>
<?php
$meta_field_embed = wp_oembed_get($meta_field);
if(!empty($meta_field_embed)){
echo $meta_field_embed;
}else{
echo make_clickable($meta_field);
}
?>
<?php
}
}
?>
</section>
<?php
}
}
}
?>
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!
I'm thinking there is an easier way to write this code, but not sure on what approach to take. Basically, I want to check if each variable exists, and if so - add the appropriate markup to the page.
Any suggestions would be great.
<?php
$words = get_field('words');
$photography = get_field('photography');
$architect = get_field('architect');
?>
<div class="panel">
<?php if( $words ): ?>
<div>
<p><span>Words</span><span><?php echo $words;?></span></p>
</div>
<?php endif ;?>
<?php if( $photography ): ?>
<div>
<p><span>Photography</span><span><?php echo $photography;?></span></p>
</div>
<?php endif; ?>
<?php if( $architect ): ?>
<div>
<p><span>Architect</span><span><?php echo $architect;?></span></p>
</div>
<?php endif; ?>
</div>
You can use array & loop -
<?php
$fields = array();
$fields['words'] = get_field('words');
$fields['photography'] = get_field('photography');
$fields['architect'] = get_field('architect');
?>
<div class="panel">
<?php foreach($fields as $key => $value):
if($value)
?>
<div>
<p><span><?php echo ucwords($key);?></span><span><?php echo $value;?></span></p>
</div>
<?php
endif;
endforeach;?>
</div>
Create a field array with their appropriate label if you want to have your own labels.
<div class="panel">
<?php
$fields = array (
'words' => 'Words',
'photography' => 'Photography',
'architect' => 'Architect'
);
foreach ( $fields as $field => $label ) {
$value = get_field ( $field );
if (!empty($value)) {
echo '<div>
<p><span>' . $label . '</span><span>' . $value . '</span></p>
</div>';
}
}
?>
</div>
<?php
$required_fields = array("words","photography","architect");
$data = array();
foreach($required_fields as $field)
{
$data[$field] = get_field($field);
}
?>
<div class="panel">
<?php foreach($data as $data_field=>$data_value):
if($data_value)
?>
<div>
<p><span><?=$data_field?></span><span><?=$data_value?></span></p>
</div>
<?php
endif;
endforeach ;?>
</div>
You can create a function like this and then iterate the data that it returns to output your content
<?php
//define a function to fetch data in specific fields
function fetch_data($keys=array()){
$data = array();
foreach($keys as $key){
$val = get_field($key);
if(!empty($val)){
$data[$key] = $val;
}
}
return $data;
}
//now you can easily add or change your fields
$data = fetch_data(array('words','photography','architect'));
?>
<div class="panel">
<?php
foreach($data as $field){
if(isset($data[$field])){
$val = $data[$field];
?>
<div>
<p><span><?php echo ucfirst($field); ?></span><span><?php echo $val; ?></span></p>
</div>
<?php
}
}
?>
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages);
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach ($children as $child) {
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail($child->ID, 'full'); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field('puff_lanktext', $child->ID);
if (get_field("popup_eller_lank", $child->ID) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage open-popup"
href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
</fieldset>
<?php
} elseif (get_field("popup_eller_lank", $child->ID) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage"
href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
<?php
$i++;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
} else {
}
?>
</div>
</div>
<?php } ?>
</div>
</div>
I want every 2 items that's rendered out to be wrapped in a <div class="row">, however I can't figure it out. Can anyone help me?
So basically the row should wrap every 2 elements that is getting looped. I have been stuck on this forever... Hopefully anyone got better expertise than me hehe.
The div="row" should wrap the col-sm-6 and class="puff".
$i = 0;
foreach ($children as $child) {
$i++;
// your code goes here
if($i % 2 == 0) {
echo "</div><div class='row'>";
// reset the counter to 0
$i = 0 ;
}
}
use proper logic
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
First check if its mod by 2 or not (Gives 0 value after MOD), then close div , open new.
Now increase counter . Because for the first time , i will be 0 , then you increment it and then you use logic. So in short counter shoul be incremented at the end only not in between before you do any operation/logic.
Updated
Use code as it is **: issue was you have i++ and your condition in 3rd else if which never executed. So took it outside All and just before foreach.
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages);
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach ($children as $child) {
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail($child->ID, 'full'); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field('puff_lanktext', $child->ID);
if (get_field("popup_eller_lank", $child->ID) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage open-popup"
href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
</fieldset>
<?php
} elseif (get_field("popup_eller_lank", $child->ID) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage"
href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
<?php
} else {
}
?>
</div>
</div>
<?php
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
} ?>
</div>
</div>
First set $i=0;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
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)