PHP Image Upload Via JQUERY -
i'm trying pass form data (avatar & user id) through jquery post command, won't upload file. can please point me in right direction might doing wrong? here form , script:
<form id="avatar"> <script> $(document).ready(function() { var ok = true; $('#avatarbutton').click(function() { var id=$("#id").val(); var avatar = $("#avatar").val(); if(ok == true) { $('.avatarvalidation').html("uploading avatar").removeclass("error").addclass("success"); jquery.post("php/<?php echo $usertype; ?>/avatar.php", { id:id, avatar:avatar }, function(data, textstatus){ if(data == 1){ $('.avatarvalidation').html("profile updated").removeclass("error").addclass("success"); } else if(data == 2){ $('.avatarvalidation').html("error").removeclass("success").addclass("error"); } }); } else { $('.avatarvalidation').html("no").removeclass("success").addclass("error"); } return false; }); }); </script> <table> <tr> <td class="textcolumn profilecolumn">add avatar:</td> <td class="profileinput inputcolumn"><input type="file" id="avatar" name="avatar" value="<?php echo $yourname; ?>"/></td> </tr> <tr> <td colspan="2"><input type="hidden" name="id" id="id" value="<?php echo $yourid; ?>"></td> </tr> <tr class="buttonspacer"></tr> <tr> <td colspan="2"> <input type="submit" class="submitbutton" id="avatarbutton" value="upload avatar" /> <span class="submitvalidation avatarvalidation"></span> </td> </tr> </table> </form>
and here php form data passed to:
<?php $id= mysqli_real_escape_string($con, $_request["id"]); $avatar= mysqli_real_escape_string($con, $_request["avatar"]); if ($_files['$avatar']['error'] > 0) { echo "2"; //echo error } else { // array of valid extensions $validextensions = array('.jpg', '.jpeg', '.gif', '.png'); // extension of uploaded file $fileextension = strrchr($_files['$avatar']['name'], "."); // check if file extension on list of allowed ones if (in_array($fileextension, $validextensions)) { $newname = time() . '_' . $_files['$avatar']['name']; $destination = 'avatar/' . $newname; if (move_uploaded_file($_files['$avatar']['tmp_name'], $destination)) { echo "1"; //echo success } } else { echo "2"; //echo error } } ?>
you can try plugin, take care of of background work you. have php samples well:
Comments
Post a Comment