Ajax post to PHP redirects instead of return result -
i'm trying make php-page uploading user avatars, want selected picture updated automatically after file-selection (possibly dialog confirm change of avatar). far i've got auto-upload bit working, i'm having trouble using post-function external php-file. while testing, instead of returning results ajax, redirects external php-file , echoes result there. i'm sure there basic thing i'm overlooking, love can't seem head around it. i've managed results ajax in success-function, here doesn't alert (only redirects). i've yet implent file-uploading bit, have working on section of site need code working.
so want answered is;
- how can callback without redirecting?
- how (and where) should implement confirm-dialog before post uploadavatar.php?
here code.
html / javascript / ajax
<?php include_once 'includes/db_connect.php'; include_once 'includes/functions.php'; sec_session_start(); include_once 'header.php'; ?> <!doctype html> <html> <head> <link rel="stylesheet" href="style.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script> function chooseavatar() { $("#avatarinput").click();} $('form#avatarform').submit( function( e ) { e.preventdefault(); var formdata = new formdata($(this)[0]); $.ajax( { url: 'uploadavatar.php', type: 'post', data: formdata, async: false, cache: false, contenttype: false, processdata: false, success: function (returndata) { alert(returndata); } }); return false; }); </script> </head> <body> <section> <h1>edit profile: <?php echo htmlentities($_session['username']) ?></h1> <article> <div style="height:0px;overflow:hidden"> <form action="uploadavatar.php" method="post" enctype="multipart/form-data" name="avatarform" id="avatarform"> <input type="file" id="avatarinput" name="avatarinput" /> <script> $("input[name='avatarinput']").change(function() { $( 'form#avatarform' ).submit(); }); </script> </form> </div> <p><span class='label_viewprofile'>avatar (65x65):</span><img src="avatar/avatar.jpg" class="avatar-box" onclick="chooseavatar();"></p> <hr class="editprofile-line"> </article> </section> </body> </html> external 'uploadavatar.php' file
<?php echo "working!"; print_r ($_files); print_r ($_post); ?> thank or input! i'm new programming in these languages, feedback appreciated.
edit 29/4-15: - posted full code now. had 2 other forms , more code on page removed check if there conflict, code i'm working now. still redirecting , not giving 'returndata' ajax, , i've had no luck removing form "action=" part either (didn't appear post or redirect). again, grateful time , effort people put helping me. have little experience js , ajax, i'm ordering book learn more, again!
welcome so. called wrong event (e), not (event):
$('form#avatarform').submit( function( e ) { e.preventdefault(); this prevent page being submitted, , use $.ajax().
edit:
https://jsfiddle.net/twisty/0feqyqmw/
made few changes overall, should work:
$(function () { $("#avatarbtn").click(function () { $("#avatarinput").click(); }); $("input[name='avatarinput']").on("change", function(){ $('form#avatarform').submit(); }); $('form#avatarform').submit(function (e) { e.preventdefault(); var formdata = new formdata($(this)[0]); $.ajax({ url: 'uploadavatar.php', type: 'post', data: formdata, async: false, cache: false, contenttype: false, processdata: false, success: function (returndata) { alert(returndata); } }); return false; }); }); i moved script jquery ease of use. added bit of wrapping in html:
<section> <h1>edit profile: username</h1> <article> <div style="height:0px;overflow:hidden"> <form action="uploadavatar.php" method="post" enctype="multipart/form-data" name="avatarform" id="avatarform"> <input type="file" id="avatarinput" name="avatarinput" /> </form> </div> <p> <span class='label_viewprofile'>avatar (65x65):</span> <label id="avatarbtn"> <img src="avatar/avatar.jpg" class="avatar-box" /> </label> </p> <hr class="editprofile-line"> </article> </section>
Comments
Post a Comment