Click here to Skip to main content
15,880,469 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Can't figure out why I'm not able to access my projects array within my JSON object Pin
Richard Deeming24-Nov-21 21:33
mveRichard Deeming24-Nov-21 21:33 
GeneralRe: Can't figure out why I'm not able to access my projects array within my JSON object Pin
jkirkerx25-Nov-21 7:37
professionaljkirkerx25-Nov-21 7:37 
Questionwait for fetch to finish within a for each loop Pin
jkirkerx16-Nov-21 13:35
professionaljkirkerx16-Nov-21 13:35 
AnswerRe: wait for fetch to finish within a for each loop Pin
Richard Deeming16-Nov-21 22:49
mveRichard Deeming16-Nov-21 22:49 
GeneralRe: wait for fetch to finish within a for each loop Pin
jkirkerx17-Nov-21 5:52
professionaljkirkerx17-Nov-21 5:52 
QuestionRecommended Apps to learn java for student? Pin
kennymanda11-Nov-21 19:44
professionalkennymanda11-Nov-21 19:44 
AnswerRe: Recommended Apps to learn java for student? Pin
OriginalGriff11-Nov-21 19:45
mveOriginalGriff11-Nov-21 19:45 
GeneralMessage Closed Pin
16-Dec-21 11:25
cryptorobotics16-Dec-21 11:25 
AnswerRe: Recommended Apps to learn java for student? Pin
Richard MacCutchan11-Nov-21 21:04
mveRichard MacCutchan11-Nov-21 21:04 
AnswerRe: Recommended Apps to learn java for student? Pin
jhonaa28-Mar-22 0:10
jhonaa28-Mar-22 0:10 
QuestionUploading image using JQuery AJAX Pin
Alex Dunlop10-Nov-21 18:56
Alex Dunlop10-Nov-21 18:56 
Hi,
I want to upload an image file using JQuery AJAX and send it to php code for further processing including image cropping. I tried this code for Js:

<script type="text/javascript">
   $(document).ready(function() {
       $('#uploadstatus1').hide();
       $('#uploadstatus2').hide();

       $('#btnupload').click(function() {
           var file = $('#formFile')[0].files[0];
           var filename = file.name;
           var filedata = $('#formFile').prop('files')[0];
           var formdata = new FormData();
           formdata.append("file", filedata);
           formdata.append("filename", filename);
           $.ajax({
               url: "../app/model/saveimage.php",
               type: "POST",
               dataType: 'script',
               cache: false,
               processData: false,
               data: formdata,

               success: function(data2) {
                   if (data2 == 1) {
                       location.reload();
                   } else {
                       alert(data2);
                       $('#uploadstatus1').show();
                   }
               }
           });
       });
   });
   </script>


Uploading modal:

<div class="modal fade" id="uploadModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
           <div class="modal-dialog">
               <div class="modal-content">
                   <div class="modal-header">
                       <h5 class="modal-title" id="exampleModalLabel">Upload image</h5>
                       <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                   </div>
                   <div class="modal-body">
                       <form action="" method="POST" enctype="multipart/form-data">
                           <p>Upload file:</p>
                           <div class="mb-3">
                               <label for="formFile" class="form-label">Only Jpeg files are supported.</label>
                               <input class="form-control" type="file" id="formFile" name="formFile" required>
                           </div>
                       </form>
                       <div class="alert alert-danger" id="uploadstatus1" role="alert">
                           Upload failed!
                       </div>
                       <div class="alert alert-success" id="uploadstatus2" role="alert">
                           File uploaded successfully!
                       </div>
                   </div>
                   <div class="modal-footer">
                       <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                       <button type="button" class="btn btn-primary" name="btnupload" id="btnupload">Upload</button>
                   </div>
               </div>
           </div>
       </div>


saveimage.php:

if (isset($_POST['file'])) {
           try {

               //For uploading original image
               $crop = new Crop();

               $crop->img_name = $_FILES['file']['name'];
               $crop->tmp_img_name = $_FILES['file']['tmp_name'];
               $crop->folder = "../public/assets/uploadIMG/";
               //For finding file extension
               $crop->ext = pathinfo($crop->img_name, PATHINFO_EXTENSION);
               //Renaming the uploaded file
               $crop->new_name = $this->RandomStringGenerator() . "." . $crop->ext;
               $this->new_name = $crop->new_name;
               //Moving to the desired path
               move_uploaded_file($crop->tmp_img_name, $crop->folder . $crop->new_name);
               $this->RegisterIntoDatabase();

               //For cropping image and creating thumbnail
               $crop->RunCrop();

               unset($_POST);
               echo '1';
           } catch (\Throwable $th) {

               echo '2';
           }
       }else{
           echo '2';
       }


The problem is that
if (isset($_POST['file']))
returns false.
How can I solve this problem?
AnswerRe: Uploading image using JQuery AJAX Pin
Richard Deeming10-Nov-21 21:44
mveRichard Deeming10-Nov-21 21:44 
GeneralRe: Uploading image using JQuery AJAX Pin
Alex Dunlop10-Nov-21 22:14
Alex Dunlop10-Nov-21 22:14 
GeneralRe: Uploading image using JQuery AJAX Pin
Richard Deeming10-Nov-21 22:17
mveRichard Deeming10-Nov-21 22:17 
AnswerRe: Uploading image using JQuery AJAX Pin
jkirkerx16-Nov-21 13:50
professionaljkirkerx16-Nov-21 13:50 
GeneralRe: Uploading image using JQuery AJAX Pin
Richard Deeming17-Nov-21 0:46
mveRichard Deeming17-Nov-21 0:46 
GeneralRe: Uploading image using JQuery AJAX Pin
jkirkerx17-Nov-21 6:18
professionaljkirkerx17-Nov-21 6:18 
Questionhow do you paint objects with two different colors in the for repetition structure Pin
Member 1541326530-Oct-21 3:38
Member 1541326530-Oct-21 3:38 
AnswerRe: how do you paint objects with two different colors in the for repetition structure Pin
Richard MacCutchan30-Oct-21 4:25
mveRichard MacCutchan30-Oct-21 4:25 
GeneralRe: how do you paint objects with two different colors in the for repetition structure Pin
Member 1541326530-Oct-21 9:07
Member 1541326530-Oct-21 9:07 
AnswerRe: sytax error unexcepted token Pin
OriginalGriff5-Oct-21 21:20
mveOriginalGriff5-Oct-21 21:20 
GeneralRe: sytax error unexcepted token Pin
Richard Deeming5-Oct-21 21:44
mveRichard Deeming5-Oct-21 21:44 
GeneralRe: sytax error unexcepted token Pin
OriginalGriff5-Oct-21 21:52
mveOriginalGriff5-Oct-21 21:52 
Questiondata import via API Pin
Francesco Reboldi23-Sep-21 19:28
Francesco Reboldi23-Sep-21 19:28 
AnswerRe: data import via API Pin
Afzaal Ahmad Zeeshan23-Sep-21 23:47
professionalAfzaal Ahmad Zeeshan23-Sep-21 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.