1

I have tried many different stack solutions to similar issues and none have worked. I have a HTML and PHP mixed together and if the clicks the download image link the PHP code checks whether the agree to Terms of Use before downloading. If they agreed and click the "Download Your Image" link, it should automatically download. If they have not agreed, an alert should pop up.

Here is the working HTML checkbox code:

 <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>

Here is the working HTML a href download link (with PHP inside to identify file):

 <a href="<?php echo $new_image_with_overlay; ?>" download="<?php echo $new_image_name; ?>">Download Your Image</a>

Here is my PHP if statement (it works but not if I put the HTML a href inside):

 <?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')";
 }
 ?>

The other option I tried which I couldn't get to work but would prefer would be having the "Download Your Image" a button instead of a link. Either would be suitable for this.

jKraut
  • 2,325
  • 6
  • 35
  • 48
  • It looks like you're attempting to mix server side code (PHP) with client side code (Javascript) which isn't going to work. Have a look at this: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Dan Smith Mar 11 '15 at 13:40
  • @Bulk but I'm not writing any Javascript.. – jKraut Mar 11 '15 at 13:46
  • `alert` is javascript but perhaps I've misunderstood what you're trying to do, sorry. Can you include the code in the format that doesn't work for you? – Dan Smith Mar 11 '15 at 13:52

2 Answers2

1

If you simply want your link inside your isset condition, you can do this:

<?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>";
 }
 ?>

Your alert() wont work as that is mixing server and client side code, but the way I have added it above should work for you, as the script will fire when outputted.

Hope this helps!

Michael Doye
  • 8,063
  • 5
  • 40
  • 56
  • Apologies, just tested and updated code. works fine now. @jKraut – Michael Doye Mar 11 '15 at 14:47
  • yep that fixed the error. only issue now is that the Form Method loads prior to even clicking "test post" button of form. Also, when it executes, it does not automatically download the image, just shows link. – jKraut Mar 11 '15 at 15:08
  • Wrap your echo into `'` instead of `"`. – Eda190 Mar 11 '15 at 15:17
0

This should work here:

 if(isset($_POST['terms']) && $_POST['terms'] == "yes"){
Vallabha Vamaravelli
  • 1,153
  • 1
  • 9
  • 15