调用PHP函数从阿贾克斯不工作函数、工作、PHP、阿贾克斯不

2023-09-10 19:59:46 作者:无人及你

我试图调用使用AJAX PHP函数。下面是我使用的脚本。

 <脚本类型=文/ JavaScript的SRC =jquery.1.4.2.js>

    $(文件)。就绪(函数(){

               //后,根据编辑
               // @的codeparadox答案

       $('#本地)。点击(函数(五){
            即preventDefault();
            e.stopPropagation();
            promptdownload();
       });
    });

        功能promptdownload(五)
        {
        $阿贾克斯({
             键入:POST,
             网址:JS / prompt.php
             数据:{得:runfunction,行动:promptlocal},
             成功:函数(响应){
             }
         });
        }
< / SCRIPT>
 

相应的PHP code(prompt.php)是:

 < PHP
$ PATH1 =下载/ 1.JPG;
$ browserFilename1 =本地游;
$ mimeType1 =为image / jpeg;


功能promptToDownload($路径,$ browserFilename,$ MIMETYPE)
{

    如果(!file_exists($ PATH)||!is_readable($路径)){

        返回null;
    }

    标题(内容类型:$ MIMETYPE。);
    标题(内容处置:附件;文件名= \$ browserFilename \);
    头('过期:gmdate(D,D MYH:I:S',gmmktime() -  3600)GMT);
    标题(内容长度:文件大小($路径));
    //如果你愿意,你可以添加一些code在这里跟踪或登录下载

    //特殊标题为IE 6
    标题(缓存控制:必重新验证,后检查= 0,pre-检查= 0');
    头('杂注:公开');
    $计划生育=的fopen($路径,R);
    fpassthru($ FP);
}

如果($ _ POST [行动] ='promptlocal')
{
    promptToDownload($ _ GET [$路径1],$ browserFilename1,$ mimeType1); //评论
}

?>
 

这是我的code,它应该触发功能键:

 <输入类型=按钮ID =本地NAME =本地值=本地游>
 
2017 年 PHP 社区总结,2018 PHP 发展展望

我的预期成果是有这个按钮PROMT用户:,其中保存1.JPG文件

但我不能使它工作。

任何建议是高度AP preciated。

解决方案

  $(本地)。点击(函数(五){
 

  $('#本地)。点击(函数(五){
 

由于本地 ID 所以你应该使用之前。而且在你的PHP code也有一些失踪的报价。

I am trying to call a PHP function using AJAX. Below is the script I used.

<script type="text/javascript" src="jquery.1.4.2.js">

    $(document).ready(function () { 

               // after EDIT according to 
               // @thecodeparadox answer

       $('#local').click(function(e){
            e.preventDefault();
            e.stopPropagation();
            promptdownload();
       });
    });

        function promptdownload(e) 
        {
        $.ajax({
             type: "POST",
             url: "js/prompt.php",
             data: { "get" : "runfunction", "action" : "promptlocal" },
             success: function (response) {
             }    
         });
        }
</script>

The corresponding PHP code (prompt.php) is:

<?php
$path1 = "downloads/1.jpg";
$browserFilename1 = "Local Travel";
$mimeType1 = "image/jpeg";


function promptToDownload($path, $browserFilename, $mimeType)
{

    if (!file_exists($path) || !is_readable($path)) {

        return null;
    }

    header("Content-Type: " . $mimeType);
    header("Content-Disposition: attachment; filename=\"$browserFilename\"");
    header('Expires: ' . gmdate('D, d M Y H:i:s', gmmktime() - 3600) . ' GMT');
    header("Content-Length: " . filesize($path));
    // If you wish you can add some code here to track or log the download

    // Special headers for IE 6
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    $fp = fopen($path, "r");
    fpassthru($fp);
}

if ($_POST["action"] = 'promptlocal')
{
    promptToDownload($_GET[$path1], $browserFilename1, $mimeType1);//comments
}

?>

This is how I code the button that is supposed to trigger the function:

<input type="button" id="local" name="local" value="Local Travel">

My expected output is to have this button promt the user: "where to save 1.jpg file".

However I couldn't make it work.

Any advise is highly appreciated.

解决方案

$('local').click(function(e){

should be

$('#local').click(function(e){

As local is an id so you should use # before it. And also in your php code there are some missing quotes.