如何使用isset在提交和POST页面刷新后显示新数据



我使用以下方法来POST一个新文件,并且运行良好:

HTML

<div class="row">
<div class="col-xs-12">
<form method="POST" action="" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="postImage" multiple="multiple" class="form-control">
</div>
<input id="uploadImg" name="uploadImgCustom" type="submit" value="AGGIORNA ALLEGATO" class="btn secondary-btn primary-bg">
</form>
</div>
</div>

上传逻辑

$myNewImg = get_post_meta($id, 'usp-file-single', true);
if (isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['uploadImgCustom'];
}
$attachments = get_posts(array( 
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' =>'any',
'post_parent' => $id
));
if ($attachments) {
foreach ( $attachments as $attachment ) {
$myNewImg = wp_get_attachment_url( $attachment->ID );
update_post_meta( $id, 'usp-file-single', $myNewImg);
} 
$pathtofile = $myNewImg;
$info = pathinfo($pathtofile);
if ( ($info["extension"] == "jpg") || ($info["extension"] == "png") ) { ?>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $myNewImg; ?>" class="bnr_img img-responsive center-block" alt="">
</a>
<?php } else { 
// Get the Video Fields
$video_mp4 =  get_post_meta($id, 'usp-file-single', TRUE);
// Build the  Shortcode
$attr =  array(
'mp4'      => $video_mp4,
'webm'     => $video_webm,
'flv'      => $video_flv,
'poster'   => $video_poster,
'preload'  => 'auto',
'autoplay' => "off"
);
// Display the Shortcode
echo wp_video_shortcode(  $attr );
} 
}

自检

if($_SERVER['REQUEST_METHOD']=="POST") {
if ('AGGIORNA ALLEGATO' === ($_POST['uploadImgCustom'])) {
if ($_FILES['postImage']) {
$attachments = get_attached_media( '', $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}   
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
//Add your error action
} else {    
$attach_id = media_handle_upload( $file, $id );
$myNewImg = get_post_meta($id, 'usp-file-single', true);
}
}   
}
}
}

我试图在html表单之前放置以下内容

$myNewImg = get_post_meta($id, 'usp-file-single', true);
if (isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['uploadImgCustom'];
}

然而,页面在POST后刷新,但我看到了旧值,如果我在POST刷新后手动刷新,我可以看到新值tho。我用错if isset了吗?

感谢评论中的建议,我最终创建了一个页面刷新,如下所示:

<?php
$myNewUploaded = get_post_meta($id, 'usp-file-single', true);
if (isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['postImage'];
ob_start(); //this should be first line of your page
$myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: '.$myurl);
ob_end_flush(); //this should be last line of your page
}
?>
<form method="POST" action="" enctype="multipart/form-data">.....

尝试使用此

$myNewImg = get_post_meta($id, 'usp-file-single', true);
if (!isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['uploadImgCustom'];
}

最新更新