Basic form submission with jquery/ajax not working

The Quantum Physicist

I'm new to jquery and ajax. I'm trying to get my first ajax script to work, but it's not working and I need some assistance, please.

I have a php page that is supposed to post to another php page, where the latter will do some processing and get some files to get zipped and download. The user needs to input a starting and ending date, and the second php script will use this information to prepare the data. This functionality works perfectly fine without jquery, but doesn't work when I add jquery.

What do I want to achieve? I want to post in the to the same php page and get the post output in a <div></div> tag.

My first php page contains (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 //-->

The second page (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>

Finally, the jquery part in downloadPage.php, which if I add it doesn't work anymore (which I'd like to learn how to do right, and I mainly learned from the manual of jquery, the last example in the link)

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

What is wrong in this? Please assist. Thank you.

If you require any additional information, please ask.

hjpotter92

Looking at the obvious, you have:

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

where, you're trying to find an element with the ID content in one of the following results:

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

or

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery form submission is not working

From Dev

Form submission is not working

From Dev

form submission not working?

From Dev

Angular submission form not working

From Dev

Ruby on Rails - basic form submission

From Dev

Javascript Auto Form submission not working

From Dev

wordpress ajax form submission is not working?

From Dev

Listener for form submission not working with jQuery

From Dev

Spring MVC, Form Submission not working

From Dev

Auto form Submission not working Ajax

From Dev

Spring MVC, Form Submission not working

From Dev

Form Post Not working While Submission

From Dev

wordpress ajax form submission is not working?

From Dev

Bootstrap Form Validation or Required not working on Ajax submission

From Dev

Form submission cancel using javascript not working

From Dev

Ajax form submission with Jquery validation plugin is not working

From Dev

php form submission is working but erroring instead of redirecting

From Dev

Regular Expression not working - Online form validation and submission

From Dev

Javascript Form Submission Not Working. What could it be?

From Dev

isset not working on form submission (was actually db issue)

From Dev

PHP - Access Code Form Submission not working?

From Dev

AJAX form submission based on ID not working

From Dev

angularjs formly basic form not working

From Dev

onSubmit is not working in struts2 Mobile form submission

From Dev

Bootstrap dropdown not working after initial ajax form submission

From Dev

Rails 4 shallow routes resource form submission not working

From Dev

JQuery form submission without page refresh not working due to JQuery conflict

From Dev

jQuery ajax form submission to php not working with response from server

From Dev

Form Submission not working second time after submiting using Jquery and Ajax

Related Related

  1. 1

    Jquery form submission is not working

  2. 2

    Form submission is not working

  3. 3

    form submission not working?

  4. 4

    Angular submission form not working

  5. 5

    Ruby on Rails - basic form submission

  6. 6

    Javascript Auto Form submission not working

  7. 7

    wordpress ajax form submission is not working?

  8. 8

    Listener for form submission not working with jQuery

  9. 9

    Spring MVC, Form Submission not working

  10. 10

    Auto form Submission not working Ajax

  11. 11

    Spring MVC, Form Submission not working

  12. 12

    Form Post Not working While Submission

  13. 13

    wordpress ajax form submission is not working?

  14. 14

    Bootstrap Form Validation or Required not working on Ajax submission

  15. 15

    Form submission cancel using javascript not working

  16. 16

    Ajax form submission with Jquery validation plugin is not working

  17. 17

    php form submission is working but erroring instead of redirecting

  18. 18

    Regular Expression not working - Online form validation and submission

  19. 19

    Javascript Form Submission Not Working. What could it be?

  20. 20

    isset not working on form submission (was actually db issue)

  21. 21

    PHP - Access Code Form Submission not working?

  22. 22

    AJAX form submission based on ID not working

  23. 23

    angularjs formly basic form not working

  24. 24

    onSubmit is not working in struts2 Mobile form submission

  25. 25

    Bootstrap dropdown not working after initial ajax form submission

  26. 26

    Rails 4 shallow routes resource form submission not working

  27. 27

    JQuery form submission without page refresh not working due to JQuery conflict

  28. 28

    jQuery ajax form submission to php not working with response from server

  29. 29

    Form Submission not working second time after submiting using Jquery and Ajax

HotTag

Archive