В приведенном ниже коде я использую поле ретранслятора для хранения ссылок и рангов изображения. В интерфейсе я пытаюсь реализовать что-то вроде этого: когда я выбираю другой ранг для изображения и нажимаю кнопку update rank
, страница должна обновить ранг в базе данных и перезагрузить страницу с новым рангом. Тем не менее, код удаляет ранг полностью при нажатии любой кнопки. При обнаружении неисправностей я обнаружил, что document.write не передает значения функции. Ниже приведен мой код:
<form id="myform" name="myform" action="" method="POST" > <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 "> <?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)) { $allimg=$allrank=array(); while( have_rows('imgs') ): the_row(); $temprank=get_sub_field('rank',$post->ID); $tempimg=get_sub_field('imglink',$post->ID); $allimg[]=$tempimg; $allrank[]=$temprank; if ($temprank=='1st') { $rank1img= $tempimg; $rank1='1st'; } if ($temprank=='2nd') { $rank2img= $tempimg; $rank2='2nd'; } if ($temprank=='3rd') { $rank3img=$tempimg; $rank3='3rd' } endwhile; if (!empty($rank1img)){ ?> <div > <img src="<?php echo $rank1img; ?>" alt="" > <div >1st <select name="rank1"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div ><input type="submit" name="rank1btn" value="update rank" id="rank1btn" onclick="document.write('<?php updatemypage($rank1img,$_POST['rank1']); ?>')" ></div> </div> <?php } if (!empty($rank2img)){ ?> <div >2nd <img src="<?php echo $rank2img; ?>" alt="" > <div > <select name="rank2"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div ><input type="submit" name="rank2btn" value="update rank" id="rank2btn" onclick="document.write('<?php updatemypage($rank2img,$_POST['rank2']); ?>')" ></div> </div> <?php } if (!empty($rank3img)){ ?> <div>3rd <img src="<?php echo $rank3img; ?>" alt="" > <div> <select name="rank3"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div><input type="submit" name="rank3btn" value="update rank" id="rank3btn" onclick="document.write('<?php updatemypage($rank3img,$_POST['rank3']); ?>')" ></div> </div> <?php } ?> } } endforeach; ?> </article> </form> <?php function update_ranks($newimgurl,$newrank){ switch ($newrank) { case "1": $rank="1st"; break; case "2": $rank="2nd"; break; case "3": $rank="3rd"; break; } $field_key = "field_582fdg46"; $value=array(); $count=0; global $totalimgs; global $allimgurl; global $allimgranks; while ($count < $totalimgs){ if ($allimgurl[$count]==$newimgurl){ $value[] = array("imglink" => $newimgurl,"rank" => $rank ); } else{ $value[] = array("imglink" => $allimgurl[$count],"rank" => $allimgranks[$count] ); } $count=$count+1; update_field( $field_key, $value, $post->ID); } unset($newrank,$newimgurl,$currentnewrank,$rank,$count); unset($allimgurl,$allimgranks,$totalimgs,$value); } ?>
с<form id="myform" name="myform" action="" method="POST" > <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 "> <?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)) { $allimg=$allrank=array(); while( have_rows('imgs') ): the_row(); $temprank=get_sub_field('rank',$post->ID); $tempimg=get_sub_field('imglink',$post->ID); $allimg[]=$tempimg; $allrank[]=$temprank; if ($temprank=='1st') { $rank1img= $tempimg; $rank1='1st'; } if ($temprank=='2nd') { $rank2img= $tempimg; $rank2='2nd'; } if ($temprank=='3rd') { $rank3img=$tempimg; $rank3='3rd' } endwhile; if (!empty($rank1img)){ ?> <div > <img src="<?php echo $rank1img; ?>" alt="" > <div >1st <select name="rank1"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div ><input type="submit" name="rank1btn" value="update rank" id="rank1btn" onclick="document.write('<?php updatemypage($rank1img,$_POST['rank1']); ?>')" ></div> </div> <?php } if (!empty($rank2img)){ ?> <div >2nd <img src="<?php echo $rank2img; ?>" alt="" > <div > <select name="rank2"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div ><input type="submit" name="rank2btn" value="update rank" id="rank2btn" onclick="document.write('<?php updatemypage($rank2img,$_POST['rank2']); ?>')" ></div> </div> <?php } if (!empty($rank3img)){ ?> <div>3rd <img src="<?php echo $rank3img; ?>" alt="" > <div> <select name="rank3"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div><input type="submit" name="rank3btn" value="update rank" id="rank3btn" onclick="document.write('<?php updatemypage($rank3img,$_POST['rank3']); ?>')" ></div> </div> <?php } ?> } } endforeach; ?> </article> </form> <?php function update_ranks($newimgurl,$newrank){ switch ($newrank) { case "1": $rank="1st"; break; case "2": $rank="2nd"; break; case "3": $rank="3rd"; break; } $field_key = "field_582fdg46"; $value=array(); $count=0; global $totalimgs; global $allimgurl; global $allimgranks; while ($count < $totalimgs){ if ($allimgurl[$count]==$newimgurl){ $value[] = array("imglink" => $newimgurl,"rank" => $rank ); } else{ $value[] = array("imglink" => $allimgurl[$count],"rank" => $allimgranks[$count] ); } $count=$count+1; update_field( $field_key, $value, $post->ID); } unset($newrank,$newimgurl,$currentnewrank,$rank,$count); unset($allimgurl,$allimgranks,$totalimgs,$value); } ?>
с<form id="myform" name="myform" action="" method="POST" > <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 "> <?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)) { $allimg=$allrank=array(); while( have_rows('imgs') ): the_row(); $temprank=get_sub_field('rank',$post->ID); $tempimg=get_sub_field('imglink',$post->ID); $allimg[]=$tempimg; $allrank[]=$temprank; if ($temprank=='1st') { $rank1img= $tempimg; $rank1='1st'; } if ($temprank=='2nd') { $rank2img= $tempimg; $rank2='2nd'; } if ($temprank=='3rd') { $rank3img=$tempimg; $rank3='3rd' } endwhile; if (!empty($rank1img)){ ?> <div > <img src="<?php echo $rank1img; ?>" alt="" > <div >1st <select name="rank1"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div ><input type="submit" name="rank1btn" value="update rank" id="rank1btn" onclick="document.write('<?php updatemypage($rank1img,$_POST['rank1']); ?>')" ></div> </div> <?php } if (!empty($rank2img)){ ?> <div >2nd <img src="<?php echo $rank2img; ?>" alt="" > <div > <select name="rank2"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div ><input type="submit" name="rank2btn" value="update rank" id="rank2btn" onclick="document.write('<?php updatemypage($rank2img,$_POST['rank2']); ?>')" ></div> </div> <?php } if (!empty($rank3img)){ ?> <div>3rd <img src="<?php echo $rank3img; ?>" alt="" > <div> <select name="rank3"> <option value="0"> </option> <option value="1">1st</option> <option value="2">2nd</option> <option value="3">3rd</option> <option value="4">act</option> </select> </div> <div><input type="submit" name="rank3btn" value="update rank" id="rank3btn" onclick="document.write('<?php updatemypage($rank3img,$_POST['rank3']); ?>')" ></div> </div> <?php } ?> } } endforeach; ?> </article> </form> <?php function update_ranks($newimgurl,$newrank){ switch ($newrank) { case "1": $rank="1st"; break; case "2": $rank="2nd"; break; case "3": $rank="3rd"; break; } $field_key = "field_582fdg46"; $value=array(); $count=0; global $totalimgs; global $allimgurl; global $allimgranks; while ($count < $totalimgs){ if ($allimgurl[$count]==$newimgurl){ $value[] = array("imglink" => $newimgurl,"rank" => $rank ); } else{ $value[] = array("imglink" => $allimgurl[$count],"rank" => $allimgranks[$count] ); } $count=$count+1; update_field( $field_key, $value, $post->ID); } unset($newrank,$newimgurl,$currentnewrank,$rank,$count); unset($allimgurl,$allimgranks,$totalimgs,$value); } ?>