繁体   English   中英

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

暂无
暂无

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

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