php - Submitting a form with jQuery/Ajax only works every other time -
php - Submitting a form with jQuery/Ajax only works every other time -
i'm trying submit form includes file upload via ajax/jquery, process form through php script, , homecoming result in div form resided in.
my current form code is:
<section id="content-right"> <form name="uploader" id="uploader" method="post" enctype="multipart/form-data"> <input type="hidden" id="max_file_size" name="max_file_size" value="10485760" /> <input type="file" name="fileselect" id="fileselect" /> <input type="submit" name="submit" id="submit" value="upload" /> </form> </section>
and current ajax/jquery script is:
<script> $(function() { $('#uploader').submit(function() { $(this).ajaxsubmit({ type: $(this).attr('method'), url: 'upload-song.php', success: function(response) { $('#content-right').html(response); } }); homecoming false; }); });
my php script "upload-song.php" (the details don't matter).
i have yui.pjax running handle normal navigation (a href) links , load in #content-right (if user clicks anything, want loading in #content-right).
with set up, navigating through normal links works perfectly, loads in #content-right, uploader works every other time.
for example, uploader load upload-song.php in #content-right , process perfectly, if navigate away page , seek upload item, won't work, it'll refresh page (if set action="upload-song.php" in form tag it'll load upload-song.php total page, not in #content-right). after refreshes page can upload item , work perfectly.
i think has how i'm attaching ajax script form submit (because if refresh page works perfectly), don't have lot of experience these languages i'm not sure how prepare it.
in addition, if disable yui.pjax fixes uploader breaks links, i'm looking work around.
any ideas?
try this:
$(document).on("submit", "#uploader", function() ...
this syntax allow submit event bubble document. way, when #content_right
section reloads, document retains event listening response set in dom ready function.
php jquery ajax forms yui
Comments
Post a Comment