Home › Forums › Add-ons › Repeater Field › unable to to change multiple subfields value for repeater on button click
i have a posttype called bookshow with each post having template set ..for each post i have title and repater field called images which has subfields – imagelink & rank. with the code below i am ab to dislay the images correctly but to unable to change their rank when i click on update rank button for each…say i have imageA ranked as 1 an imageB ranked as 2 and imageC as 3, when i chang ranking of imageC to 1 and click on update rank it should change the rank of both image B and A such that A becomes 2 and B becomes 3….but its not happening…for that matter it doesn’t even update the rank of imageC to 1
<?php
global $post;
$bookid=get_the_title($post->ID);;
$args = get_posts(array('post_type' => 'bookgallery' ,'post_title' =>$bookid, 'posts_per_page' => -1
));
?>
<?php
foreach ( $args as $post ) : setup_postdata($post);
if (!empty($post))
{
$activebookimg=$eliminatedbookimg=array();
while( have_rows('images') ): the_row();
$checkrank=get_sub_field('rank',$post->ID);
$checkimg=get_sub_field('imagelink',$post->ID);
if ($checkrank=='1st') {
$rank1img= $checkimg;
}
if ($checkrank=='2nd') {
$rank2img= $checkimg;
}
if ($checkrank=='3rd') {
$rank3img=$checkimg;
}
if ($checkrank=='Active'){
//echo "yay";
$activebookimg[]=$checkimg;
}
endwhile;
if (!empty($rank1img)){
?>
<div >
<img src="<?php echo $rank1img; ?>" alt="" >
<div >1st
<select name="newrank">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">Active</option>
</select>
</div>
<div ><button onclick="update_ranks($post_id,<?php echo $rank1img; ?>,$_POST['newrank'])">update rank</button></div>
</div>
<?php }
if (!empty($rank2img)){
?>
<div >2nd
<img src="<?php echo $rank2img; ?>" alt="" >
<div >
<select name="newrank">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">Active</option>
</select>
</div>
<div ><button onclick="update_ranks($post_id,<?php echo $rank2img; ?>,$_POST['newrank'])"">update rank</button></div>
</div>
<?php }
if (!empty($rank3img)){
?>
<div>3rd
<img src="<?php echo $rank3img; ?>" alt="" >
<div>
<select name="newrank">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">Active</option>
</select>
</div>
<div><button onclick="update_ranks($post_id,<?php echo $rank3img; ?>,$_POST['newrank'])"">update rank</button></div>
</div>
<?php }
$totalactiveimages=count($activebookimg);
$activecount=0;
if ($totalactiveimages>0){
while ($activecount<$totalactiveimages)
{
?>
<div >Active
<img src="<?php echo $activebookimg[$activecount]; ?>" alt="" >
<div>
<select name="newrank">
<option value="0"> </option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">Active</option>
</select>
</div>
<div ><button onclick="update_ranks($post_id,<?php echo $activebookimg[$activecount]; ?>,$_POST['newrank'])"">update rank</button></div>
</div>
<?php
$activecount=$activecount+1;
}
}
}
endforeach; ?>
</article>
<?php
function update_ranks($pid,$nimageurl,$rank){
$field_key = "field_56627856f6895";
$value = array("imagelink" => $nimageurl,"rank" => $rank);
update_field( $field_key, $value, $pid);
}
?>
Hi @littledevil
The onclick
parameter is used for Javascript, not PHP. So your code won’t do anything because it won’t call your PHP update_ranks() function. Please learn more about it here: http://www.w3schools.com/jsref/event_onclick.asp.
If you want to update the field from the front end, I suggest you use the acf_form() instead. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/create-a-front-end-form/.
If you’re not familiar with programming, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.
Hope this helps.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Great video about using ACF to power custom WordPress site builds 🙌 https://t.co/ZX7n1glzxt
— Advanced Custom Fields (@wp_acf) January 11, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.