gpt4 book ai didi

javascript - 多次显示警报错误消息

转载 作者:行者123 更新时间:2023-11-28 01:18:49 25 4
gpt4 key购买 nike

代码如下:

    if(isset($_FILES['hiddenfilebutton'])){
$img_name = $_FILES['hiddenfilebutton']['name'];
$img_temp = $_FILES['hiddenfilebutton']['tmp_name'];
$a = explode('.',$img_name);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$img_extension = strtolower(end($a));
unset($a);
$img_size = $_FILES['hiddenfilebutton']['size'];
if($img_size > 3000000) {
$error = 'Image should be less than 4 MB';
} else if(!in_array($img_extension, $allowed_ext)) {
$error = "Unsupported image format";
}
}
?>
<script type="text/javascript">
if('<?php echo $error; ?>' != '' && '<?php echo $error; ?>' != undefined) {
alert("<?php echo $error; ?>");
}
</script>

问题是,每当我上传不受支持的图像类型(例如 .tiff 图像)时,都会显示警报框。这按照我想要的方式工作。但如果我重新加载它,它会再次显示警报框并显示相同的消息。第三次重新加载时就没有出现了。我希望警报消息只显示一次,而不是两次或三次..

最佳答案

将代码更改为:

<?php 

if(isset($_FILES['hiddenfilebutton'])){
$img_name = $_FILES['hiddenfilebutton']['name'];
$img_temp = $_FILES['hiddenfilebutton']['tmp_name'];
$a = explode('.',$img_name);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$img_extension = strtolower(end($a));
unset($a);
$img_size = $_FILES['hiddenfilebutton']['size'];
if($img_size > 3000000) {
$error = 'Image should be less than 4 MB';
} else if(!in_array($img_extension, $allowed_ext)) {
$error = "Unsupported image format";
}
}

if(isset($error)){
?>
<script type="text/javascript">
alert("<?php echo $error; ?>");
</script>
<?php
}
?>

<?php 

if(isset($_FILES['hiddenfilebutton'])){
$img_name = $_FILES['hiddenfilebutton']['name'];
$img_temp = $_FILES['hiddenfilebutton']['tmp_name'];
$a = explode('.',$img_name);
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$img_extension = strtolower(end($a));
unset($a);
$img_size = $_FILES['hiddenfilebutton']['size'];
if($img_size > 3000000) {
$error = 'Image should be less than 4 MB';
} else if(!in_array($img_extension, $allowed_ext)) {
$error = "Unsupported image format";
}
}

if(isset($error)){
echo "<script type='text/javascript'> alert('<?php echo $error; ?>'); </script>";
}
?>

关于javascript - 多次显示警报错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23475363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com