使用jquery/ajax提交的基本表单不起作用


Basic form submission with jquery/ajax not working

我是jquery和ajax的新手。我正在尝试让我的第一个ajax脚本工作,但它不工作,我需要一些帮助,请。

我有一个php页面,它应该发布到另一个php页,后者将在那里进行一些处理,并获得一些文件进行压缩和下载。用户需要输入开始和结束日期,第二个php脚本将使用这些信息来准备数据。这个功能在没有jquery的情况下运行得很好,但在我添加jquery时不起作用。

我想要实现什么我想在中发布到同一个php页面,并在<div></div>标记中获得发布输出。

我的第一个php页面包含(downloadPage.php):

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<form action="doDownload.php" method="post" id="dateRangeID">
<input id='time1' class='input' name="datefield1" style="text-align:center;"/>
<input id='time2' class='input' name="datefield2" style="text-align:center;"/>    
<input type="submit" value="Download Data" name="submit" id="submitButton">
</form>
<div id="result"></div> <!-- I would like it to post the result here //-->

第二页(doDownload.php),

<div id="content">
<?php 
if(isset($_POST['submit']))
{
    $dateVal1      = $_POST['datefield1'];
    $dateVal2      = $_POST['datefield2'];
    if($dateVal1 != $dateVal2)
    {    
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename="file.zip"');
        $fullListOfFiles = $downloadFullTmpFolder.$filesList;
        $command = "sudo $ldlib -u myuser /usr/bin/python3 $downloadScriptFile -datadir $gnomeDataDir -time1 $dateVal1C -time2 $dateVal2C -outdir $downloadFullTmpFolder > debug_download.txt 2>&1";
        $output = shell_exec($command);
        $fp = popen('cat '.$fullListOfFiles.' | sudo -u myuser zip -@ -9 - ', 'r');
        $bufsize = 1024;
        $buff = '';
        while( !feof($fp) ) 
        {
            $buff = fread($fp, $bufsize);
            echo $buff;
        }
        pclose($fp);   
    }
    else
    {
        echo("<p>Dates have to be different in order for the download to start.</p>");
    }
}
else
{
    echo("<p>Error: Page called without submit.</p>");
}
?>
</div>

最后,downloadPage.php中的jquery部分,如果我添加它,它就不再工作了(我想学习如何正确操作,我主要是从jquery手册中学习的,链接中的最后一个例子)

<script>
/* attach a submit handler to the form */
$("#dateRangeID").submit(
function(event) 
{
    event.preventDefault();
    var $form = $(this),
        t1 = $form.find("input[name='datefield1']").val(),
        t2 = $form.find("input[name='datefield2']").val(),
        subm = $form.find("input[name='submit']").val(),
        url = $form.attr('action');
    var posting = $.post(url, { datefield1: t1, datefield2: t2, submit: subm} );
    /* Put the results in a div */
    posting.done(function(data) {
        var content = $(data).find('#content');  // <--- So this turns out to be wrong. Right is only $(data);
        $("#result").empty().append(content);
    });
});
</script>

这是怎么回事?请协助。非常感谢。

如果您需要任何其他信息,请询问。

从显而易见的方面来看,您有:

var content = $(data).find('#content');

其中,您试图在以下结果之一中找到ID为content的元素:

<p>Dates have to be different in order for the download to start.</p>

<p>Error: Page called without submit.</p>