1

this is my html form

<form id="contactForm1" method="POST" action="downloadfile">
   <input id="tesst" name="tesst" type="hidden" value="<?php echo $val['file_name'];?>"/>
   <div id="download" class="btn btn-danger">Download</div>
</form>

And here is the Jquery function

var frm = $('#contactForm1');
              frm.submit(function(ev) {
                 $.ajax({
                   type: frm.attr('method'),
                   url: frm.attr('action'),
                   data: frm.serialize(),
                   success: function(data) {
                      alert('ok');
                   }
                  });
                 ev.preventDefault();
              });

I don't know much about Jquery.

Please anybody can help me with this?

Thanks.

14
  • 3
    it misses the input submit button / you have to use the click event instead of submit - frm.submit to $('#download').click Commented Mar 5, 2014 at 15:38
  • 1
    What server side language are you using? Commented Mar 5, 2014 at 15:47
  • 1
    I didn't quite test it when I wrote it but it seems to work fine > jsfiddle.net/Spokey/bNa7d (not sure if you still added the var frm) Commented Mar 5, 2014 at 15:48
  • 1
    action doesn't require an extension if .htaccess is configured for it Commented Mar 5, 2014 at 15:50
  • 1
    If your code is in the head then yes. If you put it before you end body you won't need that Commented Mar 5, 2014 at 16:01

2 Answers 2

1

There are 2 ways to solve your problem

  1. You change your div (download button) to a input submit button and leave your script as it is
  2. You change the function to run when the div is clicked and not when the submit event is fired (the reason is that submit is only fired by a submit button)

Example: http://jsfiddle.net/Spokey/bNa7d/

var frm = $('#contactForm1');
$('#download').click(function (ev) {
    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        success: function (data) {
            alert('ok' + data);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Two things

  • You need to add input submit element to your form

  • Change action="downloadfile" to action="downloadfile.php" if your downloadfile.php is the same directory with your current HTML file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.