簡體   English   中英

HTML Checkbox with PHP下載圖像鏈接

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM