简体   繁体   English

在PHP post方法中使用jquery

[英]using jquery in php post method

I am not very experienced in jquery and ajax but I need the following. 我对jquery和ajax的经验不是很丰富,但是我需要以下内容。 After users have successfully logged in they can download pdf files from the page. 用户成功登录后,他们可以从页面下载pdf文件。 The site has a collapse div from where they can choose which file to download. 该网站有一个折叠div,他们可以从中选择要下载的文件。 Everything is done in php and works ok. 一切都在php中完成,并且可以正常运行。 However, if the file is not found the collapse div is closed back. 但是,如果找不到该文件,则将div封闭。 In other words the web page gets reloaded. 换句话说,网页被重新加载。 So the user has to make a new selection newly which is inconvenient. 因此,用户不得不重新进行新的选择,这很不方便。 So I would the following. 因此,我将采取以下行动。 If the file is not found then the div should stay collapsed and the previous value in the input field 's' on the form. 如果找不到该文件,则div应该保持折叠状态,并且表单上输入字段's'中的前一个值。 I know this should be done via jquery or javascript. 我知道这应该通过jquery或javascript完成。 I have tried the following but it does not work. 我已经尝试了以下方法,但是它不起作用。

         //PHP

  <?php
    //if download is clicked
     if (isset($_POST["download"])) {
    $data = $_POST["s"];

     //if file is found
     if (fileexists($data){
    // We'll be outputting a PDF 
    header('Content-type: application/pdf'); 

    // It will be called downloaded.pdf 
    header('Content-Disposition: attachment; filename="downloaded.pdf"'); 

    // The PDF source is in original.pdf 
    readfile($data); 
    }
    //else the div should remain open
    else{  
         echo "<script> if ($('#collapseOne').is(':hidden')) {
               $('#collapseOne').show();
              }</script>";
     }
   } 
     ?>              

         <div id="collapseOne" class="panel-collapse collapse">
            <div class="panel-body">
                <form id='searchForm' method="post" action=''>
                <p><label>Select Month</label></p>
                <input id='s' name = 's' type='text' required><br><br>
                <button type="submit" id='download' name='download' class='btn btn-default btn-lg'>
                    <span class='glyphicon glyphicon-download-alt'></span> Download
                </button>
                <br> <br>
                <button id='download1' name = 'download1' class='btn btn-default btn-lg'>
                    <span class='glyphicon glyphicon-download-alt'></span> Download All
                </button>
               </form>              
              </div>
        </div>

Try using this! 试试这个!

$.ajax({
    url:"/path/to/your/script/",
    type: "post",
    data: //fetch the data which you want to pass
}).done(function(status){
    //status is the data returned from the script called
});

In short, you echo PHP into Javascript 简而言之,您将PHP回显为Javascript

<script> 
    $(document).ready(function(){
        var hasDownload = "<?php echo $_POST['download']; ?>";
        var oldInput = "<?php echo $_POST['s']; ?>";
        if(hasDownload == false) {
           $('#collapseOne').show();
           $('input#s').val(oldInput);
        }
    })        
</script>

When the server serves your file, those values will be present for your Javascript. 服务器提供文件时,这些值将显示在Javascript中。 Place you're jQuery at the bottom in another if statement & a doc ready block so that the dom is loaded first and echo your PHP variables into Javascript variables to perform your jQuery logic. 将您的jQuery放在另一个if语句和一个doc ready块的底部,以便首先加载dom,并将PHP变量回显到Javascript变量中以执行jQuery逻辑。

<?php

    if (isset($_POST["download"]) && fileexists($_POST["s"]) {
        header('Content-type: application/pdf'); 
        header('Content-Disposition: attachment; filename="downloaded.pdf"'); 
        readfile($_POST["s"]); 

    } 

?>              

<div id="collapseOne" class="panel-collapse collapse">
    <div class="panel-body">
        <form id='searchForm' method="post" action=''>
            <p><label>Select Month</label></p>
            <input id='s' name = 's' type='text' required><br><br>
            <button type="submit" id='download' name='download' class='btn btn-default btn-lg'>
                <span class='glyphicon glyphicon-download-alt'></span> Download
            </button>
            <br> <br>
            <button id='download1' name = 'download1' class='btn btn-default btn-lg'>
                <span class='glyphicon glyphicon-download-alt'></span> Download All
            </button>
       </form>              
    </div>
</div>

<?php if(isset($_POST["download"]) == false && fileexists($_POST["s"] == false) : ?>  
    <script> 
        $(document).ready(function(){
            var hasDownload = "<?php echo $_POST['download']; ?>";
            var oldInput = "<?php echo $_POST['s']; ?>";
            if(hasDownload == false) {
               $('#collapseOne').show();
               $('input#s').val(oldInput);
            }
        })        
    </script>
<?php endif; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM