Show and Hide Button (href=) if file exist in server or not












0















I tried editing a similar solution but it doesnt do what it was intended to. Im not sure about my coding maybe im wrong and im using html. Is there a way to hide button(href=) if file exists then show if its not found? thank you!



ref: link



<button type="button" id="test_btn" style="display: none;">Download</button>

<script type="text/javascript">
$(document).ready(function () {
checkFile();

function checkFile() {
$.ajax({
url: '/path/to/file_checker.php',
type: 'GET',
success: function (data) {
if (data === "deleted") {
$('#test_btn').show();
}
else {
$('#test_btn').hidden();
}
}
});
}
}
</script>


Then your file checker php can be something similar to what you had:



if (file_exists("/aaa/file.txt")) {
echo "exists";
}
else {
echo "deleted";
}









share|improve this question



























    0















    I tried editing a similar solution but it doesnt do what it was intended to. Im not sure about my coding maybe im wrong and im using html. Is there a way to hide button(href=) if file exists then show if its not found? thank you!



    ref: link



    <button type="button" id="test_btn" style="display: none;">Download</button>

    <script type="text/javascript">
    $(document).ready(function () {
    checkFile();

    function checkFile() {
    $.ajax({
    url: '/path/to/file_checker.php',
    type: 'GET',
    success: function (data) {
    if (data === "deleted") {
    $('#test_btn').show();
    }
    else {
    $('#test_btn').hidden();
    }
    }
    });
    }
    }
    </script>


    Then your file checker php can be something similar to what you had:



    if (file_exists("/aaa/file.txt")) {
    echo "exists";
    }
    else {
    echo "deleted";
    }









    share|improve this question

























      0












      0








      0








      I tried editing a similar solution but it doesnt do what it was intended to. Im not sure about my coding maybe im wrong and im using html. Is there a way to hide button(href=) if file exists then show if its not found? thank you!



      ref: link



      <button type="button" id="test_btn" style="display: none;">Download</button>

      <script type="text/javascript">
      $(document).ready(function () {
      checkFile();

      function checkFile() {
      $.ajax({
      url: '/path/to/file_checker.php',
      type: 'GET',
      success: function (data) {
      if (data === "deleted") {
      $('#test_btn').show();
      }
      else {
      $('#test_btn').hidden();
      }
      }
      });
      }
      }
      </script>


      Then your file checker php can be something similar to what you had:



      if (file_exists("/aaa/file.txt")) {
      echo "exists";
      }
      else {
      echo "deleted";
      }









      share|improve this question














      I tried editing a similar solution but it doesnt do what it was intended to. Im not sure about my coding maybe im wrong and im using html. Is there a way to hide button(href=) if file exists then show if its not found? thank you!



      ref: link



      <button type="button" id="test_btn" style="display: none;">Download</button>

      <script type="text/javascript">
      $(document).ready(function () {
      checkFile();

      function checkFile() {
      $.ajax({
      url: '/path/to/file_checker.php',
      type: 'GET',
      success: function (data) {
      if (data === "deleted") {
      $('#test_btn').show();
      }
      else {
      $('#test_btn').hidden();
      }
      }
      });
      }
      }
      </script>


      Then your file checker php can be something similar to what you had:



      if (file_exists("/aaa/file.txt")) {
      echo "exists";
      }
      else {
      echo "deleted";
      }






      php css display






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 1:50









      nicollette16nicollette16

      125




      125
























          2 Answers
          2






          active

          oldest

          votes


















          1














          <script
          src="https://code.jquery.com/jquery-3.3.1.min.js"
          integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
          crossorigin="anonymous">

          </script>


          Add CSS class to your button and include jquery in your page



          .hidden {
          display: none
          }


          then



          <button type="button" class="hidden" id="test_btn">Download</button>

          <script>
          $(document).ready(function () {
          $.ajax({
          url: '/path/to/file_checker.php',
          type: 'GET',
          success:function(data){
          var obj = jQuery.parseJSON(data);
          if(obj.callback == 1) {
          $('button#test_btn').removeClass('hidden');
          }
          }
          });
          });
          </script>


          In your /path/to/file_checker.php



          if (file_exists("/aaa/file.txt")) {
          $data = array('callback' => 1);
          echo json_encode($data);
          }
          else {
          $data = array('callback' => 0);
          echo json_encode($data);
          }





          share|improve this answer































            1














            A different approach would be to offer a list of the files that exist to the user and allow them to select from the list.



            <?php
            // Initialize an empty array
            $files = ;

            // Get all the files in a directory (this will also return directories)
            foreach (glob('*') as $f) {

            // If the item is a file, add it to the files array
            if (is_file($f)) {
            $files = $f;
            }
            }
            ?>

            <!-- Create a select statement with the files that exist -->
            <select id="files">
            <!-- Placeholder -->
            <option disabled selected>Select file</option>

            <!-- Loop through all the files and create options for them -->
            <?php foreach ($files as $f) : ?>
            <option><?= $f ?></option>
            <?php endforeach ?>
            </select>

            <!-- The button would run the download using the selected file as the source (additional JavaScript required) -->
            <button id="go">Download</button>





            share|improve this answer
























            • wow thats great! i will try and learn how to implement this one. thank you very much!

              – nicollette16
              Nov 25 '18 at 4:21











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464001%2fshow-and-hide-button-href-if-file-exist-in-server-or-not%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            <script
            src="https://code.jquery.com/jquery-3.3.1.min.js"
            integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
            crossorigin="anonymous">

            </script>


            Add CSS class to your button and include jquery in your page



            .hidden {
            display: none
            }


            then



            <button type="button" class="hidden" id="test_btn">Download</button>

            <script>
            $(document).ready(function () {
            $.ajax({
            url: '/path/to/file_checker.php',
            type: 'GET',
            success:function(data){
            var obj = jQuery.parseJSON(data);
            if(obj.callback == 1) {
            $('button#test_btn').removeClass('hidden');
            }
            }
            });
            });
            </script>


            In your /path/to/file_checker.php



            if (file_exists("/aaa/file.txt")) {
            $data = array('callback' => 1);
            echo json_encode($data);
            }
            else {
            $data = array('callback' => 0);
            echo json_encode($data);
            }





            share|improve this answer




























              1














              <script
              src="https://code.jquery.com/jquery-3.3.1.min.js"
              integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
              crossorigin="anonymous">

              </script>


              Add CSS class to your button and include jquery in your page



              .hidden {
              display: none
              }


              then



              <button type="button" class="hidden" id="test_btn">Download</button>

              <script>
              $(document).ready(function () {
              $.ajax({
              url: '/path/to/file_checker.php',
              type: 'GET',
              success:function(data){
              var obj = jQuery.parseJSON(data);
              if(obj.callback == 1) {
              $('button#test_btn').removeClass('hidden');
              }
              }
              });
              });
              </script>


              In your /path/to/file_checker.php



              if (file_exists("/aaa/file.txt")) {
              $data = array('callback' => 1);
              echo json_encode($data);
              }
              else {
              $data = array('callback' => 0);
              echo json_encode($data);
              }





              share|improve this answer


























                1












                1








                1







                <script
                src="https://code.jquery.com/jquery-3.3.1.min.js"
                integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
                crossorigin="anonymous">

                </script>


                Add CSS class to your button and include jquery in your page



                .hidden {
                display: none
                }


                then



                <button type="button" class="hidden" id="test_btn">Download</button>

                <script>
                $(document).ready(function () {
                $.ajax({
                url: '/path/to/file_checker.php',
                type: 'GET',
                success:function(data){
                var obj = jQuery.parseJSON(data);
                if(obj.callback == 1) {
                $('button#test_btn').removeClass('hidden');
                }
                }
                });
                });
                </script>


                In your /path/to/file_checker.php



                if (file_exists("/aaa/file.txt")) {
                $data = array('callback' => 1);
                echo json_encode($data);
                }
                else {
                $data = array('callback' => 0);
                echo json_encode($data);
                }





                share|improve this answer













                <script
                src="https://code.jquery.com/jquery-3.3.1.min.js"
                integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
                crossorigin="anonymous">

                </script>


                Add CSS class to your button and include jquery in your page



                .hidden {
                display: none
                }


                then



                <button type="button" class="hidden" id="test_btn">Download</button>

                <script>
                $(document).ready(function () {
                $.ajax({
                url: '/path/to/file_checker.php',
                type: 'GET',
                success:function(data){
                var obj = jQuery.parseJSON(data);
                if(obj.callback == 1) {
                $('button#test_btn').removeClass('hidden');
                }
                }
                });
                });
                </script>


                In your /path/to/file_checker.php



                if (file_exists("/aaa/file.txt")) {
                $data = array('callback' => 1);
                echo json_encode($data);
                }
                else {
                $data = array('callback' => 0);
                echo json_encode($data);
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 3:27









                Mohsin MujawarMohsin Mujawar

                666




                666

























                    1














                    A different approach would be to offer a list of the files that exist to the user and allow them to select from the list.



                    <?php
                    // Initialize an empty array
                    $files = ;

                    // Get all the files in a directory (this will also return directories)
                    foreach (glob('*') as $f) {

                    // If the item is a file, add it to the files array
                    if (is_file($f)) {
                    $files = $f;
                    }
                    }
                    ?>

                    <!-- Create a select statement with the files that exist -->
                    <select id="files">
                    <!-- Placeholder -->
                    <option disabled selected>Select file</option>

                    <!-- Loop through all the files and create options for them -->
                    <?php foreach ($files as $f) : ?>
                    <option><?= $f ?></option>
                    <?php endforeach ?>
                    </select>

                    <!-- The button would run the download using the selected file as the source (additional JavaScript required) -->
                    <button id="go">Download</button>





                    share|improve this answer
























                    • wow thats great! i will try and learn how to implement this one. thank you very much!

                      – nicollette16
                      Nov 25 '18 at 4:21
















                    1














                    A different approach would be to offer a list of the files that exist to the user and allow them to select from the list.



                    <?php
                    // Initialize an empty array
                    $files = ;

                    // Get all the files in a directory (this will also return directories)
                    foreach (glob('*') as $f) {

                    // If the item is a file, add it to the files array
                    if (is_file($f)) {
                    $files = $f;
                    }
                    }
                    ?>

                    <!-- Create a select statement with the files that exist -->
                    <select id="files">
                    <!-- Placeholder -->
                    <option disabled selected>Select file</option>

                    <!-- Loop through all the files and create options for them -->
                    <?php foreach ($files as $f) : ?>
                    <option><?= $f ?></option>
                    <?php endforeach ?>
                    </select>

                    <!-- The button would run the download using the selected file as the source (additional JavaScript required) -->
                    <button id="go">Download</button>





                    share|improve this answer
























                    • wow thats great! i will try and learn how to implement this one. thank you very much!

                      – nicollette16
                      Nov 25 '18 at 4:21














                    1












                    1








                    1







                    A different approach would be to offer a list of the files that exist to the user and allow them to select from the list.



                    <?php
                    // Initialize an empty array
                    $files = ;

                    // Get all the files in a directory (this will also return directories)
                    foreach (glob('*') as $f) {

                    // If the item is a file, add it to the files array
                    if (is_file($f)) {
                    $files = $f;
                    }
                    }
                    ?>

                    <!-- Create a select statement with the files that exist -->
                    <select id="files">
                    <!-- Placeholder -->
                    <option disabled selected>Select file</option>

                    <!-- Loop through all the files and create options for them -->
                    <?php foreach ($files as $f) : ?>
                    <option><?= $f ?></option>
                    <?php endforeach ?>
                    </select>

                    <!-- The button would run the download using the selected file as the source (additional JavaScript required) -->
                    <button id="go">Download</button>





                    share|improve this answer













                    A different approach would be to offer a list of the files that exist to the user and allow them to select from the list.



                    <?php
                    // Initialize an empty array
                    $files = ;

                    // Get all the files in a directory (this will also return directories)
                    foreach (glob('*') as $f) {

                    // If the item is a file, add it to the files array
                    if (is_file($f)) {
                    $files = $f;
                    }
                    }
                    ?>

                    <!-- Create a select statement with the files that exist -->
                    <select id="files">
                    <!-- Placeholder -->
                    <option disabled selected>Select file</option>

                    <!-- Loop through all the files and create options for them -->
                    <?php foreach ($files as $f) : ?>
                    <option><?= $f ?></option>
                    <?php endforeach ?>
                    </select>

                    <!-- The button would run the download using the selected file as the source (additional JavaScript required) -->
                    <button id="go">Download</button>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 4:12









                    user2182349user2182349

                    7,21321633




                    7,21321633













                    • wow thats great! i will try and learn how to implement this one. thank you very much!

                      – nicollette16
                      Nov 25 '18 at 4:21



















                    • wow thats great! i will try and learn how to implement this one. thank you very much!

                      – nicollette16
                      Nov 25 '18 at 4:21

















                    wow thats great! i will try and learn how to implement this one. thank you very much!

                    – nicollette16
                    Nov 25 '18 at 4:21





                    wow thats great! i will try and learn how to implement this one. thank you very much!

                    – nicollette16
                    Nov 25 '18 at 4:21


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464001%2fshow-and-hide-button-href-if-file-exist-in-server-or-not%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'