[英]HTML Checkbox with PHP to download image link
我已经尝试了许多不同的堆栈解决方案来解决类似问题,但都没有成功。 我将HTML和PHP混合在一起,如果单击下载图像链接,PHP代码将在下载前检查是否同意使用条款。 如果他们同意并单击“下载您的图像”链接,它将自动下载。 如果他们不同意,则应弹出警报。
这是有效的HTML复选框代码:
<form method="post">
<input type="checkbox" name="terms" value="yes" id="agree" /> I have read and agree to the Terms and Conditions</br>
<button type="submit">test_post</button>
</form>
这是工作中的HTML a href下载链接(其中包含PHP来标识文件):
<a href="<?php echo $new_image_with_overlay; ?>" download="<?php echo $new_image_name; ?>">Download Your Image</a>
这是我的PHP if语句(它可以工作,但是如果我将HTML a href放入其中,则不行):
<?php
if(isset($_POST['terms'])){
// the HTML a href or similar would go here to automatically download the image
} else {
// would like an alert presented to the user
echo "alert('Please indicate that you have read and agree to the Terms of Use')";
}
?>
我尝试过的另一个选项是我无法使用,但希望使用的是“下载图像”按钮而不是链接。 两者都适合。
如果只希望链接处于isset
条件内,则可以执行以下操作:
<?php
if(isset($_POST['terms'])){
echo "<a href=".$new_image_with_overlay." download=" .$new_image_name.">Download Your Image</a>";
} else {
$message = "Please indicate that you have read and agree to the Terms of Use";
echo "<script type='text/javascript'>alert('$message');</script>";
}
?>
您的alert()
正常工作,因为它将服务器和客户端代码混合在一起,但是我在上面添加的方式应该对您有用,因为脚本会在输出时触发。
希望这可以帮助!
这应该在这里工作:
if(isset($_POST['terms']) && $_POST['terms'] == "yes"){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.