Submit value from first dropdown to second dropdown without reloading the page - php

I have a repair prices calculator on my website, where users can first select the brand from a dropdown and then select the exact model from the second dropdown.
Currently the page reloads after selecting an option from the first dropdown and passes the selected value with $_POST, the second dropdown then shows all devices from the selected brand. After selecting the model, the user gets forwarded to the specific device page.
I would prefer to pass the value and show the second dropdown without the page being reloaded. I have tried doing this with ajax, but wasn't able to get the correct code, so I went back to the first working setup. Could anyone tell me the code I need to submit the value to the second dropdown without reloading?
This is the code I have so far:
<h2>Preisrechner</h2>
<div class="preisrechner-select-wrapper">
<form method="POST" action="">
<div>
<?php
$args = array(
'hierarchical' => 1,
'depth' => 1,
'orderby' => 'name',
'echo' => 0,
'taxonomy' => 'marke',
// this leads to variable name $_POST['marke']
'name' => 'marke-sel'
);
if( ! isset($_POST['marke-sel']) ):
$args['show_option_none'] = 'Hersteller auswählen';
else:
$args['selected'] = $_POST['marke-sel'];
endif;
$marke = wp_dropdown_categories( $args );
// this enables the buttonless js possibility
$marke = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $marke);
echo $marke;
?>
<noscript>
<div>
<input type="submit" value="marke"/>
</div>
</noscript>
</div>
</form>
<?php
if( isset($_POST['marke-sel']) && $_POST['marke-sel'] ):
?>
<form method="POST" action="<? bloginfo('url'); ?>">
<input type="hidden" name="marke" value="<?php echo $_POST['marke-sel'] ?>">
<div>
<?php
$the_query = new WP_Query( array(
'post_type' => 'reparaturpreise',
'tax_query' => array(
array (
'taxonomy' => 'marke',
'field' => 'id',
'terms' => $_POST['marke-sel'],
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""/>
<select name='modell' id='modell' onchange='document.location=this.value'>
<option value="">Modell auswählen</option>
<?php while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_permalink();?>">
<?php the_title(); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<?php endif;
wp_reset_postdata();
$modell = preg_replace("#<select([^>]*)>#", "<select$1 onchange='this.form.submit()'>", $modell);
echo $modell;
?>
<noscript>
<div>
<input type="submit" value="modell"/>
</div>
</noscript>
</div>
</form>
<?php endif; ?>
<?php
if( isset($_POST['marke-sel']) && $_POST['modell'] ):
$args = array(
'post_type' => 'reparaturpreise',
'cat' => $_POST['marke-sel'],
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
?>
</div>

If the code generated by PHP needs to change (ie results) then the page will need to reload. The only way around this would be to move this code to an endpoint which can be called via an AJAX call and returns the results.
A good example which can be changed for your scenario can be found here:
https://premium.wpmudev.org/blog/using-ajax-with-wordpress/

Related

Wordpress - Custom Post Type & Custom Field Issues

I seem to be getting nowhere with this.
I have custom post types set up as Areas.
I'm trying to call all pages under the CPT "areas" into a select box.
<form action="<? bloginfo('url'); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
$args = array(
'numberposts' => -1,
'show_option_none' => __( 'None' ),
'orderby' => 'title',
'hide_empty' => false,
'post_type'=>'areas',
'hierarchical' => true,
'suppress_filters' => true);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="view" />
</form>
This code works but cancels out other custom fields being called further on down the page.
For example, this bit of code works if the other code is removed:
<img src="<?php
if(get_post_meta($post->ID, 'mainimg', true)) {
echo get_post_meta($post->ID, 'mainimg', true);
}
?>">
I'd really appreciate any help. I have no idea what's causing this issue. The closest I came was thinking it's to do with the post ID part but I am genuinely at a loss.
Thank you in advance.
SOLVED
Thanks to Huseyin
Changed $post to $post2
Also changed <?php the_title(); ?> to <?php echo $post2->post_title; ?> as Wordpress was showing the current page title for all options.

Edit form for custom loop post

So, I have a custom loop post.
On the page, I have 10 posts as below:
<?php
$args = array(
'post_type' => 'mail',
'paged'=>$paged,
'posts_per_page' => 10,
'mail_cat' => '',
'orderby' => 'date',
'order' => 'DESC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
Each post gets title, content and price based on the data-mail_id as below:
<div data-mail_id="' . esc_attr( $mail->id ) . '">
<?php the_title(); ?>
<?php the_content(); ?>
<?php echo $mail->get_price_html(); ?>
</div>
Now, how can I add an edit form (<form>) to edit the post contents?
I am guessing I need following:
Get the <form> with the targeted post-mail_id data such as title.
Change the contents of the post
Submit the change which will update the data for that specific post-mail_id.
What do you guys think?
EDIT 1:
Well, I am not sure if this is correct, but it is a start :p
<form role="form" method="post">
<?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?>
<div id="title_change">
<input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>"></input>
<input type="submit" name="update" type="submit" ></input>
</div>
</form>
So, now what? lol
So, I can show the original title in the input field.
I am not sure how the change can be submitted and be updated.

highlight active page option value in select element using "option selected value"

I'm using a custom wp-query on my website to display all pages from a parent page inside a list style with select, when selecting a page I'm redirected to the right page.
works fine :
here is my code :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby'=>'meta_value',
'meta_key'=>'nom_de_loeuvre'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<span class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>A - Z</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_field('nom_de_loeuvre'); ?>
, de
<?php if(get_field('prenom_artiste')): ?>
<?php the_field('prenom_artiste'); ?>
<?php endif ;?>
<?php the_field('nom_artiste'); ?> | Galerie
<?php if(get_field('prenom_galerie')): ?>
<?php the_field('prenom_galerie'); ?>
<?php endif ;?>
<?php the_field('nom_galerie'); ?>
</option>
<?php endwhile; ?>
</select>
</span>
<?php endif; wp_reset_query(); ?>
no I'm trying to hightlight the active page of my list, using "option selected value".
when on a page, I want the select list to display the current page in the liste.
Can anybody help me with this ?
I can't find a solution or a way to do it on the web...
I'm not sure if it's possible, but I guess if it is, It must be something to add in my Wp-query or maybe in jquery ?
thanks a lot for your help...

get sub_field from children pages and sort them alphabetically

I'm using a php code to display children pages from a specific parent page ID to display page links in a select list type.
all my children pages have a sub_field called "artiste", with an artist name.
here is my php code to display the page name with link to the page :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
this works fine, I get all the children page titles and the permalink and sorted alphabetically.
now I want to get instead of the page title, a custom field from each children pages, here is my code :
my custom field is "the_sub_field('nom_artiste');"
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>--- A - Z ---</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(get_field('artiste')): while (has_sub_field('artiste') ): ?><?php the_sub_field('nom_artiste'); ?><?php endwhile; endif; ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
it works also, I get the custom field instead of the page title, and the permalink of my page title, that's perfect.
but in this second code, my custom field are sorted depending of there page title.
for example, I have 2 pages :
AAAAA
ZZZZZ
and custom fields :
for AAAA : custom field is GGGG
for ZZZZ : custom field is BBBB
so with the second php code, my list is not sorted right because it sorted depending on the page title and not of custom field... I get :
GGGG
BBBB
I think I have to create an array with my custom field to be abble to sort them alphabetically before displaying them in my select onchange, but I don't know how to do it...
can anybody help me with this ?
thanks a lot for your help
Try this, replacing everything between <?php while ... endwhile; ?> in your code:
<?php
//Declare an array to hold data
$options = array();
//The loop
while ( $parent->have_posts() ) : $parent->the_post();
//Define a temporary array
$option = array();
//Get all the values we need and store them in an array.
$option['value'] = get_permalink();
$option['title'] = get_the_title();
if(get_field('artiste')):
while (has_sub_field('artiste') ) {
$option['label'] = get_sub_field('nom_artiste');
}
else:
$option['label'] = get_the_title(); //Put something here if artiste is not populated.
endif;
$options[] = $option;
endwhile;
//Sort the array on the 'label' field
usort($options, function($a, $b) { return strcmp($a['label'], $b['label']); });
//Output the <option> tags
$format = ' <option value="%s" title="%s">%s</option>'."\n";
foreach ($options as $o) {
printf($format, $o['value'], $o['title'], $o['label']);
}
?>

Showing only part of the custom meta in wordpress

I have a wordpress code that displays a check list for each wordpress user. The code works fine and I am able to get it to display on a page.
<?php
$args = array( 'post_type' => 'todo_listing', 'posts_per_page' => 3,'order'=>'ASC' );
$loop = new WP_Query( $args );
$_meta_val_arr=array(10=>"All Item",0=>"Cat0",
1=> "cat1",
2=>"cat2",
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="todo" id="<?php echo get_the_ID();?>" itemage="<?php echo get_post_meta(get_the_ID(),'_todotime',true)?>"><a href="javascript:;"
<?php if($all_meta_for_user[get_the_ID()][0]){
?>
class="strike"
<?php
}
?>
>
<?php if($all_meta_for_user[get_the_ID()][0]){?>
<span class="check_box cb"></span>
<?php }else{?>
<span class="uncheck_box cb"></span>
<?php }?>
<p><?php the_title(); ?></p></a>
<?php
endwhile;
?>
I just want to show those items with with Class ="strike" which is saved as meta [1012] => Array ( [0] => 1 ) where 1012 is the todo id.

Categories