Created
September 30, 2017 12:41
-
-
Save Sanix-Darker/913f1c5d696d575337eb8efe38fce1bb to your computer and use it in GitHub Desktop.
[PHP, JS, HTML] Simple Uploading form with files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- For a friend D Dark Burning, just copy the block you need, i don't ever test this brick of code but it's work --> | |
| <div class="col-md-3"> | |
| <center> | |
| <img id="blah" src="../site/images/product_default.png" alt="Image du produit" style="width: 100%;max-height: 97px;" /> | |
| <input type="button" value="Changer l'image" class="btn btn-default" id="addImage" onclick="addImage()"> | |
| <input type="file" id="imgInp" name="img" required="required" class="form-control col-md-9 col-xs-12" required="" style="display: none;" onchange="readURL(this,'blah');"/> | |
| </center> | |
| </div> | |
| <div class="col-md-9"> | |
| <div class="col-md-12"> | |
| <div class="form-group"> | |
| <label>Nom du produit</label> | |
| <input type="text" id="nom" name="nom" class="form-control" placeholder=""> | |
| </div> | |
| </div> | |
| <div class="col-md-6"> | |
| <div class="form-group"> | |
| <label>Prix</label> | |
| <input type="text" id="prix" name="prix" class="form-control" placeholder="En euro"> | |
| </div> | |
| </div> | |
| <div class="col-md-6"> | |
| <div class="form-group"> | |
| <label>Quantité</label> | |
| <input type="number" id="quantite" name="quantite" class="form-control" placeholder=""> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="col-md-12"> | |
| <div class="form-group"> | |
| <label>Description</label> | |
| <textarea class="form-control" name="description" id="description" placeholder="" style="width: 97%;" rows="5"></textarea> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="pust" style="text-align: right;padding-right: 30px;"> | |
| <button data-dismiss="modal" class="btn btn-default" type="button">Fermer</button> | |
| <button class="btn btn-success" type="button" id="saveProduit">Enregistrer</button> | |
| </div><br><br> | |
| <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| $(document).on('click', '#saveProduit', function(){ | |
| var formData = new FormData(); | |
| formData.append('img', $('input[type=file]')[0].files[0]); | |
| formData.append('nom', $('#nom').val()); | |
| formData.append('prix', $('#prix').val()); | |
| formData.append('quantite', $('#quantite').val()); | |
| formData.append('description', $('#description').val()); | |
| submitLeForm(formData, 'produit/add', undefined, 'contentproduit', 'produit/show', 3); | |
| }); | |
| // Pour le preview de l'image | |
| function readURL(input,cible) { | |
| if (input.files && input.files[0]) { | |
| var reader = new FileReader(); | |
| reader.onload = function (e) { | |
| $('body #'+cible).attr('src', e.target.result); | |
| } | |
| reader.readAsDataURL(input.files[0]); | |
| } | |
| } | |
| $("#imgInp").change(function(){ | |
| readURL(this,'blah'); | |
| }); | |
| // Pour soumettre le formuaire | |
| function submitLeForm(formdata, url, modalToClose, ContentToRefresh, UrlForRefresh, TypeForRefresh, nbreColonnes){ | |
| $('.resultERROR').html('<center><i class="fa fa-spinner fa-spin"></i>Traitement en cours...</center>'); | |
| $.ajax({ | |
| url: url, | |
| data: formdata, | |
| type: 'POST', | |
| contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+) | |
| processData: false, // NEEDED, DON'T OMIT THIS | |
| success: function(data){ | |
| // alert(data) | |
| $('.resultERROR').html(data); | |
| $('.resultERROR').slideDown(); | |
| $('#'+ContentToRefresh).html('<tr><td colspan="'+nbreColonnes+'"><center><h5><i class="fa fa-spinner fa-spin"></i> Chargement en cours...</h5></center></td></tr>'); | |
| $.ajax({ | |
| type: "POST", | |
| url: UrlForRefresh, | |
| data: {'type': TypeForRefresh}, | |
| dataType: "text", | |
| success: function(datao){ | |
| alert(datao) | |
| } | |
| }); | |
| return false; | |
| } | |
| }); | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment