0

I have html form in PHP page which offers image download in different sizes.

<form action='download.php' method='post'>
     <select name='size'>
          <option value='1'>Small</option>
          <option value='2'>Medium</option>
          <option value='3'>Large</option>
     </select>
     <button type='submit'>Download</button>
</form>

And in "download.php" page there is code which generate image and start downloading automatically using header.

header('Content-type: image/jpeg');
header('Content-Disposition: attachment; filename="'.$filename.'"');        
imagejpeg($image, null, 100);

When I click on download button, form submits successfully and image starts downloading. Because I am using header in download.php file, current page not redirects to download.php file, and form remains as it is. Everything works till now.

Now if I click on download button again, form redirects to download.php file, with no image downloading.

Is there any way to work download button more than one time, without refreshing my index page?

Notes:

1.) I am using jquery on index page.

2.) Instead of <input type='submit' />, I am using <button type='submit'></button> because I am using font-awesome jquery plugin to put icon in button. Which is not possible in input tag.

1 Answer 1

1

You have use jquery ajax to do what you are asking.

You can find the ajax Tutorial here.

Step 1: You need to post the data using ajax

Step 2: For resetting the field you can jquery to reset the field using their IDs

Ajax jquery download script

Sign up to request clarification or add additional context in comments.

3 Comments

Form can be submit using ajax but downloading not starts automatically.
Ajax jquery download script will work with little modification, thanks.
Thanks a lot, Ajax jquery download plugin is working like a charm. It doesn't even need form, simple <a> tag will do the trick. Thanks again..

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.